Adding more than two site collection administrators programmatically.

https://embed.spotify.com/follow/1/?uri=spotify:artist:4KfwTCNqT3gXele8WP3s5b&size=detail&theme=light

The SiteAdministrators collection of the SPWeb object returns a SPUserCollection of all the Site Collection Administrators for the site in question.

Unfortunately, it doesn’t appear to actually allow you to “ADD” new users to the Site Collection Administrators via this collection like you can do with any other SPUserCollection for a user.

Whether it’s a bug, or not, a simple way around this is to do the following:

  1. Add the user to the site collection users list
  2. Retrieve the user you just added
  3. Set the IsSiteAdmin property for the user to true.
  4. Call SPUser.Update() for the user.

Sample code:

someSPWeb.AllUsers.Add(“domain\\user”,”user@domain.com”,”The User”,null);

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

spUser.IsSiteAdmin = true;

spUser.Update();

Hope this helps

– Keith

12 Replies to “Adding more than two site collection administrators programmatically.”

  1. Hi.

    Thanks for this short but very usefull peace of code.

    Any idea why the spuser.update would throw an exception but the update is still completed?

  2. My sharepoint experience is limited but I can follow clear instructions.

    Can someone explain step by step how to do this without assuming any knowledge of sharepoint objects etc?

    Thanks.

    1. this is a Great Post. I found it quickly and did not have to waste too much time trying to debug siteadministrators.add. Thanks!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s