Skip to content

Instantly share code, notes, and snippets.

@anoopt
Last active May 5, 2021 20:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anoopt/45abde570c228f2a168fec9c4508b230 to your computer and use it in GitHub Desktop.
Save anoopt/45abde570c228f2a168fec9c4508b230 to your computer and use it in GitHub Desktop.
A script to set the author byline of a modern SharePoint page using PnP PowerShell
Param(
[Parameter(mandatory = $true)]
[string]$SiteUrl,
[Parameter(mandatory = $true)]
[string]$PageName,
[Parameter(mandatory = $true)]
[string]$UserEmail
)
function Set-PageHeaderAuthor {
# Connect to the site
Connect-PnPOnline -Url $SiteUrl;
# If there is an error in the connection then return
if ($null -eq $(Get-PnPConnection).ConnectionType) {
return;
}
# Get the page object from the specified page name / url
$page = Get-PnPPage -Identity $PageName;
# Return if page is not found
if ($null -eq $page) {
Write-Error "Page Name is not valid";
return;
}
# Get the required user from the User Information list
Write-Host "Getting user information from User Information list..." -ForegroundColor Yellow;
$user = Get-PnPUser | Where-Object Email -eq $UserEmail;
if ($null -ne $user) {
Write-Host "Got user information from User Information list." -ForegroundColor Yellow;
}
else {
# If not user is not present in User Information list then add the user to the list
# This will not affect any permissions to the site
Write-Host "User information not present in User Information list, hence adding..." -ForegroundColor Yellow;
$user = New-PnPUser -LoginName $UserEmail;
# Return if the user is not found / email address is incorrect
if ($null -eq $user) {
Write-Error "User Name is not valid";
return;
}
}
Write-Host "Setting page header author..." -ForegroundColor Yellow;
# Set the Authors and AuthorByLine properties of the PageHeader
# Both these are string properties
$page.PageHeader.Authors = "[{`"id`":`"$($user.LoginName)`"}]";
$page.PageHeader.AuthorByLine = "[`"$($user.Email)`"]";
# Save the chnages and publish the page
$page.Save() | Out-Null;
$page.Publish();
Write-Host "Done." -ForegroundColor Green;
Disconnect-PnPOnline;
}
Set-PageHeaderAuthor;
# Set-PageHeader-Author-Byline.ps1 -SiteUrl https://tenantname.sharepoint.com/sites/sitename -PageName Page-1.aspx -UserEmail user@tenantname.onmicrosoft.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment