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 = "[email protected]"
    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

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 .

Get Site Collection Properties

Update Site Collection Properties

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. Write-Host "Site Collection has been deleted"
    8. Write-Host "----------------------------"
    9. }
    10. catch{
    11. write-host "$($_.Exception.Message)" -foregroundcolor red
    12. }
    13. }
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.