Introduction
In this article, you will learn how to retrieve, create, update or delete SharePoint Site collections programmatically using CSOM with PowerShell on SharePoint online / office 365.
Steps Involved
The following prerequisites need to be executed before going for CRUD operations using CSOM PowerShell on SharePoint online sites.
- Add the references using the Add-Type command with necessary reference paths. The necessary references are Client.dll, Client.Runtime.dll and Tenant dlls. The path might differ user to user.
- Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
- Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
- Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
- Initialize client context object with the site URL. The site URL should be tenant admin URL.
- $siteURL = "http://abc-admin.SharePoint.com"
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- Then you need to setup the site credentials with credentials parameter and get it set to the client context.
- $userId = "[email protected]"
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx.credentials = $creds
Retrieve All Site Collections & its Properties
- To get all the the site collections, we need create a tenant class object with admin URL context.
- Using get site properties method, get the site collections using index and details boolean parameter.
- Load and execute query using the context to see the final results.
- The following code snippet shows you the flow.
- function GetSiteCollections(){
- try{
- $tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)
- $sites = $tenant.GetSiteProperties(0,$true)
- $ctx.Load($sites)
- $ctx.ExecuteQuery()
- foreach($site in $sites){
- Write-Host "Title : " $site.Title
- Write-Host "URL : " $site.Url
- Write-Host "Template : " $site.Template
- Write-Host "----------------------------"
- }
- }
- catch{
- write-host "$($_.Exception.Message)" -foregroundcolor red
- }
- }
Create Site Collection
The site collections can be created on SharePoint Online tenant. The necessary parameters for creating site are title, URL, template, owner, storage level .
- Initialize the site creation properties object with necessary parameters.
- create a tenant class object with admin URL context.
- Then create the site using CreateSite method with the site creation property initialized above.
- Load and execute the query.
- function CreateSiteCollection(){
- try{
- $siteCreationInfo = New-Object Microsoft.Online.SharePoint.TenantAdministration.SiteCreationProperties
- $siteCreationInfo.Title = "CustomTest"
- $siteCreationInfo.Url = "https://abc.sharepoint.com/sites/CustomTest"
- $siteCreationInfo.Owner = "[email protected]"
- $siteCreationInfo.Template = "STS#0"
- $siteCreationInfo.StorageMaximumLevel = "10"
- $siteCreationInfo.UserCodeMaximumLevel = "10"
- $tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)
- $newSite = $tenant.CreateSite($siteCreationInfo)
- $ctx.Load($tenant)
- $ctx.Load($newSite)
- $ctx.ExecuteQuery()
- Write-Host "URL : " $newSite.Url
- }
- catch{
- write-host "$($_.Exception.Message)" -foregroundcolor red
- }
- }

Debasis SahaPosted May 23, 2016, 1:52 PM
Good One..
Munesh SharmaPosted May 22, 2016, 10:55 AM
good one
Humayun Kabir MamunPosted May 22, 2016, 7:32 AM
Nice...
Kuppurasu NagarajPosted May 22, 2016, 5:18 AM
Nice Sharing..
Vignesh ManiPosted May 21, 2016, 9:28 AM
Nice
Prasanna MuraliPosted May 20, 2016, 12:36 AM
nice one..
Neeraj KumarPosted May 19, 2016, 11:20 PM
Good