Krichie – That SharePoint Guy

October 25, 2007

Adding more than two site collection administrators programmatically.

Filed under: SharePoint — Keith Richie @ 3:10 am

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

8 Comments »

  1. [...] Adding more than two site collection administrators programmatically. [...]

    Pingback by SharePoint: Useability Links | Alpesh Nakars' Blogosphere — October 26, 2007 @ 10:26 am

  2. 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?

    Comment by Harry Solsem — December 13, 2008 @ 6:01 pm

  3. Thanx, it works and give no error at all.

    Comment by Bilal Iqbal — February 11, 2009 @ 8:45 pm

  4. [...] and add multiple Site Collection Administrators. I searched the internet offcourse and found a solution by Keith Richie. You can merge step 1 and 2 by using the EnsureUser method provided by the [...]

    Pingback by Adding site collection administrators programmatically « Devblog of Bart Snyckers — March 18, 2009 @ 5:58 pm

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

    Comment by Dave — August 27, 2009 @ 7:11 am

  6. 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)

    Comment by Keith Richie — August 27, 2009 @ 7:32 pm

  7. Thank you very much.

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

    Comment by Bala — January 22, 2010 @ 5:29 pm

  8. 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.

    Comment by Mo — February 10, 2010 @ 5:26 pm


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.