Forum Discussion

Anonymous's avatar
Anonymous
Oct 23, 2018

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

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

  • Ben Ide's avatar
    Ben Ide
    Copper Contributor

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

    • Anonymous's avatar
      Anonymous

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

      • Ben Ide's avatar
        Ben Ide
        Copper 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);

Resources