Create, Update, Delete Or Retrieve SharePoint Site Collections Using CSOM PowerShell

In this article you will learn how to retrieve, create, update or delete SharePoint Site Collections programmatically on SharePoint Online Tenant using CSOM with PowerShell (SharePoint Online / Office 365).

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.

  1. 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.

    1. Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"  
    2. Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"  
    3. Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"  

  2. Initialize client context object with the site URL. The site URL should be tenant admin URL.
    1. $siteURL = "http://abc-admin.SharePoint.com"  
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  

  3. Then you need to setup the site credentials with credentials parameter and get it set to the client context. 
    1. $userId = "abc@abc.onmicrosoft.com"  
    2. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    3. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
    4. $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.

    1. function GetSiteCollections(){  
    2.     try{  
    3.         $tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)  
    4.         $sites = $tenant.GetSiteProperties(0,$true)  
    5.         $ctx.Load($sites)  
    6.         $ctx.ExecuteQuery()  
    7.           
    8.         foreach($site in $sites){  
    9.             Write-Host "Title    : " $site.Title  
    10.             Write-Host "URL      : " $site.Url  
    11.             Write-Host "Template : " $site.Template  
    12.             Write-Host "----------------------------"  
    13.         }  
    14.   
    15.     }  
    16.     catch{  
    17.          write-host "$($_.Exception.Message)" -foregroundcolor red  
    18.     }      
    19. }  

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.

    1. function CreateSiteCollection(){  
    2.     try{  
    3.         $siteCreationInfo = New-Object Microsoft.Online.SharePoint.TenantAdministration.SiteCreationProperties  
    4.         $siteCreationInfo.Title = "CustomTest"  
    5.         $siteCreationInfo.Url = "https://abc.sharepoint.com/sites/CustomTest"  
    6.         $siteCreationInfo.Owner = "abc@abc.onmicrosoft.com"  
    7.         $siteCreationInfo.Template = "STS#0"  
    8.         $siteCreationInfo.StorageMaximumLevel = "10"  
    9.         $siteCreationInfo.UserCodeMaximumLevel = "10"  
    10.           
    11.         $tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)  
    12.   
    13.         $newSite = $tenant.CreateSite($siteCreationInfo)  
    14.   
    15.         $ctx.Load($tenant)  
    16.         $ctx.Load($newSite)  
    17.         $ctx.ExecuteQuery()  

    18.         Write-Host "URL : " $newSite.Url    
    19.     }  
    20.     catch{  
    21.          write-host "$($_.Exception.Message)" -foregroundcolor red  
    22.     }      
    23. }  

Get Site Collection Properties

  • Create the tenant object with admin URL context.
  • Access the site using GetSitePropertiesByUrl method and site collection URL.
  • Load and execute the query with content and read the site properties. 

    1. function GetSiteCollection(){  
    2.     try{  
    3.         $tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)  
    4.         #$site = $tenant.GetSiteByUrl("https://abc.sharepoint.com/sites/CustomTest")  
    5.         $site = $tenant.GetSitePropertiesByUrl("https://abc.sharepoint.com/sites/CustomTest",$true)  
    6.           
    7.         $ctx.Load($site)  
    8.         $ctx.ExecuteQuery()  
    9.           
    10.         Write-Host "Title    : " $site.Title  
    11.         Write-Host "URL      : " $site.Url  
    12.         Write-Host "Template : " $site.Template          
    13.         Write-Host "----------------------------"  
    14.     }  
    15.     catch{  
    16.          write-host "$($_.Exception.Message)" -foregroundcolor red  
    17.     }      
    18. }  

Update Site Collection Properties

  • Create the tenant object with admin URL context.
  • Access the site using GetSitePropertiesByUrl method and site collection URL. 
  • Then update the necessary parameters. Let us update title.
  • Load and execute the query with content and read the site properties.

    1. function UpdateSiteCollection(){  
    2.     try{  
    3.         $tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)  
    4.         $site = $tenant.GetSitePropertiesByUrl("https://abc.sharepoint.com/sites/CustomTest",$true)  
    5.         $site.Title = "CustomTest1"                  
    6.         $ctx.Load($site)  
    7.         $ctx.ExecuteQuery()  
    8.           
    9.         Write-Host "Title    : " $site.Title  
    10.         Write-Host "URL      : " $site.Url  
    11.         Write-Host "Template : " $site.Template          
    12.         Write-Host "----------------------------"  
    13.     }  
    14.     catch{  
    15.          write-host "$($_.Exception.Message)" -foregroundcolor red  
    16.     }      
    17. }  

Delete Site Collection

  • Create the tenant object with admin URL context.
  • Remove the site using RemoveSite method with Site Collection URL.
  • Load and execute the query with remove object.

    1. function DeleteSiteCollection(){  
    2.     try{  
    3.         $tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)  
    4.         $removeSite = $tenant.RemoveSite("https://abc.sharepoint.com/sites/CustomTest")  
    5.         $ctx.Load($removeSite)          
    6.         $ctx.ExecuteQuery()  
    7.           
    8.         Write-Host "Site Collection has been deleted"        
    9.         Write-Host "----------------------------"  
    10.     }  
    11.     catch{  
    12.          write-host "$($_.Exception.Message)" -foregroundcolor red  
    13.     }   
    14. }  
Note: The above set of operations needs to be called to get it executed.
  1. GetSiteCollections #Gets all Site Collections  
  2. CreateSiteCollection #Creates Site Collection  
  3. GetSiteCollection #Gets Single Site Collection  
  4. UpdateSiteCollection #Updates site collection properties  
  5. DeleteSiteCollection #Deletes site collection using url  
Summary

Thus you have learned how to get all the site collections on SharePoint Online tenant and also you have seen how to create, retrieve, update or delete site collections using CSOM with PowerShell on SharePoint online.