Forum Discussion
Adding a Page Properties webpart to a modern page using PnP Powershell
Hopefully you can use this as a basis for the PowerShell code:
var page = ClientSidePage.Load(context, "Home.aspx");
var components = page.AvailableClientSideComponents();
var pagePropertyWebPartDefinition = components.FirstOrDefault(x => x.ComponentType == 1 && x.Name == "cf91cf5d-ac23-4a7a-9dbc-cd9ea2a4e859"); // Page Properties Webpart Id
var pagePropertiesWebPart = new ClientSideWebPart(pagePropertyWebPartDefinition) { Order = 0 };
List<string> fieldIds = new List<string>() { "20457c05-86aa-4931-9d4b-ea9e5725f40a" , "c5c97a76-74dc-44df-be13-af1b3f660746" }; // You field Guids
pagePropertiesWebPart.PropertiesJson = JsonConvert.SerializeObject(new
{
title = "My Page Properties",
selectedFieldIds = fieldIds.ToArray(),
availableFields = new string[] { }
});
page.AddControl(pagePropertiesWebPart);
page.Save();
page.Publish();
context.ExecuteQuery();
- DeletedNov 16, 2018
Thanks, will give that a try later today and let you know. How did you pull the IDs?
- Ben IdeNov 18, 2018Copper Contributor
I got the field Ids by querying the list and loading the fields property:
context.Load(sitePagesList, l => l.Fields);
Then matching against a list of my chosen field names, something like:
sitePagesList.Fields.FirstOrDefault(x => x.InternalName == columnName ||
x.InternalName == columnName.Replace(" ", "_x0020_") ||
x.Title == columnName);