I'm quite new to using PowerShell and SharePoint, and I've been running into issues creating a new page on a subsite. I've been trying to follow code I found here: https://sharepointstew.wordpress.com/2015/03/09/csom-powershell-to-create-a-sharepoint-publishing-page-with-custom-or-oob-page-layout/ and here: https://www.sharepointdiary.com/2018/04/sharepoint-online-create-publishing-page-using-powershell.html

I think the issue is that it's trying to find blank web part.aspx in the master page gallery in my subsite, however, checking there I have no blankwebpart.aspx files there or in the root webmaster page gallery. I've also tried running it on my root web with DefaultLayout.aspx but still the same issues. I'm kind of at a loss for how to proceed, here are some of the errors I'm running into:

function returnThings($Context) {
    $List = $Context.Web.Lists

    $mpList = $List.GetByTitle("Master Page Gallery")

    $PageLayoutName = "DefaultLayout.aspx"
    $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
    $camlQuery.ViewXml = '<View><Query><Where><Eq><FieldRef Name="FileLeafRef" /><Value Type="Text">' + $PageLayoutName + '</Value></Eq></Where></Query></View>'
    $items = $mpList.GetItems($camlQuery)
    $pageLayoutItem = $items[0]

    foreach ($PageL in $items) {
        write-host "See below: " -ForegroundColor Blue
        $PageL | ConvertTo-Csv

    # Get publishing web object
    $web = $Context.Web
    $pubWeb = [Microsoft.SharePoint.Client.Publishing.PublishingWeb]::GetPublishingWeb($Context, $web)

    #Get pages library.

    $pagesList = $Context.Web.Lists.GetByTitle("Pages")

    $PageName = "Project Deviations"

    $pubPageInfo = New-Object Microsoft.SharePoint.Client.Publishing.PublishingPageInformation
    $pubPageInfo.Name = $PageName.Replace(" ", "-") + ".aspx"
    $pubPageInfo.PageLayoutListItem = $pageLayoutItem 
    $pubPage = $pubWeb.AddPublishingpage($pubPageInfo)

    $pubPage.ListItem.File.CheckIn("", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)

Exception calling "ExecuteQuery" with "0" argument(s): "List 'Pages' does not exist at site with URL
At C:\Users\conormo\Documents\Sharepoint Repos\KcentralMigration\Scripts\testProjDeviations.ps1:70 char:5
+     $Context.ExecuteQuery()
+     ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ServerException

I'm using the SharePoint Online management shell and have the SharePoint dll's installed and all. I'm sure I'm missing something plainly obvious here, cheers

1 Answer 1


You need enable publishing feature so SharePoint will create Pages library then the script could find the library.

enter image description here

And $PageLayoutName is page layout file

enter image description here

enter image description here

  • Yup, that just about sorted it, knew it was something simple. Thanks very much!
    – conor moss
    Commented Feb 4, 2020 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.