MOSS MVP

I've moved my blog to http://blog.falchionconsulting.com!. Please update your links. This blog is no longer in use--you can find all posts and comments at my new blog; I will no longer be posting to this site and comments have been disabled.

Thursday, April 30, 2009

Creating Default Site Groups After Creating Site Collections Using STSADM

I got an email from Jennifer Davis today asking why, when she ran my gl-createsiteindb command, did the default site groups not show up in the site collection, specifically the “<site name> Members”, “<site name> Owners”, and “<site name> Visitors” groups.  Upon digging further she realized that this behavior was not limited to my command as the out-of-the-box createsite and createsiteinnewdb commands exhibited the same behavior.

Basically what’s happening is that if you create the site collection via the browser an additional method call gets made on the root web site of the site collection: SPWeb.CreateDefaultAssociatedGroups.  For whatever reason this method call is not made when using STSADM and as my gl-createsiteindb command just mimics the createsite command I too did not make the necessary method call.  Well, I agree with Jennifer that this is just wrong so I decided to go ahead and modify my code so that those default site groups would get created.  Fortunately it was a really simple change - here’s the code that I added to the command:

   1: if (!string.IsNullOrEmpty(webTemplate))
   2: {
   3:     using (SPWeb web = site.RootWeb)
   4:     {
   5:         web.CreateDefaultAssociatedGroups(ownerLogin, secondaryContactLogin, string.Empty);
   6:     }
   7: }

If you’re using any of the existing out of the box commands you can easily achieve the same end result with a couple lines of PowerShell, as the following demonstrates (requires my custom cmdlets):

$url = "http://<site url>"
$primaryOwner = "domain\user"
$secondaryOwner = "domain\user"
$site = Get-SPSite $url
$site.SPBase.RootWeb.CreateDefaultAssociatedGroups($primaryOwner, $secondaryOwner, "")
$site.SPBase.Dispose()

1 comment:

tripwire said...

Hi Gary, to make this even more useful it would be great if you could provide an optional flag to specify the comma-delimited names for the groups.

e.g. stsadm -o gl-createsiteindb -groups '[site] owners,[site] members,[site] visitors'

I find that having them auto-generated using the sitename can often lead to unwieldy titles.