Wednesday, January 5, 2011

Adding site collection administrators via Powershell

I was setting up a SharePoint server using Powershell scripts. Since my team manages the server I wanted to add them all as Site Collection Administrators on the top site, My Site and the Search Center. This turned out to be fairly straightforward but searching for a reference proved quite frustrating.

What I wanted to do is to use Powershell to do the equivalent of going to Site Actions->Site Settings,
Users and Permissions: Site collection administrators and then adding my team members to that list. If you are following along with your installation you will see that it does indeed call these users "Site Collection Administrators". However, do any search and you will come across articles such as "Add or remove site collection administrators (SharePoint Server 2010)" or "Add or remove site collection administrators (SharePoint Foundation 2010)" both of which clearly state "A site collection can only have two administrators ...".

Major head scratching commences. Lots of searching, swearing ...

Then I figured why not look at how the web UI does it. You will notice that the page that manages this is /_layouts/mngsiteadmin.aspx. Looking carefully I couldn't help but wonder why it's called Manage Site Admin. Subtle, but it's a clue.

Doing some searching and poking around I found that the SPWeb object has a SiteAdministrators property which as an Add() method. So, I tried calling SiteAdministrators.Add() with a user reference. There was no error, but nothing happened. So, I did more searching and eventually I came across this post: Adding more than two site collection administrators programmatically. Bingo!

Here's my code:
$sp_web = Get-SPWeb <URL>
$sp_web.AllUsers.Add("<username-1>", "<email-1>", "<name-1>",  "<notes-1>")
$sp_web.AllUsers.Add("<username-2>", "<email-2>", "<name-2>",  "<notes-2>")
...
$sp_web.AllUsers | where { "<username-1>", "<username-2>", ... -contains $_.UserLogin } | foreach { $_.IsSiteAdmin = "True"; $_.Update() }

Here's the lesson learned. What Microsoft has been calling Site Collection Administrators really has 2 meanings:
  1. Site Owners and Secondary Owners
  2. Site Administratrators
These are quite different and a good post that describes it is: Difference between Site Administrators and Site Collection Administrators and Site Collection Owners.


So clear now! What I was really trying to do is to add Site Administrators.

No comments:

Post a Comment