Friday 30 August 2013

Programmatically set a user as the Site Collection Administrator in SharePoint

tring 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