Freigeben über


Office 365 - Creating a Subsite (Web) using CSOM in SharePoint Online

SharePoint Online has a number of PowerShell Cmdlets - https://technet.microsoft.com/en-us/library/fp161374(v=office.15).aspx, these Cmdlets include New-SPOSite which provides the ability to create a Site Collection, unfortunately it's not possible to create a SubSite (web) using these Cmdlets - it is however possible to use CSOM to do this and the script below demonstrates how to create a SubSite and a site beneath the SubSite (a SubSubSite?).

The script below is broken into three sections, the first section is used to connect to the SharePoint Online Site that you wish to create the Subsite within, simply update the highlighted $Site variable with the relevant URL.

The second section creates a SubSite within the Site Collection. Update the highlighted variables to match your requirements, I've used the Team Site template (STS#0) in this example.

The third section creates a Site beneath the SubSite that was just created, again update the highlighted variables to meet your requirements. The key thing to note when creating a SubSite beneath an existing SubSite is that you must include the relative path to the new SubSite within the $WCI.Url variable. In this case I first created a SubSite with the URL https://site.sharepoint.com/SubSite to create a SubSite beneath this I must specify the relative path of the SubSubSite to create therefore /SubSite/SubSubSite.

#Add references to SharePoint client assemblies and authenticate to Office 365 site
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Username = Read-Host -Prompt "Please enter your username"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Site = "https://site.sharepoint.com"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username,$Password)
$Context.Credentials = $Creds

#Create SubSite
$WCI = New-Object Microsoft.SharePoint.Client.WebCreationInformation
$WCI.WebTemplate = "STS#0"
$WCI.Description = "SubSite"
$WCI.Title = "SubSite"
$WCI.Url = "SubSite"
$WCI.Language = "1033"
$SubWeb = $Context.Web.Webs.Add($WCI)
$Context.ExecuteQuery()

#Create SubSubSite
$WCI = New-Object Microsoft.SharePoint.Client.WebCreationInformation
$WCI.WebTemplate = "STS#0"
$WCI.Description = "SubSubSite"
$WCI.Title = "SubSubSite"
$WCI.Url = "SubSite/SubSubSite"
$WCI.Language = "1033"
$SubWeb = $Context.Web.Webs.Add($WCI)
$Context.ExecuteQuery()

Brendan Griffin - @brendankarl

Steve Jeffery - @moss_sjeffery

Comments

  • Anonymous
    January 26, 2015
    HI Brendan,

    I have recently had to do this although on premise with 2010 and although possibly more to the script than what you have above I decided that I would close the connection to the top level site and then connect to the Subsite and then create the SubSubSite from there as i felt this was a little cleaner to implement and ensured that the SubSite was accessible.

    Regards
  • Anonymous
    April 20, 2015
    How can we create multiple subsites under a site collection using above code? Can we do it by importing a csv file?
    Please share.
  • Anonymous
    April 20, 2015
    Check this code out. It's a little less hard coded.

    Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
    Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Publishing.dll"
    Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
    $Site = Read-Host -Prompt "Please enter the target site collection URL"
    $Username = Read-Host -Prompt "Please enter your username"
    $Password = Read-Host -Prompt "Please enter your password" -AsSecureString
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
    $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username,$Password)
    $Context.Credentials = $Creds
    pause
    $WCI = New-Object Microsoft.SharePoint.Client.WebCreationInformation
    $WCI.WebTemplate = "STS#0"
    $WCI.Description = "SubSite"
    $WCI.Title = Read-Host -Prompt "Please enter subsite name"
    $WCI.Url = Read-Host -Prompt "Please enter subsite Url"
    $WCI.Language = "1033"
    $SubWeb = $Context.Web.Webs.Add($WCI)
    $Context.ExecuteQuery()
    $wshell = New-Object -ComObject Wscript.Shell
    $wshell.Popup("Subsite created successfully",0,"Done",0x1)
  • Anonymous
    October 02, 2015
    Can you create a subsite based off of a custom template in SPO? I've read that because templates are farm level, and SPO doesn't give us that level of access, the answer is no but I haven't given up yet.
  • Anonymous
    October 07, 2015
    Should be possible if the template has been added to the SC?
  • Anonymous
    June 01, 2016
    getting an error : Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (401) Unauthorized."for your info, I'm using free trial account . and while running this script I'm using global administrators credential.am I missing something? please let me know it will be really helpful for me.Thanks in advance,Regards,
    • Anonymous
      June 21, 2016
      You need to make sure you have Site Collection admin permissions to the site too. Are you running this within an elevated PowerShell console too?
  • Anonymous
    December 02, 2016
    Is there somewhere that the steps of implementing this are listed?I'm not an expert in using PowerShell.This looks like it is referencing local C: Drive dll's but this is for SharePoint Online right?That's part of where I'm confused.Can you provide more descriptive steps or point me to somewhere already documented?Thanks!Sam