Guest User

Untitled

a guest
Aug 18th, 2018
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #Specify tenant admin and site URL
  2. $Username = "admin@contoso.onmicrosoft.com"
  3. $Password = Read-Host -Prompt "Please enter your password" -AsSecureString
  4. $Site = "https://contoso.sharepoint.com/sites/projects"
  5. $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
  6. $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username,$Password)
  7. $Context.Credentials = $Creds
  8.  
  9. #Search Subsite Templates
  10. #$Templates = $Context.Site.GetWebTemplates("1043","0")
  11. #$Context.Load($Templates)
  12. #$Context.ExecuteQuery()
  13. #$Templates | Where {$_.Name -like "*STS#0*" } | Select Name, Description
  14.  
  15. #Main / project
  16.  
  17. #Load CSV
  18. $dataSource = import-csv β€œC:tempcontoso.csv”
  19.  
  20. foreach($dataRecord in $dataSource)
  21. {
  22. #Define properties
  23. $projectnummer = $dataRecord.Projectnummer
  24. $project = $dataRecord.Project
  25.  
  26. #check if subsite is present or not
  27. $web = $Context.Web
  28. $Context.Load($web.Webs)
  29. $Context.ExecuteQuery()
  30.  
  31. $subweb = $web.Webs | where{$_.Url -like "*$projectnummer*"}
  32. if($subweb)
  33. {
  34. #Execute if subsite exists
  35. Write-Host "Project $projectnummer exists" -ForegroundColor Green
  36. continue
  37. }
  38. else
  39. {
  40. #Execute if subsite does not exists
  41. Write-Host "Project $projectnummer does not exists yet, creating..." -ForegroundColor Green
  42.  
  43. #Create Subsite in root
  44. $Subsite = New-Object Microsoft.SharePoint.Client.WebCreationInformation
  45. $Subsite.WebTemplate = "STS#0"
  46. $Subsite.Title = "$project"
  47. $Subsite.Description = "$projectnummer"
  48. $Subsite.Url = $projectnummer
  49. $Subsite.Language = "1043"
  50. $SubWeb = $Context.Web.Webs.Add($Subsite)
  51. $Context.Load($SubWeb)
  52. $Context.ExecuteQuery()
  53. }
  54. }
  55.  
  56. function GetClientContext($url, $user, $password) {
  57. $context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
  58. $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user, $password)
  59. $context.Credentials = $credentials
  60. return $context
  61. }
  62.  
  63. #variables
  64. #top level site
  65. $WebUrl = "https://test.sharepoint.com"
  66. #subsite name/url
  67. $newWebURL = "subsite"
  68. #subsite of subsite url
  69. $SubSubsiteUrl = "subsite2"
  70.  
  71. #Create subsite
  72. $context = GetClientContext $WebUrl $User $Pass
  73. #create site here
  74. $Subsite = New-Object Microsoft.SharePoint.Client.WebCreationInformation
  75. $Subsite.WebTemplate = "STS#0"
  76. $Subsite.Title = "$newWebURL"
  77. $Subsite.Url = "$newWebURL"
  78. $SubWeb = $context.Web.Webs.Add($Subsite)
  79. $context.Load($SubWeb)
  80. Try{
  81. $context.ExecuteQuery()
  82. LogWrite "Success creating site for $newWebURL"
  83. }
  84. Catch{
  85. LogWrite "Error creating subsite for $newWebURL $_.Exception.Message"
  86. }
  87.  
  88. #Create subsite of subsite
  89. $newWebFullURL = $WebUrl + "/" + $newWebURL
  90. $context = GetClientContext $newWebFullURL $User $Pass
  91. #create site here
  92. $Subsite = New-Object Microsoft.SharePoint.Client.WebCreationInformation
  93. $Subsite.WebTemplate = "STS#0"
  94. $Subsite.Title = "$SubSubsiteUrl"
  95. $Subsite.Url = "$SubSubsiteUrl"
  96. $SubWeb = $context.Web.Webs.Add($Subsite)
  97. $context.Load($SubWeb)
  98. Try{
  99. $context.ExecuteQuery()
  100. LogWrite "Success creating site"
  101. }
  102. Catch{
  103. LogWrite "Error creating site $_.Exception.Message"
  104. }
Add Comment
Please, Sign In to add comment