Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Specify tenant admin and site URL
- $Username = "admin@contoso.onmicrosoft.com"
- $Password = Read-Host -Prompt "Please enter your password" -AsSecureString
- $Site = "https://contoso.sharepoint.com/sites/projects"
- $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
- $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username,$Password)
- $Context.Credentials = $Creds
- #Search Subsite Templates
- #$Templates = $Context.Site.GetWebTemplates("1043","0")
- #$Context.Load($Templates)
- #$Context.ExecuteQuery()
- #$Templates | Where {$_.Name -like "*STS#0*" } | Select Name, Description
- #Main / project
- #Load CSV
- $dataSource = import-csv βC:tempcontoso.csvβ
- foreach($dataRecord in $dataSource)
- {
- #Define properties
- $projectnummer = $dataRecord.Projectnummer
- $project = $dataRecord.Project
- #check if subsite is present or not
- $web = $Context.Web
- $Context.Load($web.Webs)
- $Context.ExecuteQuery()
- $subweb = $web.Webs | where{$_.Url -like "*$projectnummer*"}
- if($subweb)
- {
- #Execute if subsite exists
- Write-Host "Project $projectnummer exists" -ForegroundColor Green
- continue
- }
- else
- {
- #Execute if subsite does not exists
- Write-Host "Project $projectnummer does not exists yet, creating..." -ForegroundColor Green
- #Create Subsite in root
- $Subsite = New-Object Microsoft.SharePoint.Client.WebCreationInformation
- $Subsite.WebTemplate = "STS#0"
- $Subsite.Title = "$project"
- $Subsite.Description = "$projectnummer"
- $Subsite.Url = $projectnummer
- $Subsite.Language = "1043"
- $SubWeb = $Context.Web.Webs.Add($Subsite)
- $Context.Load($SubWeb)
- $Context.ExecuteQuery()
- }
- }
- function GetClientContext($url, $user, $password) {
- $context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
- $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user, $password)
- $context.Credentials = $credentials
- return $context
- }
- #variables
- #top level site
- $WebUrl = "https://test.sharepoint.com"
- #subsite name/url
- $newWebURL = "subsite"
- #subsite of subsite url
- $SubSubsiteUrl = "subsite2"
- #Create subsite
- $context = GetClientContext $WebUrl $User $Pass
- #create site here
- $Subsite = New-Object Microsoft.SharePoint.Client.WebCreationInformation
- $Subsite.WebTemplate = "STS#0"
- $Subsite.Title = "$newWebURL"
- $Subsite.Url = "$newWebURL"
- $SubWeb = $context.Web.Webs.Add($Subsite)
- $context.Load($SubWeb)
- Try{
- $context.ExecuteQuery()
- LogWrite "Success creating site for $newWebURL"
- }
- Catch{
- LogWrite "Error creating subsite for $newWebURL $_.Exception.Message"
- }
- #Create subsite of subsite
- $newWebFullURL = $WebUrl + "/" + $newWebURL
- $context = GetClientContext $newWebFullURL $User $Pass
- #create site here
- $Subsite = New-Object Microsoft.SharePoint.Client.WebCreationInformation
- $Subsite.WebTemplate = "STS#0"
- $Subsite.Title = "$SubSubsiteUrl"
- $Subsite.Url = "$SubSubsiteUrl"
- $SubWeb = $context.Web.Webs.Add($Subsite)
- $context.Load($SubWeb)
- Try{
- $context.ExecuteQuery()
- LogWrite "Success creating site"
- }
- Catch{
- LogWrite "Error creating site $_.Exception.Message"
- }
Add Comment
Please, Sign In to add comment