Tuesday, January 8, 2013

Programmatically set a user as the Site Collection Administrator in SharePoint

In this post we will discuss about how to set a user as the Site Collection Administrator in SharePoint programmatically.

You can also check my previous articles on ECMA Client Object Model (SP.js) in SharePoint 2010, Get URL value SharePoint hyperlink field and Client Object Model SharePoint 2010.

string siteUrl = "http://Site URL";
string strUserName = "DomainName/UserName";
SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(siteUrl))
                {
                    using (SPWeb web = site.RootWeb)
                    {
                        web.AllowUnsafeUpdates = true;
                        SPUser spUser = web.EnsureUser(strUserName);
                        spUser.IsSiteAdmin = true;
                        spUser.Update();
                        web.AllowUnsafeUpdates = false;
                    }
                }
            });

This will set the user as an admin for the SharePoint site.

No comments:

Post a Comment