0

I am facing error:"the given key was not present in the dictionary " while i am creating publishing page in sharepoint online using powershell CSOM.

Here is my code:

 $subsiteCtx=New-Object Microsoft.SharePoint.Client.ClientContext($subsiteUrl)
            $subsiteCtx.Credentials = $credentials
            $subsiteCtx.ExecuteQuery()
            $subsiteweb=$subsiteCtx.Web  

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) 
             $ctx.Credentials = $credentials
             $ctx.ExecuteQuery()
             $pageLayoutList = $ctx.Web.Lists.GetByTitle("Master Page Gallery")
             $ctx.Load($pageLayoutList)
             $ctx.ExecuteQuery()

             $pageLayoutQuery = New-Object Microsoft.SharePoint.Client.CamlQuery  
             $pageLayoutQuery.ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>"+$pageLayoutName+"</Value></Eq></Where></Query></View>"  

             $pageLayouts = $pageLayoutList.GetItems($pageLayoutQuery)  
             $ctx.Load($pageLayouts)  
             $ctx.ExecuteQuery()  
             $pageLayout = $pageLayouts[0]

                 Write-Host $pageLayouts.Count "-" $pageLayout.Id 

             #Get the publishing Web  
             $pubWeb = [Microsoft.SharePoint.Client.Publishing.PublishingWeb]::GetPublishingWeb($subsiteCtx, $subsiteCtx.Web)  
             $subsiteCtx.Load($pubWeb)    
             $subsiteCtx.ExecuteQuery() 

             $pageInfo = New-Object Microsoft.SharePoint.Client.Publishing.PublishingPageInformation  
             $pageInfo.Name = $pageName  
             $pageInfo.PageLayoutListItem = $pageLayout  
             $page = $pubWeb.AddPublishingPage($pageInfo)  
             $subsiteCtx.Load($page)  
             $subsiteCtx.ExecuteQuery() 
0

Here is my sample test script, create publishing page in sub site.

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"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"

$siteUrl = "https://tenant.sharepoint.com/sites/Developer"
$UserName = "lee@tenant.onmicrosoft.com"
$Password = ConvertTo-SecureString "pw" -AsPlainText -Force
$credentials =New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password) 

# - Uncomment above lines incase of SharePoint online 
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl) 
$ctx.Credentials = $credentials 

$rootWeb = $ctx.Site.RootWeb
$ctx.Load($rootWeb)
$ctx.ExecuteQuery()

$mpList = $rootWeb.Lists.GetByTitle("Master Page Gallery")
$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$camlQuery.ViewXml = '<View Scope="RecursiveAll"><Query><Where><Eq><FieldRef Name="FileLeafRef" /><Value Type="Text">ArticleLeft.aspx</Value></Eq></Where></Query></View>'
$items = $mpList.GetItems($camlQuery)
$ctx.Load($items)
$ctx.ExecuteQuery()

$tpLayoutItem = $items[0]
$ctx.Load($tpLayoutItem)

$web = $ctx.Site.OpenWeb("test")
$pubWeb = [Microsoft.SharePoint.Client.Publishing.PublishingWeb]::GetPublishingWeb($ctx, $web)
$ctx.Load($pubWeb)

$pagesList = $web.Lists.GetByTitle("Pages")

$pubPageInfo = New-Object Microsoft.SharePoint.Client.Publishing.PublishingPageInformation
$pubPageInfo.Name ="testplaa.aspx"
$pubPageInfo.PageLayoutListItem = $tpLayoutItem
$pubPage = $pubWeb.AddPublishingpage($pubPageInfo)

$ctx.Load($pubPage)
$ctx.ExecuteQuery()

$listItem = $pubPage.get_listItem()
$ctx.Load($listItem)
$ctx.ExecuteQuery()
1
  • Code is same but i'm unable to create publishing page in subsite ,getting error "The given key was not present in the dictionary."In sitecollection level i'm able to create pages without error. Sep 17 '18 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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