Skip to content

Adding more than two site collection administrators programmatically.

October 25, 2007

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

Advertisement
11 Comments leave one →
  1. Harry Solsem permalink
    December 13, 2008 6:01 pm

    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. Bilal Iqbal permalink
    February 11, 2009 8:45 pm

    Thanx, it works and give no error at all.

  3. Dave permalink
    August 27, 2009 7:11 am

    Can this be achieved without code? i.e. via stsadm command line?

  4. August 27, 2009 7:32 pm

    The purpose of this was for software I was writing at the time.
    STSADM -o siteowner only supports adding a primary and secondary owner (Site collection admin)

  5. Bala permalink
    January 22, 2010 5:29 pm

    Thank you very much.

    i was hitting my head with SpWeb.SiteAdministrators.Add()

  6. February 10, 2010 5:26 pm

    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.

    • Bill permalink
      April 17, 2011 1:06 am

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

  7. September 15, 2011 8:40 pm

    Its Great helped me also,Thanks for the post

Trackbacks

  1. SharePoint: Useability Links | Alpesh Nakars' Blogosphere
  2. Adding site collection administrators programmatically « Devblog of Bart Snyckers
  3. Adding or removing a Site Collection Administrator from all Site Collections « The SharePoint Swiss Army Knife

Leave a Reply

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

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

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

Facebook photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.