Create, Update, Delete or Retrieve SharePoint Sub Sites Using CSOM PowerShell

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.

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

    1. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"

    2. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

  2. Initialize client context object with the site URL.
    1. $siteURL = ""  
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
  3. 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. 
    1. #Not required for on premise site - Start  
    2. $userId = ""  
    3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
    5. $ctx.credentials = $creds  
    6. #Not required for on premise site - End  
  4. 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.
    1. function GetSites()  
    2. {  
    3.     try  
    4.     {  
    5.         # Relative Site URL of root site  
    6.         $rootWeb = $ctx.Site.OpenWeb("/")  
    7.         $subWebs = $rootWeb.Webs  
    8.         $ctx.Load($subWebs)  
    9.         $ctx.ExecuteQuery()  
    10.         foreach($subWeb in $subWebs)  
    11.         {  
    12.             Write - Host "Title : "  
    13.             $subWeb.Title  
    14.             Write - Host "Template : "  
    15.             $subWeb.webTemplate  
    16.             Write - Host "Description : "  
    17.             $subWeb.Description  
    18.         }  
    19.     }  
    20.     catch  
    21.     {  
    22.         write - host "$($_.Exception.Message)" - foregroundcolor red  
    23.     }  
    24. }  

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.
    1. function CreateSite()  
    2. {  
    3.     try  
    4.     {#Web Creation object to set new site details  
    5.         $webInfo = New - Object Microsoft.SharePoint.Client.WebCreationInformation  
    6.         $webInfo.Title = "SubSite"  
    7.         $webInfo.Description = "SubSite using powershell"  
    8.         $webInfo.Url = "SubSite"  
    9.         $webInfo.WebTemplate = "Blog"  
    10.         $webInfo.UseSamePermissionsAsParentSite = $true  
    11.         $webInfo.Language = 1033  
    12.         $rootWeb = $ctx.Site.OpenWeb("/")# Relative Site URL  
    13.         $newSite = $rootWeb.Webs.Add($webInfo)  
    14.         $ctx.Load($newSite)  
    15.         $ctx.ExecuteQuery()  
    16.     }  
    17.     catch  
    18.     {  
    19.         write - host "$($_.Exception.Message)" - foregroundcolor red  
    20.     }  
    21. }  

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. 
    1. function GetSite()  
    2. {  
    3.     try  
    4.     {  
    5.         $web = $ctx.site.OpenWeb("/SubSite")# Relative Site URL  
    6.         $ctx.Load($web)  
    7.         $ctx.ExecuteQuery()  
    8.         Write - Host "Title : "  
    9.         $web.Title  
    10.         Write - Host "Description: "  
    11.         $web.Description  
    12.         Write - Host "WebTemplate: "  
    13.         $web.WebTemplate  
    14.     }  
    15.     catch  
    16.     {  
    17.         write - host "$($_.Exception.Message)" - foregroundcolor red  
    18.     }  
    19. }  

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.
    1. function UpdateSite()  
    2. {  
    3.     try  
    4.     {  
    5.         $web = $ctx.site.OpenWeb("/SubSite")# Relative Site URL  
    6.         $web.Title = "SP Sub Site"  
    7.         $web.Description = "Changed description"  
    8.         $ctx.Load($web)  
    9.         $ctx.ExecuteQuery()  
    10.         Write - Host "Title : "  
    11.         $web.Title  
    12.         Write - Host "Description: "  
    13.         $web.Description  
    14.         Write - Host "WebTemplate: "  
    15.         $web.WebTemplate  
    16.     }  
    17.     catch  
    18.     {  
    19.         write - host "$($_.Exception.Message)" - foregroundcolor red  
    20.     }  
    21. }  

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.
    1. function DeleteSite()  
    2. {  
    3.     try  
    4.     {  
    5.         $deleteWeb = $ctx.site.OpenWeb("/SubSite")# Relative Site URL  
    6.         $deleteWeb.DeleteObject()  
    7.         $ctx.ExecuteQuery()  
    8.     }  
    9.     catch  
    10.     {  
    11.         write - host "$($_.Exception.Message)" - foregroundcolor red  
    12.     }  
    13. }  
Note: The above set of operations needs to be called to get it executed.
  1. GetSites #Gets all sub sites  
  2. CreateSite #Creates sub site  
  3. GetSite #Gets sub site using the relative url  
  4. UpdateSite #Updates sub site  
  5. 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.

Up Next
    Ebook Download
    View all
    Learn
    View all