Adding a Page Properties webpart to a modern page using PnP Powershell

%3CLINGO-SUB%20id%3D%22lingo-sub-276915%22%20slang%3D%22en-US%22%3EAdding%20a%20Page%20Properties%20webpart%20to%20a%20modern%20page%20using%20PnP%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-276915%22%20slang%3D%22en-US%22%3E%3CP%3EIs%20it%20possible%20at%20the%20moment%20to%20add%20a%20Page%20Properties%20web%20part%20to%20a%20modern%20page%20using%20PnP%20PowerShell%3F%20I've%20managed%20to%20get%20the%20others%20working%20but%20looks%20like%20the%20latest%20webpart%20hasn't%20been%20added%20yet%20-%20has%20anyone%20investigated%20this%20yet%3F%20It's%20one%20of%20the%20hardest%20bits%20of%20info%20to%20search%20for!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F122%22%20target%3D%22_blank%22%3E%40Erwin%20van%20Hunen%3C%2FA%3E%26nbsp%3B-%20is%20this%20still%20waiting%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-288510%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20a%20Page%20Properties%20webpart%20to%20a%20modern%20page%20using%20PnP%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-288510%22%20slang%3D%22en-US%22%3E%3CP%3EI%20got%20the%20field%20Ids%20by%20querying%20the%20list%20and%20loading%20the%20fields%20property%3A%3C%2FP%3E%3CP%3Econtext.Load(sitePagesList%2C%20l%20%3D%26gt%3B%20l.Fields)%3B%3C%2FP%3E%3CP%3EThen%20matching%20against%26nbsp%3Ba%20list%20of%26nbsp%3Bmy%20chosen%20field%20names%2C%20something%20like%3A%3C%2FP%3E%3CP%3EsitePagesList.Fields.FirstOrDefault(x%20%3D%26gt%3B%20x.InternalName%20%3D%3D%20columnName%20%7C%7C%3CBR%20%2F%3Ex.InternalName%20%3D%3D%20columnName.Replace(%22%20%22%2C%20%22_x0020_%22)%20%7C%7C%3CBR%20%2F%3Ex.Title%20%3D%3D%20columnName)%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-287590%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20a%20Page%20Properties%20webpart%20to%20a%20modern%20page%20using%20PnP%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-287590%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%2C%20will%20give%20that%20a%20try%20later%20today%20and%20let%20you%20know.%20How%20did%20you%20pull%20the%20IDs%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-286994%22%20slang%3D%22en-US%22%3ERe%3A%20Adding%20a%20Page%20Properties%20webpart%20to%20a%20modern%20page%20using%20PnP%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-286994%22%20slang%3D%22en-US%22%3E%3CP%3EHopefully%20you%20can%20use%20this%20as%20a%20basis%20for%20the%20PowerShell%20code%3A%3C%2FP%3E%3CP%3Evar%20page%20%3D%20ClientSidePage.Load(context%2C%20%22Home.aspx%22)%3B%3C%2FP%3E%3CP%3Evar%20components%20%3D%20page.AvailableClientSideComponents()%3B%3C%2FP%3E%3CP%3Evar%20pagePropertyWebPartDefinition%20%3D%20components.FirstOrDefault(x%20%3D%26gt%3B%20x.ComponentType%20%3D%3D%201%20%26amp%3B%26amp%3B%20x.Name%20%3D%3D%20%22cf91cf5d-ac23-4a7a-9dbc-cd9ea2a4e859%22)%3B%20%2F%2F%20Page%20Properties%20Webpart%20Id%3C%2FP%3E%3CP%3Evar%20pagePropertiesWebPart%20%3D%20new%20ClientSideWebPart(pagePropertyWebPartDefinition)%20%7B%20Order%20%3D%200%20%7D%3B%3C%2FP%3E%3CP%3EList%3CSTRING%3E%20fieldIds%20%3D%20new%20List%3CSTRING%3E()%20%7B%20%2220457c05-86aa-4931-9d4b-ea9e5725f40a%22%20%2C%20%22c5c97a76-74dc-44df-be13-af1b3f660746%22%20%7D%3B%20%2F%2F%20You%20field%20Guids%3C%2FSTRING%3E%3C%2FSTRING%3E%3C%2FP%3E%3CP%3EpagePropertiesWebPart.PropertiesJson%20%3D%20JsonConvert.SerializeObject(new%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%26nbsp%3B%20title%20%3D%20%22My%20Page%20Properties%22%2C%3CBR%20%2F%3E%26nbsp%3B%20selectedFieldIds%20%3D%20fieldIds.ToArray()%2C%3CBR%20%2F%3E%26nbsp%3B%20availableFields%20%3D%20new%20string%5B%5D%20%7B%20%7D%3CBR%20%2F%3E%7D)%3B%3C%2FP%3E%3CP%3Epage.AddControl(pagePropertiesWebPart)%3B%3CBR%20%2F%3Epage.Save()%3B%3CBR%20%2F%3Epage.Publish()%3B%3C%2FP%3E%3CP%3Econtext.ExecuteQuery()%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Deleted
Not applicable

Is it possible at the moment to add a Page Properties web part to a modern page using PnP PowerShell? I've managed to get the others working but looks like the latest webpart hasn't been added yet - has anyone investigated this yet? It's one of the hardest bits of info to search for!

 

@Erwin van Hunen - is this still waiting?

3 Replies
Highlighted

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();

Highlighted

Thanks, will give that a try later today and let you know. How did you pull the IDs?

Highlighted

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);