top of page

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


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.

  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"

  • 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)  

  • 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

a. To get all the the site collections, we need create a tenant class object with admin URL context.

b. Using get site properties method, get the site collections using index and details boolean parameter.

c. Load and execute query using the context to see the final results.

d. 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. foreach($site in $sites){              

  8. Write-Host "Title    : " $site.Title              

  9. Write-Host "URL      : " $site.Url              

  10. Write-Host "Template : " $site.Template              

  11. Write-Host "----------------------------"         

  12. }       }  

  13. catch{           

  14. write-host "$($_.Exception.Message)" -foregroundcolor red      

  15. }      }  


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 .

a. Initialize the site creation properties object with necessary parameters.

b. create a tenant class object with admin URL context.

c. Then create the site using CreateSite method with the site creation property initialized above.

d. 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. $tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)           

  11. $newSite = $tenant.CreateSite($siteCreationInfo)           

  12. $ctx.Load($tenant)          

  13. $ctx.Load($newSite)          

  14. $ctx.ExecuteQuery()       

  15. Write-Host "URL : " $newSite.Url       }  

  16. catch{          

  17.  write-host "$($_.Exception.Message)" -foregroundcolor red      

  18. }      }  


Get Site Collection Properties

a. Create the tenant object with admin URL context.

b. Access the site using GetSitePropertiesByUrl method and site collection URL. c. 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. $ctx.Load($site)          

  7. $ctx.ExecuteQuery()           

  8. Write-Host "Title    : " $site.Title          

  9. Write-Host "URL      : " $site.Url          

  10. Write-Host "Template : " $site.Template                  

  11. Write-Host "----------------------------"     }  

  12. catch{           

  13. write-host "$($_.Exception.Message)" -foregroundcolor red      

  14. }      }  


Update Site Collection Properties

a. Create the tenant object with admin URL context.

b. Access the site using GetSitePropertiesByUrl method and site collection URL. 

c. Then update the necessary parameters.

d. 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. Write-Host "Title    : " $site.Title          

  9. Write-Host "URL      : " $site.Url          

  10. Write-Host "Template : " $site.Template                  

  11. Write-Host "----------------------------"     }  

  12. catch{           

  13. write-host "$($_.Exception.Message)" -foregroundcolor red      

  14. }      

  15. }  


Delete Site Collection

a. Create the tenant object with admin URL context.

b. Remove the site using RemoveSite method with Site Collection URL.

c. 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. Write-Host "Site Collection has been deleted"         

  8. Write-Host "----------------------------"     }  

  9. catch{           

  10. write-host "$($_.Exception.Message)" -foregroundcolor red      

  11. }   

  12. }  


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.

0 comments
bottom of page