Monday, July 13, 2009

Updating SPGroup owner

Hi

To update the owner of the group, you simply set the Owner property of the SPGroup object to a SPMember (which can be a SPUser or an SPGroup).

SPGroup spGroup = currentWeb.SiteGroups["GroupName"];
spGroup1.Owner = spGroup2;
sp.Group1.Update();

I was trying to update the owner of the SharePoint group through the API using the code above, but unfortunately that did not work.

So i did what most of the developers do, google it (i mean bing it). However i didn't find an answer to my question, so i tried to change my code and debug it until i found the solution.

For some reason, i had to get the reference to the SharePoint group from the root web of the site collection, rather than getting it from the web SiteGroups property.

so to fix the problem, i changed the above code to:
SPGroup spGroup = currentWeb.Site.RootWeb.SiteGroups["GroupName"];
spGroup1.Owner = spGroup2;
sp.Group1.Update();

I hope you will find this useful.




No comments:

Post a Comment