Introduction
In this article, you will learn how to retrieve, create, update or delete SharePoint sub sites programmatically using CSOM with PowerShell on SharePoint 2013 / SharePoint online.
Steps Involved
The following prerequisites needs to be executed before going for CRUD operations using CSOM PowerShell on SharePoint sites.
- Add the references using the Add-Type command with necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll.
- Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
- Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
- Initialize client context object with the site URL.
- $siteURL = ""
- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
- If you are trying to access SharePoint Online site, then you need to setup the site credentials with credentials parameter and get it set to the client context.
- #Not required for on premise site - Start
- $userId = ""
- $pwd = Read-Host -Prompt "Enter password" -AsSecureString
- $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
- $ctx.credentials = $creds
- #Not required for on premise site - End
- If you are trying to access the SharePoint on premise site, then the credentials parameter is not required to be set to the context. But you need to run the code on the respective SharePoint server.
Retrieve Sub Sites
- To get all the sites from the root site collection or from particular site collection, web object needs to be accessed using OpenWeb method. If it is a root site, then RootWeb method can also be used.
- Get all the sub sites using Webs property.
- Load and execute query using the context to see the final results.
- The following code snippet shows you the flow.
- function GetSites()
- {
- try
- {
- # Relative Site URL of root site
- $rootWeb = $ctx.Site.OpenWeb("/")
- $subWebs = $rootWeb.Webs
- $ctx.Load($subWebs)
- $ctx.ExecuteQuery()
- foreach($subWeb in $subWebs)
- {
- Write - Host "Title : "
- $subWeb.Title
- Write - Host "Template : "
- $subWeb.webTemplate
- Write - Host "Description : "
- $subWeb.Description
- }
- }
- catch
- {
- write - host "$($_.Exception.Message)" - foregroundcolor red
- }
- }
Create Sub Site
The sites can be created under any site collection or sites using web creation object. The necessary parameters for creating site are title, URL, web template, language.
- Initialize the web creation object with necessary parameters.
- Access the root level site where sub site needs to be created.
- Then add the web creation object to the site. Load and execute the query.
- function CreateSite()
- {
- try
- {#Web Creation object to set new site details
- $webInfo = New - Object Microsoft.SharePoint.Client.WebCreationInformation
- $webInfo.Title = "SubSite"
- $webInfo.Description = "SubSite using powershell"
- $webInfo.Url = "SubSite"
- $webInfo.WebTemplate = "Blog"
- $webInfo.UseSamePermissionsAsParentSite = $true
- $webInfo.Language = 1033
- $rootWeb = $ctx.Site.OpenWeb("/")# Relative Site URL
- $newSite = $rootWeb.Webs.Add($webInfo)
- $ctx.Load($newSite)
- $ctx.ExecuteQuery()
- }
- catch
- {
- write - host "$($_.Exception.Message)" - foregroundcolor red
- }
- }
Get Site
- Access the sub site using OpenWeb method with site relative URL.
- Load and execute the query with content and access the site properties.
- function GetSite()
- {
- try
- {
- $web = $ctx.site.OpenWeb("/SubSite")# Relative Site URL
- $ctx.Load($web)
- $ctx.ExecuteQuery()
- Write - Host "Title : "
- $web.Title
- Write - Host "Description: "
- $web.Description
- Write - Host "WebTemplate: "
- $web.WebTemplate
- }
- catch
- {
- write - host "$($_.Exception.Message)" - foregroundcolor red
- }
- }
Update Sub Site
- Access the sub site using OpenWeb method with site relative URL.
- Change the necessary properties using web object.
- Load and execute the query with content and access the site properties.
- function UpdateSite()
- {
- try
- {
- $web = $ctx.site.OpenWeb("/SubSite")# Relative Site URL
- $web.Title = "SP Sub Site"
- $web.Description = "Changed description"
- $ctx.Load($web)
- $ctx.ExecuteQuery()
- Write - Host "Title : "
- $web.Title
- Write - Host "Description: "
- $web.Description
- Write - Host "WebTemplate: "
- $web.WebTemplate
- }
- catch
- {
- write - host "$($_.Exception.Message)" - foregroundcolor red
- }
- }
Delete Sub Site
- Access the sub site using OpenWeb method with site relative URL.
- Delete the web object using DeleteObject method and execute the query.
- function DeleteSite()
- {
- try
- {
- $deleteWeb = $ctx.site.OpenWeb("/SubSite")# Relative Site URL
- $deleteWeb.DeleteObject()
- $ctx.ExecuteQuery()
- }
- catch
- {
- write - host "$($_.Exception.Message)" - foregroundcolor red
- }
- }
Note: The above set of operations needs to be called to get it executed.
- GetSites #Gets all sub sites
- CreateSite #Creates sub site
- GetSite #Gets sub site using the relative url
- UpdateSite #Updates sub site
- DeleteSite #Deletes subsite using relative url
Summary
Thus you have learned how to get all sub sites within a site collection and also you have seen how to create, retrieve, update or delete sub sites using CSOM with PowerShell on SharePoint 2013 / SharePoint online sites.