Advertisement
Guest User

Untitled

a guest
Mar 9th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. Function New-SPOSubsite {
  2. <#
  3. .SYNOPSIS
  4. Creates a new subsite under the specified site with the specified parameters.
  5. .DESCRIPTION
  6. Creates a new subsite under the specified site with the specified parameters.
  7. .PARAMETER SiteUrl
  8. The URL of the site where the subsite belongs, can be a root level site collection or a subsite.
  9. .PARAMETER SubsiteUrl
  10. The short URL of the new subsite.
  11. .PARAMETER Title
  12. The subsite title.
  13. .PARAMETER Template
  14. Template is an enumeration, your choices are either Project or Team.
  15. .PARAMETER SamePermissions
  16. A boolean parameter that specifies if the subsite is going to use the same permissions as the parent. $false=broken, $true=inherit
  17. .PARAMETER Description
  18. An optional site description.
  19. .EXAMPLE
  20. Create a new project subsite from the root of a site collection with inherited permissions
  21. 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"
  22. .EXAMPLE
  23. Create a new team subsite with broken permissions inheritance
  24. New-OISubsite -SiteUrl "https://tenant.sharepoint.com/teams/eric/operations" -SubsiteUrl "DevOps" -Title "DevOps Team Site" -Template Team -SamePermissions $false
  25. .NOTES
  26. Mainly used as part of New-Plant provisioning, but could be used to create a subsite in any site collection.
  27. Created since there is no New-SPOWeb commandlet.
  28. #>
  29. Param(
  30. [Parameter(Mandatory=$true,HelpMessage="The URL of the site collection",Position=0)][ValidateNotNull()]
  31. [string]$SiteUrl,
  32.  
  33. [Parameter(Mandatory=$true,HelpMessage="The short URL of the subsite",Position=1)][ValidateNotNull()]
  34. [string]$SubsiteUrl,
  35.  
  36. [Parameter(Mandatory=$true,HelpMessage="The site title",Position=2)][ValidateNotNull()]
  37. [string]$Title,
  38.  
  39. [Parameter(Mandatory=$true,HelpMessage="The template to use, Project or Team",Position=3)][ValidateSet("Project","Team")]
  40. [string]$Template,
  41.  
  42. [Parameter(Mandatory=$true,HelpMessage="Use Same Permissions As Parent Site",Position=4)]
  43. [boolean]$SamePermissions,
  44.  
  45. [Parameter(Mandatory=$false,HelpMessage="Site description",Position=5)]
  46. [string]$Description
  47.  
  48. )
  49. begin{
  50. $tmplt = ""
  51.  
  52. If($Template -eq "Team"){
  53. $tmplt = "STS#0"
  54. }
  55. Else {
  56. $tmplt = "PROJECTSITE#0"
  57. }
  58.  
  59. $subsite = $SiteUrl+"/"+$SubsiteUrl
  60. write-host "Creating subsite $subsite"
  61. }
  62. process{
  63. $context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
  64. $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)
  65.  
  66. #Create SubSite
  67. $wci = New-Object Microsoft.SharePoint.Client.WebCreationInformation
  68. $wci.WebTemplate = $tmplt
  69. if ($Description.Length -gt 0){
  70. $wci.Description = $Description
  71. }
  72. $wci.UseSamePermissionsAsParentSite = $SamePermissions
  73. $wci.Title = $Title
  74. $wci.Url = $SubsiteUrl
  75. $wci.Language = "1033"
  76.  
  77. $SubWeb = $context.Web.Webs.Add($wci)
  78. $context.ExecuteQuery()
  79.  
  80. if($SamePermissions -eq $false){
  81. try{
  82. #Check to see if this is a root site collection that was passed
  83. Get-SPOSite -Identity $SiteUrl
  84. }
  85. catch {
  86. #If here we are working with a subsite
  87. Write-Host "Oops, it looks like we're dealing with a subsite, I can handle that" -ForegroundColor Yellow
  88. #reconstruct the SiteUrl parameter
  89. $SiteUrl = "https://tenant.sharepoint.com/"+$SiteUrl.Split("/")[3]+"/"+$SiteUrl.Split("/")[4]
  90. }
  91. finally{
  92. $ownerGroup = "$Title Owners"
  93. $memberGroup = "$Title Members"
  94. $visitorGroup = "$Title Visitors"
  95. Write-Host "Hang tight, creating groups, updating ownership and adjusting permissions"
  96. New-SPOSiteGroup -Site $SiteUrl -Group $ownerGroup -PermissionLevels "Read" | Out-Null
  97. #Updating the group owner does not work from PowerShell, using the new Set-GroupOwner function
  98. Set-GroupOwner -SiteUrl $SiteUrl -GroupToUpdate $ownerGroup.ToString() -GroupOwner $ownerGroup.ToString()
  99. New-SPOSiteGroup -Site $SiteUrl -Group $memberGroup -PermissionLevels "Read" | Out-Null
  100. Set-GroupOwner -SiteUrl $SiteUrl -GroupToUpdate $memberGroup.ToString() -GroupOwner $ownerGroup.ToString()
  101. New-SPOSiteGroup -Site $SiteUrl -Group $visitorGroup -PermissionLevels "Read" | Out-Null
  102. Set-GroupOwner -SiteUrl $SiteUrl -GroupToUpdate $visitorGroup.ToString() -GroupOwner $ownerGroup.ToString()
  103. Set-PermissionsOnSite -Url $subsite -GroupName $ownerGroup -Roletype "Full Control"
  104. Set-PermissionsOnSite -Url $subsite -GroupName $memberGroup -Roletype "Contribute"
  105. Set-PermissionsOnSite -Url $subsite -GroupName $visitorGroup -Roletype "Read"
  106. Set-DefaultSiteGroups -SiteUrl $subsite -VisitorGroup $visitorGroup -MemberGroup $memberGroup -OwnerGroup $ownerGroup
  107. }
  108. }
  109. }
  110. end {
  111. $context.Dispose()
  112. Write-Host -ForegroundColor Green "Subsite $subsite created successfully!"
  113. }
  114. }
  115.  
  116. #Add references to SharePoint client assemblies and authenticate to Office 365 site
  117. Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll"
  118. Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Publishing.dll"
  119. Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
  120. $Username = Read-Host -Prompt "Please enter your username"
  121. $Password = Read-Host -Prompt "Please enter your password" -AsSecureString
  122. $Site = "https://site.sharepoint.com"
  123. $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
  124. $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username,$Password)
  125. $Context.Credentials = $Creds
  126.  
  127.  
  128. #Create SubSite
  129. $WCI = New-Object Microsoft.SharePoint.Client.WebCreationInformation
  130. $WCI.WebTemplate = "STS#0"
  131. $WCI.Description = "SubSite"
  132. $WCI.Title = "SubSite"
  133. $WCI.Url = "SubSite"
  134. $WCI.Language = "1033"
  135. $SubWeb = $Context.Web.Webs.Add($WCI)
  136. $Context.ExecuteQuery()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement