Emad GabrielThoughts, tips and shortcuts about building software
SharePoint

Creating SharePoint online Site Structure from a CSV using PowerShell

Here is a simple script to create the site structure for SharePoint Online based on a csv file.

The script is using the Client-side SharePoint PowerShell project from Codeplex (https://sharepointpowershell.codeplex.com/ (archived copy) ).

 

In order to create the site structure just fill the csv file of the following format

Url,Name,Template,Description,Language Blog,Blog,BLOG#0,Blog,1033 Subsite1, SubSite1, STS#0, Name1, 1033 SubSite1/Subsite11, Name11, STS#0, Description, 1033 SubSite1/Subsite11/Subsite1111, Name3, STS#0, Description, 1033

 

And then use the following script

 

Import-Module "$PSScriptRoot\spps.psm1" Import-Module "$PSScriptRoot\spps.subsites.psm1" Import-Module "$PSScriptRoot\ConfigCredentials.ps1"

Initialize-SPPS -siteURL $O365SiteUrl -isOnline $true -onlineusername $username -onlinepassword $password

Import-Csv $PSScriptRoot\SiteStructure.csv  | ForEach-Object{

    $name = $_.Name     $template = $_.Template     $description = $_.Description     $lang = $_.Language         Write-Host "Processing " $name " "  $_.Url     Open-Rootsite         $relUrl = ""        foreach($subsite in $_.Url -split "/")     {         $relUrl += "/"+ $subsite         try         {             Open-Subsite -relativeUrl $relUrl             Write-Host "Site" [$relUrl] "Already exists"         }         catch         {             Write-Host "Trying to create" $subsite             Add-Subsite -title $name -url $subsite -webTemplate $template -language $lang -description $description         }     }     Write-Host " ----------------------- " }

 

Note, the Script ConfigCredentials.ps1 is used to store the Site Url and the credentials as follows

$O365SiteUrl = “https://url_here” $username = username@domain.com $password = "password"

 

Note: to make sure the script behaves as expected, make sure that the hierarchy in the csv file is in order. i.e.

Subsite1 Subsite1/Subsite2

in other words do no list the sub site before its parent site.

Office365PowerShellSharePointOnline