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 to set the author byline in the header can be found here.
Modern pages have a field called
_AuthorByLine. When we set the value of this field of a page to the login name (or the email address) of a user, we will see the user details appear in the page header byline. However, as soon as we edit the page those details disappear (or the details of the user that were set before are shown).
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
LayoutWebpartsContent field contains HTML content and to update that field we can rely on the PnP Core SDK. PnP have made our task easy by extracting the HTML content from
LayoutWebpartsContent field and mapping them to properties in the
PageHeader property of the page. The properties that we are interested in are
AuthorByLine. We need to set these properties and save the page. This will then set the
LayoutWebpartsContent property of the page.
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
AuthorByLine properties of
PageHeader are of type
String . We can set them using PnP PowerShell with the following lines of code.
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
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 view mode, the page header byline gets the user information (like Name, Job Title) from the
User Information List for user that is set. If the Job Title doesn’t appear then it might be because it is not yet updated in the
User Information List. The information in the
User Information List takes a few minutes to update/sync.
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
- If the
Authorsis set to email address of the user then the user information is obtained using Microsoft Graph.
- If the
Authorsis 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).
To display a user’s information in header byline of a modern SharePoint page, we need to set
LayoutWebpartsContent field of the page. We can do that using PnP PowerShell by setting the
AuthorByline properties of the
PageHeader property of a page.