1

I am trying to automate the creation of multiple sub-sites in a site collection. Which command will do this? I am using SharePoint online 2013. The site collection is already created so now I'm trying to automate the sub-site creation, and creating the unique security groups for each sub-site. This routine has to be recreated every semester.

1

You can now do this with one PNPPowerShell command,

New-PnPWeb

| improve this answer | |
0

There isn't one in the SPO Management shell, you have to roll your own using CSOM. Here is an example of ours that I created to use in our provisioning process. It has a bunch of other custom commandlets in it to handle some specific needs we had.

Function New-SPOSubsite {
    <#
        .SYNOPSIS
        Creates a new subsite under the specified site with the specified parameters.
        .DESCRIPTION    
        Creates a new subsite under the specified site with the specified parameters.
        .PARAMETER SiteUrl
        The URL of the site where the subsite belongs, can be a root level site collection or a subsite.
        .PARAMETER SubsiteUrl
        The short URL of the new subsite.
        .PARAMETER Title
        The subsite title.
        .PARAMETER Template
        Template is an enumeration, your choices are either Project or Team.
        .PARAMETER SamePermissions
        A boolean parameter that specifies if the subsite is going to use the same permissions as the parent. $false=broken, $true=inherit
        .PARAMETER Description
        An optional site description.
        .EXAMPLE
        Create a new project subsite from the root of a site collection with inherited permissions
        New-OISubsite -SiteUrl "https://tenant.sharepoint.com/teams/eric" -SubsiteUrl "ProjectX" -Title "Secret Project X" -Template Project -SamePermissions $true -Description "This is a project space"
        .EXAMPLE
        Create a new team subsite with broken permissions inheritance
        New-OISubsite -SiteUrl "https://tenant.sharepoint.com/teams/eric/operations" -SubsiteUrl "DevOps" -Title "DevOps Team Site" -Template Team -SamePermissions $false
        .NOTES
        Mainly used as part of New-Plant provisioning, but could be used to create a subsite in any site collection.
        Created since there is no New-SPOWeb commandlet.
    #>
    Param(
        [Parameter(Mandatory=$true,HelpMessage="The URL of the site collection",Position=0)][ValidateNotNull()]
        [string]$SiteUrl,

        [Parameter(Mandatory=$true,HelpMessage="The short URL of the subsite",Position=1)][ValidateNotNull()]
        [string]$SubsiteUrl,

        [Parameter(Mandatory=$true,HelpMessage="The site title",Position=2)][ValidateNotNull()]
        [string]$Title,

        [Parameter(Mandatory=$true,HelpMessage="The template to use, Project or Team",Position=3)][ValidateSet("Project","Team")]
        [string]$Template,

        [Parameter(Mandatory=$true,HelpMessage="Use Same Permissions As Parent Site",Position=4)]
        [boolean]$SamePermissions,

        [Parameter(Mandatory=$false,HelpMessage="Site description",Position=5)]
        [string]$Description

    )
    begin{
        $tmplt = ""

        If($Template -eq "Team"){
            $tmplt = "STS#0"
        }
        Else {
            $tmplt = "PROJECTSITE#0"
        }

        $subsite = $SiteUrl+"/"+$SubsiteUrl
        write-host "Creating subsite $subsite"
    }
    process{
        $context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
        $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)

        #Create SubSite
        $wci = New-Object Microsoft.SharePoint.Client.WebCreationInformation
        $wci.WebTemplate = $tmplt
        if ($Description.Length -gt 0){
            $wci.Description = $Description
        }
        $wci.UseSamePermissionsAsParentSite = $SamePermissions
        $wci.Title = $Title
        $wci.Url = $SubsiteUrl
        $wci.Language = "1033"

        $SubWeb = $context.Web.Webs.Add($wci)
        $context.ExecuteQuery()

        if($SamePermissions -eq $false){
            try{
                #Check to see if this is a root site collection that was passed
                Get-SPOSite -Identity $SiteUrl
            }
            catch {
               #If here we are working with a subsite
               Write-Host "Oops, it looks like we're dealing with a subsite, I can handle that" -ForegroundColor Yellow 
               #reconstruct the SiteUrl parameter
               $SiteUrl = "https://tenant.sharepoint.com/"+$SiteUrl.Split("/")[3]+"/"+$SiteUrl.Split("/")[4]
            }
            finally{
                $ownerGroup = "$Title Owners"
                $memberGroup = "$Title Members"
                $visitorGroup = "$Title Visitors"
                Write-Host "Hang tight, creating groups, updating ownership and adjusting permissions"
                New-SPOSiteGroup -Site $SiteUrl -Group $ownerGroup -PermissionLevels "Read" | Out-Null
                #Updating the group owner does not work from PowerShell, using the new Set-GroupOwner function
                Set-GroupOwner -SiteUrl $SiteUrl -GroupToUpdate $ownerGroup.ToString() -GroupOwner $ownerGroup.ToString()
                New-SPOSiteGroup -Site $SiteUrl -Group $memberGroup -PermissionLevels "Read" | Out-Null
                Set-GroupOwner -SiteUrl $SiteUrl -GroupToUpdate $memberGroup.ToString() -GroupOwner $ownerGroup.ToString()
                New-SPOSiteGroup -Site $SiteUrl -Group $visitorGroup -PermissionLevels "Read" | Out-Null
                Set-GroupOwner -SiteUrl $SiteUrl -GroupToUpdate $visitorGroup.ToString() -GroupOwner $ownerGroup.ToString()
                Set-PermissionsOnSite -Url $subsite -GroupName $ownerGroup -Roletype "Full Control" 
                Set-PermissionsOnSite -Url $subsite -GroupName $memberGroup -Roletype "Contribute"  
                Set-PermissionsOnSite -Url $subsite -GroupName $visitorGroup -Roletype "Read"  
                Set-DefaultSiteGroups -SiteUrl $subsite -VisitorGroup $visitorGroup -MemberGroup $memberGroup -OwnerGroup $ownerGroup 
            }
        }
    }
    end {
        $context.Dispose()
        Write-Host -ForegroundColor Green "Subsite $subsite created successfully!"
    }
}
| improve this answer | |
  • Thank you for sharing this. I've been taking PowerShell training classes so some of this is starting to look familiar. Hopefully between the two posts, I can figure it out. – Sandra Boyd Mar 9 '16 at 16:52
0

Here is combination of CSOM and Powershell.

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.

#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()

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

| improve this answer | |
  • Thanks for sharing this. I'm new to PowerShell and I need all the help I can get. – Sandra Boyd Mar 9 '16 at 16:51
  • This worked wonderful. I didn't have a folder "16" but I had one for "15" so I adjusted the script accordingly. It worked anyway but are there some updates that I need to have applied to the server and will I be missing any functionality for PowerShell commands if I don't have any updates installed? – Sandra Boyd Mar 9 '16 at 18:24

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.