1

Trying to create multiple things in PowerShell with CSOM for SharePoint Online:

  1. Create a project (subsite) in the site collection (root)
  2. Create a level (subsite) underneath the project subsite (so basically a subsite under an existing subsite).

I used this code to create a subsite in the root:

#Specify tenant admin and site URL
$Username = "admin@contoso.onmicrosoft.com"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Site = "https://contoso.sharepoint.com/sites/projects"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username,$Password)
$Context.Credentials = $Creds

#Search Subsite Templates
#$Templates = $Context.Site.GetWebTemplates("1043","0")
#$Context.Load($Templates)
#$Context.ExecuteQuery()
#$Templates | Where {$_.Name -like "*STS#0*" } | Select Name, Description

#Main / project

#Load CSV
$dataSource = import-csv “C:\temp\contoso.csv”

foreach($dataRecord in $dataSource)
{ 
    #Define properties
$projectnummer = $dataRecord.Projectnummer
$project = $dataRecord.Project

#check if subsite is present or not
$web = $Context.Web 
$Context.Load($web.Webs)   
$Context.ExecuteQuery()

$subweb = $web.Webs | where{$_.Url -like "*$projectnummer*"}
if($subweb)
{
    #Execute if subsite exists
    Write-Host "Project $projectnummer exists" -ForegroundColor Green
    continue
}
else
{
    #Execute if subsite does not exists
    Write-Host "Project $projectnummer does not exists yet, creating..." -ForegroundColor Green

    #Create Subsite in root
    $Subsite = New-Object Microsoft.SharePoint.Client.WebCreationInformation
    $Subsite.WebTemplate = "STS#0"
    $Subsite.Title = "$project"
    $Subsite.Description = "$projectnummer"
    $Subsite.Url = $projectnummer
    $Subsite.Language = "1043"
    $SubWeb = $Context.Web.Webs.Add($Subsite)
    $Context.Load($SubWeb)
    $Context.ExecuteQuery()
}
}

This is working, but now I want to create a subsite in the subsite that is just created, but how? I changed the $site value with this, but that did not work: $Site = $Site + "/" + $projectnummer + "/"

In that subsite, I also need to create multiple document libraries, but I think I can do that as long as I can create the correct subsite at the right location.

  • Reloading the client context with the new url should work. Are you getting an error for this? – Tonyp543212003 May 31 '17 at 10:24
0

I tested this and it worked for me:

function GetClientContext($url, $user, $password) {
 $context = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
 $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user, $password) 
 $context.Credentials = $credentials
 return $context
}

#variables
#top level site
$WebUrl = "https://test.sharepoint.com"
#subsite name/url
$newWebURL = "subsite"
#subsite of subsite url
$SubSubsiteUrl = "subsite2"

#Create subsite
$context = GetClientContext $WebUrl $User $Pass
#create site here
$Subsite = New-Object Microsoft.SharePoint.Client.WebCreationInformation
$Subsite.WebTemplate = "STS#0"
$Subsite.Title = "$newWebURL"
$Subsite.Url = "$newWebURL"
$SubWeb = $context.Web.Webs.Add($Subsite)
$context.Load($SubWeb)
Try{
    $context.ExecuteQuery()
    LogWrite "Success creating site for $newWebURL"
}
Catch{
    LogWrite "Error creating subsite for $newWebURL $_.Exception.Message"
}

#Create subsite of subsite
$newWebFullURL = $WebUrl + "/" + $newWebURL
$context = GetClientContext $newWebFullURL $User $Pass
#create site here
$Subsite = New-Object Microsoft.SharePoint.Client.WebCreationInformation
$Subsite.WebTemplate = "STS#0"
$Subsite.Title = "$SubSubsiteUrl"
$Subsite.Url = "$SubSubsiteUrl"
$SubWeb = $context.Web.Webs.Add($Subsite)
$context.Load($SubWeb)
Try{
    $context.ExecuteQuery()
    LogWrite "Success creating site"
}
Catch{
    LogWrite "Error creating site $_.Exception.Message"
}

This is a basic example but you could feed in the site structure from XML or JSON file and then loop the script to reuse the code.

Hope this helps

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.