Setting AuthorByline using PnP PowerShell
(updated)
Recently I had to look into setting the AuthorByline of a modern page using PowerShell. As some suggestions (translated to PowerShell) found by searching
on the internet didn't seem to work, I started looking into the possibilities. I came up with the following PowerShell, that uses PnP PowerShell to
get the user added and visible in the modern page.
So today (8th November 2022) I noticed this script didn't work as expected anymore. I did some additional research and figured out that I needed to add at least one extra
line of PowerShell code now. Things change in SharePoint Online, so then scripts like these can break. To be even more rigorous I added some additional code as well.
I used the following PowerShell commands to set the AuthorByline:
$page = Get-PnPClientSidePage -Identity "Test-page.aspx"
$user = New-PnPUser -LoginName "test.user@mypersonaltenant.onmicrosoft.com"
$userInfo = Get-PnPListItem -List "_catalogs/users" -Id $user.Id # Added
$author = @(
@{
"id" = $userInfo.FieldValues.UserName;
"email" = $user.Email;
"name" = $userInfo.FieldValues.Title;
"role" = $userInfo.FieldValues.JobTitle
}
) # Updated to add additional metadata (left in the ; so this can be easily changed one line if preferred)
$page.PageHeader.Authors = ConvertTo-Json $author -Compress # Updated added Compress
$page.PageHeader.AuthorByLine = "[""$($user.Email)""]" # Added (minimally needed to add)
$page.PageHeader.AuthorByLineId = $user.Id # Added
$page.Save()
I hope this is helpful to you, as this was not that straight forward.