Skip to content

Instantly share code, notes, and snippets.

@devendrasv
Last active August 29, 2015 14:06
Show Gist options
  • Save devendrasv/e662941ae18b163781ae to your computer and use it in GitHub Desktop.
Save devendrasv/e662941ae18b163781ae to your computer and use it in GitHub Desktop.
#Credentials to connect to office 365 site collection url
$url ="https://velegandla.sharepoint.com/sites/training"
$username="[email protected]"
$password="yourpassword"
$Password = $password |ConvertTo-SecureString -AsPlainText -force
Write-Host "Load CSOM libraries" -foregroundcolor black -backgroundcolor yellow
Set-Location $PSScriptRoot
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll")
Write-Host "CSOM libraries loaded successfully" -foregroundcolor black -backgroundcolor Green
Write-Host "authenticate to SharePoint Online Tenant site $url and get ClientContext object" -foregroundcolor black -backgroundcolor yellow
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$Context.Credentials = $credentials
$context.RequestTimeOut = 5000 * 60 * 10;
$web = $context.Web
$site = $context.Site
$context.Load($web)
$context.Load($site)
try
{
$context.ExecuteQuery()
Write-Host "authenticateed to SharePoint Online site collection $url and get ClientContext object succeefully" -foregroundcolor black -backgroundcolor Green
}
catch
{
Write-Host "Not able to authenticateed to SharePoint Online site collection $url $_.Exception.Message" -foregroundcolor black -backgroundcolor Red
return
}
#creating site using WebCreationInformation calss
Write-Host "creating subsite using custom webtemplate" -foregroundcolor black -backgroundcolor yellow
$wci = New-Object Microsoft.SharePoint.Client.WebCreationInformation
$wci.Url = "myblog"
$wci.Title = "My blog"
$wci.UseSamePermissionsAsParentSite = $true
$wci.WebTemplate = "{35F87D57-3004-428B-A5EC-B4B840E38FB6}#mycustomwebtemplate"
$wci.Language = 1033
$blogWeb = $site.RootWeb.Webs.Add($wci);
try
{
$context.ExecuteQuery();
Write-Host "Sub site created successfully using custom webtemplate" -foregroundcolor black -backgroundcolor Green
}
catch
{
Write-Host "Error while creating the Sub site using custom webtemplate" $_.Exception.Message -foregroundcolor black -backgroundcolor RED
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment