none
SharePoint Online - Load Subsite via CSOM Powershell

    Question

  • Hi All,

    I'm trying to load a subsite via CSOM Powershell, using the SharePoint Online Client Components SDK.

    However, my site collection is using a managed path (teams):

    https://tenant.sharepoint.com/teams/sitecollection

    I want to create groups and add them to a subsite with unique permissions.

    https://tenant.sharepoint.com/teams/sitecollection/testsubsite

    Could anyone explain, what I'm doing wrong? $web1 is loading the root site collection fine. $web2 is not loading correctly.

    Exception calling "ExecuteQuery" with "0" argument(s): "Invalid URL: /testsubsite."

    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
    
    $WebURL = "https://tenant.sharepoint.com/teams/sitecollection"
    
    if($Script:Credential -eq $null) {
    $Credentials = Get-Credential -Message "SharePoint Online Credential" 
    $Script:Credential = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credentials.UserName , $Credentials.Password)
    $SPOClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
    $SPOClientContext.Credentials = $Credential;
    }
    
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($WebURL)
    $ctx.Credentials = $Credential
    
    $web1 = $ctx.Site.RootWeb
    $ctx.Load($web1)
    $ctx.ExecuteQuery()
    
    $web2 = $ctx.Site.OpenWeb("/testsubsite")
    $ctx.Load($web2)
    $ctx.ExecuteQuery()

    Regards,

    Christian

    Friday, August 12, 2016 2:58 PM

Answers

  • Hi Christian,

    The “OpenWeb()” method is one of the SPSite methods. It works on the on-premises version of SharePoint, but it does not fit for SharePoint Online.

    To retrieve subsites from root web site, you should use the following command:

    # root web site
    $rootWeb = $ctx.Web  
    # sites under the root web site
    $sites  = $rootWeb.Webs 
    $ctx.Load($rootWeb) 
    $ctx.Load($sites) 
    $ctx.ExecuteQuery()
    foreach($web in $web.Webs)
      {
           # get specific site
      }
    

    Best Regards,

    Linda Zhang


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Monday, August 15, 2016 11:51 AM

All replies

  • Try like below

    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext("https://tenant.sharepoint.com/teams/sitecollection/testsubsite")
    $ctx.Credentials = $Credential
    
    $web2 = $ctx.Web
    $ctx.Load($web2)
    $ctx.ExecuteQuery()


    ---
    Rajesh
    rjesh.com| @rjesh
    You don't need to buy me a beer, if helpful just smile, vote, and mark it as answer.

    Saturday, August 13, 2016 3:12 PM
  • Here you go

    http://stackoverflow.com/questions/22604076/use-powershell-to-retrieve-all-subsites-for-sharepoint-online


    Please remember to click Mark as Answer on the answer if it helps you

    Sunday, August 14, 2016 8:19 PM
  • Hi Christian,

    The “OpenWeb()” method is one of the SPSite methods. It works on the on-premises version of SharePoint, but it does not fit for SharePoint Online.

    To retrieve subsites from root web site, you should use the following command:

    # root web site
    $rootWeb = $ctx.Web  
    # sites under the root web site
    $sites  = $rootWeb.Webs 
    $ctx.Load($rootWeb) 
    $ctx.Load($sites) 
    $ctx.ExecuteQuery()
    foreach($web in $web.Webs)
      {
           # get specific site
      }
    

    Best Regards,

    Linda Zhang


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Monday, August 15, 2016 11:51 AM