Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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!"
- }
- }
- #Add references to SharePoint client assemblies and authenticate to Office 365 site
- Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll"
- Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Publishing.dll"
- Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement