Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am having various site collections and I want to add few users into administrator group. So that more people will have access to edit the groups. I can add users to the site collection administrators group through Site Actions -> Site Collection Administrators (and add users)

But I want to add users into admin group through code? I didn't get much idea about it.

Any help is appreciated.

Thanks.

share|improve this question
add comment

3 Answers 3

up vote 3 down vote accepted

There is a big difference with being site collection administrator. While the answer by MBsurf makes a user site admin, that will give him probably to much permissions. Using the code below you can retrieve the Owners group of a site and then add a user to that group:

// get the owners group based on permission
var ownerAssignment = (from a in site.RootWeb.RoleAssignments.OfType<SPRoleAssignment>() 
where a.RoleDefinitionBindings.OfType<SPRoleDefinition>().
Contains(site.RootWeb.RoleDefinitions.GetByType(SPRoleType.Administrator))
select a).FirstOrDefault();

// get the assignment's SharePoint group
var adminGroup = (SPGroup)ownerAssignment.Member;
// you can now add a user to this group
share|improve this answer
    
Agreed. Much of the time Site Collection admin rights are probably too much. However, Ankit requested the user be able to edit group membership. –  MBSurf Jun 7 '11 at 13:17
    
Thanks for your solution. I will try this and will update about my findings :) Thanks again.. :) –  Ankit Jun 7 '11 at 13:44
add comment

Check out this post.

Here is the example from the post:

//if the user is not a user on the site already
someSPWeb.AllUsers.Add(“domain\\user”,”user@domain.com”,”The User”,null);

SPUser spUser = someSPWeb.AllUsers["domain\\user"];

spUser.IsSiteAdmin = true;

spUser.Update();
share|improve this answer
    
Good answer, but please remember that external links tend to die. Since we plan to stay in business for quite a while, it would be great if you could extend your answer with the code sample in the external link. Thank you! –  Anders Rask Jun 7 '11 at 13:01
3  
You can replace the first two lines by : SPUser spUser = someSPWeb.EnsureUser("domain\\user"). –  Steve B Jun 7 '11 at 13:44
add comment

The below property is more effective to retrieve the site (SPWeb) owner group:

SPWeb.AssociatedOwnerGroup

Regards,

Vinicius

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.