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
}
-Identity
toSet-PnPPage
cmdlet inside quotes ("
).