Set modern page header author byline using PnP PowerShell

In this post we will look at a way to set the author byline (shown in the image below) of a modern SharePoint page using PnP PowerShell.

The script

_AuthorByline field

It appears that when in view mode of the page, the page header byline loads the data from _AuthorByLine field of the page. (If no data is found then the page header byline tries to get user data from LayoutWebpartsContent field of the page.) Whereas in edit mode, page header byline tries to get user data directly from LayoutWebpartsContent field of the page. Since LayoutWebpartsContent will not have the details of the user the header byline won’t show anything in edit mode.

In the image below we set the _AuthorByLine field of Page-1.aspx and then we get the LayoutWebpartsContent field of the page. We can see that the authors data in the HTML is empty.

So to make this work, along with the _AuthorByLine field, we also need to set LayoutWebpartsContent field of the page with the details of the user.

PnP Core SDK

When we set the Authors property of PageHeader , PnP Core SDK will also set the _AuthorByLine field of the page. So there is no need for us to worry about setting the _AuthorByLine field separately.

Setting Authors and AuthorByLine properties

The entire script can be found here.

We can simply compose the strings in the above format using an email address, run the above two lines and save the page. However, if the email address is invalid then we will face a problem by which an invalid value gets set as the header byline. Therefore, the script checks if the email address passed is valid by using Get-PnPUser and New-PnPUser functions.

Running the script sets the above two properties, saves the page and publishes it. By reloading the page we can see the user information in the page header byline.

Displaying user information

In the edit mode (or in view mode when _AuthorByline field is empty) the page header byline picks the user information based on the id set in Authors data of LayoutWebpartsContent .

  • If the id in Authors is set to email address of the user then the user information is obtained using Microsoft Graph.
  • If the id in Authors is set to LoginName of the user (i.e. of the format i:0#.f|membership|... then the user information is obtained using the SharePoint user profile service using REST API (SP.UserProfiles.PeopleManager/GetPropertiesFor).

Summary

SharePoint developer at Content+Cloud.