0

I have a PnP PowerShell script that takes items from an announcements list and creates news posts in SharePoint Online. It keeps the original author and creation date of the post. This script is run during migration work to O365, to keep the new site updated.

Now I have updated to the latest version of PnP and the script stopped working. I used Add-PnpClientSidePage, Set-PnpClientSidePage and Add-PnPClientSideText before, but with this version I was recommended to use Add-PnPPage, Set-PnPPage and Add-PnPTextPart, and since it stopped working anyway with the old commandlets, I tried the new ones.

In the code I am having issues finding the pages I created (so right now I can only generate empty pages):

Set-PnPPage : Page My page.aspx cannot be found.
Add-PnPPageTextPart : Page My page.aspx cannot be found.
Set-PnPListItem : Cannot bind argument to parameter 'Identity' because it is null.

I do not know what the syntax [-Identity] <PagePipeBind> means for Set-PnPPage.

What should I write to get my script back to running again?

[CmdletBinding()]
param (

    [Parameter(Mandatory = $true, HelpMessage = "Url of the site that should have news added")]
    [string]$SourceUrl
)
begin
{
    Write-Host "Connecting to " $SourceUrl
    
    Connect-PnPOnline -Url $SourceUrl -UseWebLogin
    Start-Sleep -s 3 
}
process 
{   

    $targetList = "News"
    $ListItems = Get-PnPListItem -List $targetList -Fields $fields | select -Last 1

    $ListItems | ForEach-Object {
      
        Write-Host "Item Id : " $_["ID"] "Item Title : " $_["Title"] "Item Body : " $_["Body"]
        $Created = $_["Created"]
        $CreatedBy = $_["Author"].Email
        $Modified = $_["Modified"]
        $ModifiedBy = $_["Editor"].Email
        
        Add-PnPPage -Name $_["Title"] -PromoteAs NewsArticle -Publish -HeaderLayoutType NoImage
        Set-PnPPage -Identity $_["Title"] -Title $_["Title"]
        Add-PnPTextPart -Page $_["Title"] -Text $_["Body"]

        Write-Host "Updating metadata!"
        
        $myTitle = $_["Title"].ToString()
        Write-Host "Get page!"
        $page = Get-PnPListItem -List "Site Pages" -Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>$myTitle</Value></Eq></Where></Query></View>"
        Write-Host "Set page!"
        Set-PnPListItem -List SitePages -Identity $page.Id -Values @{"Created"=$Created; "Modified"=$Modified; "Editor"=$ModifiedBy; "Author"=$CreatedBy; "FirstPublishedDate"=$Created; "_AuthorByline"=$CreatedBy;} | Out-Null
    }

    Write-Host "Done!" -ForegroundColor Green
}
end
{
    Disconnect-PnPOnline
}
2
  • Try wrapping page title you are passing as -Identity to Set-PnPPage cmdlet inside quotes ("). Mar 19, 2021 at 13:00
  • It does not seem to help to enclose it in quotes. Also, I noted that if I create a page with Title "My Page" with Add-PnPPage, the name in SharePoint will be My-Page.aspx. I do not need to rename the page, I just need to be able to find the page later to be able to add the text and the metadata.
    – Gunilla
    Mar 19, 2021 at 14:01

1 Answer 1

0

🤔 So I think the problem is that the $_["Title"] hold some text with space, but actually in SharePoint when you create a page with a space in name it will be changed to '-'.

😉 So if you would replace it the $_["Title"] all spaces to '-' it should work as you will pass the proper page name (so the My-page.aspx)

👍 But I think the best solution would be to save the result of the Add-PnPPage method to a variable and then use the property .Name of this variable as the -Identity param you should pass to Set-PnPPage.

👨‍💻 Please see the fallowing code:

> $page = Add-PnPPage -Name "Some New Page"
> Set-PnPPage -Identity $page.Name -Title "changed Title"

🙄 The result of the above should be enter image description here

and then in SharePoint I have enter image description here

Hope this will be of any help 😅

BR.

1
  • Thank you very much! The handle to $page did the trick. I could also use $page.PageId in Set-PnPListItem so I could skip the query for finding the page.
    – Gunilla
    Mar 22, 2021 at 10:04

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.