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 = "abc@abc.onmicrosoft.com"
$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
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.
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 .
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.
function CreateSiteCollection(){
try{
$siteCreationInfo = New-Object Microsoft.Online.SharePoint.TenantAdministration.SiteCreationProperties
$siteCreationInfo.Title = "CustomTest"
$siteCreationInfo.Url = "https://abc.sharepoint.com/sites/CustomTest"
$siteCreationInfo.Owner = "abc@abc.onmicrosoft.com"
$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
} }
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.
function GetSiteCollection(){
try{
$tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)
#$site = $tenant.GetSiteByUrl("https://abc.sharepoint.com/sites/CustomTest")
$site = $tenant.GetSitePropertiesByUrl("https://abc.sharepoint.com/sites/CustomTest",$true)
$ctx.Load($site)
$ctx.ExecuteQuery()
Write-Host "Title : " $site.Title
Write-Host "URL : " $site.Url
Write-Host "Template : " $site.Template
Write-Host "----------------------------" }
catch{
write-host "$($_.Exception.Message)" -foregroundcolor red
} }
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.
function UpdateSiteCollection(){
try{
$tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)
$site = $tenant.GetSitePropertiesByUrl("https://abc.sharepoint.com/sites/CustomTest",$true)
$site.Title = "CustomTest1"
$ctx.Load($site)
$ctx.ExecuteQuery()
Write-Host "Title : " $site.Title
Write-Host "URL : " $site.Url
Write-Host "Template : " $site.Template
Write-Host "----------------------------" }
catch{
write-host "$($_.Exception.Message)" -foregroundcolor red
}
}
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.
function DeleteSiteCollection(){
try{
$tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)
$removeSite = $tenant.RemoveSite("https://abc.sharepoint.com/sites/CustomTest")
$ctx.Load($removeSite)
$ctx.ExecuteQuery()
Write-Host "Site Collection has been deleted"
Write-Host "----------------------------" }
catch{
write-host "$($_.Exception.Message)" -foregroundcolor red
}
}
Note: The above set of operations needs to be called to get it executed.
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.
Comments