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

You can add users to the site collection administrators group through
Site Actions ->Site Collection Administrators (and add users)

But how can you do this through code? What is the grpName?

web.EnsureUser(User.LoginName);
web.SiteGroups[grpName].AddUser(@User.LoginName, string.Empty, LookupValue, string.Empty);

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You need to get the SPUser object for a user in a Site Collection and update the IsSiteAdmin property.

function makeUserSiteCollectionAdmin(SPUser user)
{
    user.IsSiteAdmin = true;
    user.Update();
}
share|improve this answer
    
Worth noting that the account running the code (i.e. if in a webpart) needs permissions to make this change so a SPSecurity.RunWithElevatedPrivileges block might be required. –  Neil Richards Feb 4 '11 at 12:00
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.