SharePoint Online: How to Create a Subsite using PowerShell?

Requirement: Create a Subsite in SharePoint Online using PowerShell.

How to Create a Subsite in SharePoint Online?
Subsites in SharePoint Online can be created within any existing root site or subsite. By default, site owners (or users with Full control or Manage Hierarchy) have the permissions to create subsites. To add a subsite in SharePoint online, follow these steps:
  • Sign-in to your SharePoint Online site as Site Collection Administrator/Site Owner >> Navigate to the site under which the new subsite is to be created.
  • Click on Site Settings Gear Icon >> Choose "Site Contents" (or Click on the Site Content link from the Quick Launch)
    sharepoint online powershell create subsite
  • Scroll down to the bottom and click on "Create Subsite" link
    sharepoint online powershell create web
  • Provide the Name, description, URL and template for your new subsite. Specify other optional settings such as Permissions, Navigation.
    how to create subsite in sharepoint online
  • Click on "Create" button at the bottom of the page to create a subsite in SharePoint Online.
It's the best practice to exclude special characters and spaces from the URL of the site! E.g. If your site title is Human Resources, the URL name should be either HumanResources or HR.

Once the subsite is created, you'll be taken to the home page of the new subsite. Let's see the SharePoint Online PowerShell to create subsite.

SharePoint Online: PowerShell to Create Subsite:
Let's create a subsite in SharePoint online using PowerShell CSOM.
#Load SharePoint Online Assemblies
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.Runtime.dll"

#Variables for processing
$SiteURL = "https://crescent.sharepoint.com/Sites/Sales"
$AdminAccount = "SPAdmin@crescent.com"
 
#User Names Password to connect 
$Password = Read-host -assecurestring "Enter Password for $AdminAccount" 
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminAccount, $Password

Try {
    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Context.Credentials = $credentials

    #Specify Subsite details
    $WebCI = New-Object Microsoft.SharePoint.Client.WebCreationInformation
    $WebCI.Title = "Sales - US "
    $WebCI.WebTemplate = "STS#0" #Team Site
    $WebCI.Url = "us"
    $SubWeb = $Context.Web.Webs.Add($WebCI)
    $Context.ExecuteQuery()

    Write-host "Subsite Created Successfully!" -ForegroundColor Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
This script creates a web in SharePoint Online. The above script uses only mandatory parameters. You can refer this Technet link if you want to specify other parameters such as Permission, navigation settings, etc. https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.client.webcreationinformation_members.aspx

Create Subsite in SharePoint Online using PnP PowerShell
To create a new subsite in the current site collection, use: New-PnPWeb cmdlet. Specify the parameters as below:
#Config Variables
$SiteCollURL = "https://crescenttech.sharepoint.com"
$SiteTitle = "Town Hall 2017"
$SiteURL = "townhall2017"
$Description = "Town Hall 2017 site - Find all Videos, Presentations, Demos Shared here!"
$Locale = 1033 #English
$Template = "STS#0" #Team site

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PNP Online
    Connect-PnPOnline -Url $SiteCollURL -Credentials $Cred

    #Create New subsite
    New-PnPWeb -Title $SiteTitle -Url $SiteURL -Description $Description -Locale $Locale -Template $Template -ErrorAction Stop
    Write-host "Site '$SiteTitle' Created Successfully!" -f Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
Additionally, The New-PnPWeb cmdlet supports -BreakInheritance, -InheritNavigation switches.
SharePoint Online: How to Create a Subsite using PowerShell? SharePoint Online: How to Create a Subsite using PowerShell? Reviewed by Salaudeen Rajack on May 31, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.