none
Get the Page Layout of a page using CSOM RRS feed

  • Question

  • We are migrating our content up to Office 365 SharePoint. We've got a problem where we need to change the Page Layout of all of our pages. I'm working on a Powershell script to get all the pages, which I've done, but I'm running into a problem getting the current Page Layout of the page.  How do I do that?  I've tried several different things but none are working.  My latest iteration is using the PublishingPage feature, but I don't know how to get to the page layout information. 

    $allWebs | %{
        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($_.Url);
        $ctx.Credentials = $SPOCredentials; 
        $ctx.Load($ctx.web)
        $ctx.ExecuteQuery();
    
        $web = $ctx.Web
        $pubWeb = [Microsoft.SharePoint.Client.Publishing.PublishingWeb]::GetPublishingWeb($ctx, $web)
        $ctx.Load($pubWeb)
        $ctx.ExecuteQuery();
    
        $pubweb.name
    
        $pagesList = $ctx.Web.Lists.GetByTitle('Pages')
        $ctx.load($pagesList);
        $ctx.ExecuteQuery();
    
        foreach($i in $pagesList.items) {
            $pubPage = [Microsoft.SharePoint.Client.Publishing.PublishingPage]::GetPublishingPage($ctx, $i)
            $ctx.Load($pubPage);
            $ctx.ExecuteQuery();
            $pubPage['PageLayoutListItem'];
        }
    
        $ctx.dispose();
    }
    

    The $pubPage["PageLayoutListItem"] property is empty. 

    I've also tried using 

    foreach($i in $pagesList.items) {
            $file = i.file
            $ctx.Load($file);
            $ctx.ExecuteQuery();
            $file.properties['PageLayout']
        }

    but I get errors about null references in relation to the properties array.  Is this possible?

    Saturday, January 28, 2017 9:23 PM

Answers

  • I figured it out.  I had to use the Get_Item method.

        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Url);
        $ctx.Credentials = $SPOCredentials; 
        $ctx.Load($ctx.web)
        $ctx.ExecuteQuery();

        $web = $ctx.Web
        $ctx.Load($web)
        $ctx.ExecuteQuery();
        
        "Url:" + $web.url;
        $pagesList = $web.Lists.GetByTitle('Pages')
        $ctx.load($pagesList);
        $ctx.ExecuteQuery();
        $qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
        $items = $pagesList.getItems($qry);
        $ctx.Load($items)
        $ctx.ExecuteQuery()

    foreach($page in $items) { $file = $page.file; $ctx.load($file); $ctx.ExecuteQuery(); $file.name; $layout = $page.Get_Item("PublishingPageLayout"); $layout.url; .... }




    Sunday, January 29, 2017 1:55 PM

All replies

  • Hi

    because you are not getting objects with $pagesList.items

    First , get all items from PagesList, and next work with each item

    $items =$pagesList.GetItems($query)
    $ctx.load($items)
    $ctx.ExecuteQuery()
    
    foreach ($item in $items)
    {
         $item['FileRef']
    }

    To get all properties for $Item use

    $item.FieldValues

    Let me know if you need more details


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Saturday, January 28, 2017 10:38 PM
  • where $query is

        $query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
    


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Saturday, January 28, 2017 10:39 PM
  • I figured it out.  I had to use the Get_Item method.

        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Url);
        $ctx.Credentials = $SPOCredentials; 
        $ctx.Load($ctx.web)
        $ctx.ExecuteQuery();

        $web = $ctx.Web
        $ctx.Load($web)
        $ctx.ExecuteQuery();
        
        "Url:" + $web.url;
        $pagesList = $web.Lists.GetByTitle('Pages')
        $ctx.load($pagesList);
        $ctx.ExecuteQuery();
        $qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
        $items = $pagesList.getItems($qry);
        $ctx.Load($items)
        $ctx.ExecuteQuery()

    foreach($page in $items) { $file = $page.file; $ctx.load($file); $ctx.ExecuteQuery(); $file.name; $layout = $page.Get_Item("PublishingPageLayout"); $layout.url; .... }




    Sunday, January 29, 2017 1:55 PM
  • Hi Robbertv,

    Glad to hear that you solve the issue, thanks for your sharing.

    If the original issue has been resolved, it would be appreciated if you can mark the reply as answer, others who stuck with the similar issue would be easier to search for valid solutions in this forum.

    Thanks,               

    Patrick


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

    Monday, January 30, 2017 7:54 AM
    Moderator