Geeks With Blogs
Reidar Husmo SharePoint from the trenches

It is frequently useful to create / delete web applications in a development environment.

If you need to create a structure, this can quickly become tedious.

Enter Powershell, xml and recursive functions.

Create the structure in xml. Something like:

<Sites>
    <Site Name="Test 1" Url="Test1" />
    <Site Name="Test 2" Url="Test2" >
        <Site Name="Test 2 1" Url="Test21" >
            <Site Name="Test 2 1 1" Url="Test211" />
            <Site Name="Test 2 1 2" Url="Test212" />
        </Site>
    </Site>
    <Site Name="Test 3" Url="Test3" >
        <Site Name="Test 3 1" Url="Test31" />
        <Site Name="Test 3 2" Url="Test32" />
        <Site Name="Test 3 3" Url="Test33" >
            <Site Name="Test 3 3 1" Url="Test331" />
            <Site Name="Test 3 3 2" Url="Test332" />
        </Site>
        <Site Name="Test 3 4" Url="Test34" />
    </Site>
</Sites>

Read this structure in Powershell, and recursively create the sites. Oh, and have cool progress dialogs, too.

$snap = Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.SharePoint.Powershell" }
if ($snap -eq $null)
{
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
}

function CreateSites($baseUrl, $sites, [int]$progressid)
{
    $sitecount = $sites.ChildNodes.Count
    $counter = 0
    foreach ($site in $sites.Site)
    {
        Write-Progress -ID $progressid -Activity "Creating sites" -status "Creating $($site.Name)" -percentComplete ($counter / $sitecount*100)
        $counter = $counter + 1

        Write-Host "Creating $($site.Name) $($baseUrl)/$($site.Url)"
        New-SPWeb -Url "$($baseUrl)/$($site.Url)" -AddToQuickLaunch:$false -AddToTopNav:$false -Confirm:$false -Name "$($site.Name)" -Template "STS#0" -UseParentTopNav:$true
        if ($site.ChildNodes.Count -gt 0)
        {
            CreateSites "$($baseUrl)/$($site.Url)" $site ($progressid +1)
        }
        Write-Progress -ID $progressid -Activity "Creating sites" -status "Creating $($site.Name)" -Completed
    }
}

# read an xml file
$xml = [xml](Get-Content "C:\Projects\Powershell\sites.xml")
$xml.PreserveWhitespace = $false

CreateSites "http://$($env:computername)" $xml.Sites 1

Easy!

Sensible real life implementations will also include templateid in the xml, will check for existence of a site before creating it, etc.

Posted on Thursday, April 12, 2012 2:53 PM SharePoint 2010 , Powershell | Back to top


Comments on this post: Creating SharePoint sites from xml using Powershell

# re: Creating SharePoint sites from xml using Powershell
Requesting Gravatar...
Microsoft Sharepoint has opened a new era in the world of web content mangement and docoment management. It increases the central control efficiency. Really in enhances the team work efficiency. You nicely present how to set this for the best result
sharepoint Tools
Left by John Scarbrough on Jun 07, 2012 11:47 AM

# re: Creating SharePoint sites from xml using Powershell
Requesting Gravatar...
Thank You for an awesome post, It´s really helpful!

I´ve build up my XML site structure, kinda like yours. But when I launch my script it gets stuck and freezes..Do you know what could be the problem? What am I Doing wrong?

thanks in advance :)

/ E
Left by SPEngine on Sep 12, 2013 1:57 PM

Your comment:
 (will show your gravatar)


Copyright © Norgean | Powered by: GeeksWithBlogs.net