Simon Sebright

Simon Sebright
08.02.2012
0 Kommentar(e)

If you have features or look and feel which can be applied to sub-sites, then you will want to make sure that they are compatible with all the relevant site templates available. This means creating a suitable sub-site for each site template, a task ideally suited for scripting.

Powershell Snapin and the Commandlets

Using the snapin for SharePoint 2010 (preloaded when you use the SharePoint Management Shell), we can base our approach on two key commandlets.  First of all, to create a sub-site, we use New-SPWeb.  An example call is shown here:

$web = New-SPWeb -Template STS#0 -Name MySubSite http://MyWebApp/Sites/MySiteCollection/MySubSite -AddToQuickLaunch

For a particular list of site templates, we can implement an array of name/title pairs, and iterate over them:
 

$siteDetails = (("STS#0", "STSNum0"),

("STS#1", "STSNum1"),

("STS#2", "STSNum2"),

("MPS#0", "MPSNum0"),

("MPS#1", "MPSNum1"),

("MPS#2", "MPSNum2"),

("MPS#3", "MPSNum3"),

("MPS#4", "MPSNum4"))
 

function Create-Sites($root)

{

      $site = New-Object Microsoft.SharePoint.SPSite ($root)

       Write-Host "Processing site: " $site.Url

 

      $siteDetails | foreach {

            Write-Host $_[0]

            $web = New-SPWeb -Template $_[0] -Name $_[1]  -Url $($root + "/" + $_[1]) -AddToQuickLaunch

      }

 

      $site.Dispose()

}

 This of course relies on us knowing the names of the site templates and choosing a suitable name/url for the sites.  The # character is not allowed in urls, so we have to change that – note the use of “Num” in the above array.  This can be easily done in bulk with a text editor using the regular expression:
 

 {.*}#{.*}
 

And a replace expression:
 

("\1#\2", "\1Num\2"),
 

Now for the second commandlet: Get-SPWebTemplate. If we just call that in the SharePoint Management Shell, we get a list of all the templates with Name, Title, etc.:

Get-SPWebTemplate results in Command Window

Note that the results are per language – so it appears at first as though there are duplicates.  We can pipe these results into a foreach loop to process each in turn:

function Create-SubSites($root)

{

      $site = New-Object Microsoft.SharePoint.SPSite ($root)

     

      Write-Host "Processing site: " $site.Url

           

      Get-SPWebTemplate | foreach {

            Write-Host $_.Name

            $web = New-SPWeb -Template $_.Name -Name $_.Title $($root + "/" + $_.Title) -AddToQuickLaunch

      }

     

      $site.Dispose()

}

 

This will fail for a couple of templates because the Title is not always suitable for a well-formed url, and also there are duplicates.  In order to get round this, a find/replace on the # in Name will suffice.

Also note that certain templates will require that you site collection has certain services and features available (for example, the Access-based templates (ACCSRV#0, etc.)).

Cleaning up and Re-running the Script

You may wish to run this script again on the same site collection, in which case it will fail because the sites already exist.  This means you will have to delete all your subsites first.  This is slightly complicated by the fact that some templates create sub-sub-sites (e.g. Collaboration Portal, Publishing Portal).  A simple recursion is listed below, this could be done to any required depth with a proper recursive function.

 

function Clean-SubSites($root)

{

      $site = New-Object Microsoft.SharePoint.SPSite ($root)

     

      Write-Host "Cleaning site: " $site.Url

     

      $site.Rootweb.Webs | foreach {

            Write-Host "Deleting" $_.Url

            $_.Webs | foreach {$_.Delete()}

            $_.Delete()

            $_.Dispose()

      }

     

      $site.Dispose()

     

}

Putting it all Together

Let’s put that all together in a script which takes a parameter – the url of your site collection:

param($url)


$snapin = Get-PSSnapin -Name Microsoft.SharePoint.PowerShell

if($snapin -eq $null) { Add-PSSnapin Microsoft.SharePoint.PowerShell }


function Clean-SubSites($root)

{

      $site = New-Object Microsoft.SharePoint.SPSite ($root)

      Write-Host "Cleaning site: " $site.Url

     

      $site.Rootweb.Webs | foreach {

            Write-Host "Deleting" $_.Url

            $_.Webs | foreach {$_.Delete()}

            $_.Delete()

            $_.Dispose()

      }

     

      $site.Dispose()

     

}

 

function Create-SubSites($root)

{

      $site = New-Object Microsoft.SharePoint.SPSite ($root)

      Write-Host "Processing site: " $site.Url

      Get-SPWebTemplate | foreach {

            Write-Host $_.Name

            $web = New-SPWeb -Template $_.Name -Name $_.Title $($root + "/" + $_.Name.Replace("#", "Num")) -AddToQuickLaunch

      }

     

      $site.Dispose()

}

Clean-SubSites($url)

Create-SubSites($url)


Enjoy!

Blog-Archiv

Juli 2014 (6)
Juni 2014 (2)
Mai 2014 (2)


Das könnte Sie auch interessieren:
Offene Stellen

Kommentar hinterlassen




= three + seven

Kommentar(e)

Noch keine Kommentare.