1

it's me again.

Going deeper and deeper into PnP and remote provisioning is like a roller coaster experience. We can do so much things ! And yet, there is like no real formal documentation.

What i want :

Well, for today, my question is quite simple : Via my xml template, how do I set up my Library settings ? And more particularly, how do i set up metadata navigation (aka key filters) for my Library ?

What i did :

For the moment, i have two things.

  1. i've added the Web Feature in my Site Creation's xml template. Using this code below :

<

<pnp:Features> <pnp:WebFeatures><pnp:Feature ID="7201d6a4-a5d3-49a1-8c19-19c4bac6e668" Deactivate="false" Description="Metadata Navigation and Filtering (MetaDataNav) site feature" /></pnp:WebFeatures></pnp:Features>
  1. I've created my Document Library, enabling ContentTypes etc... like this :

<

<pnp:ListInstance Title="Documents projet"
                          Description="Documents partagées entre les acteurs du projet (ASI et client)"
                          TemplateType="101"
                          Url="documentprojet"
                          EnableVersioning="true"
                          EnableMinorVersions="true"
                          MinorVersionLimit="20"
                          MaxVersionLimit="100"
                          DraftVersionVisibility="0"
                          ContentTypesEnabled="true"
                          RemoveExistingContentTypes="true"
                          EnableFolderCreation="true">
         <pnp:ContentTypeBindings>
              <pnp:ContentTypeBinding ContentTypeID="0x010100BBD2E6E36069334ABFD4717B8C2FA4D9" Default="true" />
              <pnp:ContentTypeBinding ContentTypeID="0x010100BBD2E6E36069334ABFD4717B8C2FA4D901"/>
              <pnp:ContentTypeBinding ContentTypeID="0x010100BBD2E6E36069334ABFD4717B8C2FA4D902"/>
            </pnp:ContentTypeBindings>
            <pnp:FieldDefaults>
              <pnp:FieldDefault FieldName="EtatValidation">Brouillon</pnp:FieldDefault>
              <pnp:FieldDefault FieldName="EtatValidationCR">A valider</pnp:FieldDefault>
              <pnp:FieldDefault FieldName="NumeroContrat">{parameter:NumContrat}</pnp:FieldDefault>
              <pnp:FieldDefault FieldName="Client">{parameter:Client}</pnp:FieldDefault>
              <pnp:FieldDefault FieldName="NomProjet">{parameter:Projet}</pnp:FieldDefault>
            </pnp:FieldDefaults>
          </pnp:ListInstance>

But i don't see anywhere any settings, any xml attriubte talking about metadata navigation. Is it already implemented or not ?

So, do you know how i should write my Library Creation xml template to set up my Metadata Navigation setting?

Thanks a lot for your help. have a nice day !

2
  • 1
    Last I knew, there was no CSOM or REST endpoint available to enable metadata navigation, which would make it unavailable in the PnP library as well. Commented Dec 6, 2016 at 14:37
  • @EricAlexander : Thanks for the answer, it could give me path to follow. I've did a little search and i found this one here on sp.stackexchange : sharepoint.stackexchange.com/questions/144134/… Do not know if it is "real" CSOM or liek diy code... i'll get to that later. Thanks for your time !
    – Gaelle
    Commented Dec 6, 2016 at 14:59

1 Answer 1

1

In CSOM and hence in PnP guidance, there is no equivalent to MetadataNavigationSettings that we find in the Server Object Model 2013.

But there is a hack available which via property bag using CSOM. By setting the property bag value, you would be able to achieve the Metadata navigation setting.

Try the below code:

private static void AddMetadataNavigationToDocLib()
{
    ClientContext ctx = new ClientContext("https://SiteUrl");
    var rootWeb = ctx.Site.RootWeb;
    List docLib = ctx.Web.Lists.GetByTitle("Documents");
    ctx.Load(rootWeb, p => p.AvailableFields);
    ctx.Load(docLib, p => p.RootFolder, p => p.RootFolder.Properties);

    ctx.ExecuteQuery();
    if (rootWeb.AvailableFields.Any(p => p.InternalName.Equals("MetadataFieldName")))
    {
        var field = rootWeb.Fields.GetByInternalNameOrTitle("MetadataFieldName");

        ctx.Load(field, p => p.Id, p => p.InternalName, p => p.TypeAsString);
        ctx.ExecuteQuery();

        var sb = new StringBuilder();
        sb.Append("<MetadataNavigationSettings SchemaVersion='1' IsEnabled='True' AutoIndex='True'>");
        sb.Append("<NavigationHierarchies><FolderHierarchy HideFoldersNode='False' />");
        sb.AppendFormat("<MetadataField FieldID='{0}' FieldType='{1}' CachedName='{2}' CachedDisplayName='{3}' />", field.Id, field.TypeAsString, field.InternalName, field.InternalName);
        sb.Append("</NavigationHierarchies><KeyFilters /></MetadataNavigationSettings>");

        docLib.RootFolder.Properties["client_MOSS_MetadataNavigationSettings"] = sb.ToString();
        docLib.RootFolder.Update();
        docLib.Update();
        ctx.ExecuteQuery();
    }
}

Reference - Activating Document Lib Tree navigation using CSOM

MetadataNavigationSettings in Server object model 2013

Edit 2 - adding multiple fields as filters

private static void AddMetadataNavigationToDocLib()
{
    ClientContext ctx = new ClientContext("https://SiteUrl");
    var rootWeb = ctx.Site.RootWeb;
    List docLib = ctx.Web.Lists.GetByTitle("Documents");
    ctx.Load(rootWeb, p => p.AvailableFields);
    ctx.Load(docLib, p => p.RootFolder, p => p.RootFolder.Properties);

    ctx.ExecuteQuery();
    if (rootWeb.AvailableFields.Any(p => p.InternalName.Equals("MetadataFieldName")) && rootWeb.AvailableFields.Any(p => p.InternalName.Equals("MetadataFieldName2")) && rootWeb.AvailableFields.Any(p => p.InternalName.Equals("MetadataFieldName3")))
    {
        var field = rootWeb.Fields.GetByInternalNameOrTitle("MetadataFieldName");
        var field2 = rootWeb.Fields.GetByInternalNameOrTitle("MetadataFieldName2");
        var field3 = rootWeb.Fields.GetByInternalNameOrTitle("MetadataFieldName3");

        ctx.Load(field, p => p.Id, p => p.InternalName, p => p.TypeAsString);
        ctx.Load(field2, p => p.Id, p => p.InternalName, p => p.TypeAsString);
        ctx.Load(field3, p => p.Id, p => p.InternalName, p => p.TypeAsString);
        ctx.ExecuteQuery();

        var sb = new StringBuilder();
        sb.Append("<MetadataNavigationSettings SchemaVersion='1' IsEnabled='True' AutoIndex='True'>");
        sb.Append("<NavigationHierarchies><FolderHierarchy HideFoldersNode='False' />");
        sb.AppendFormat("<MetadataField FieldID='{0}' FieldType='{1}' CachedName='{2}' CachedDisplayName='{3}' />", field.Id, field.TypeAsString, field.InternalName, field.InternalName);
        sb.AppendFormat("<MetadataField FieldID='{0}' FieldType='{1}' CachedName='{2}' CachedDisplayName='{3}' />", field2.Id, field2.TypeAsString, field2.InternalName, field2.InternalName);
        sb.AppendFormat("<MetadataField FieldID='{0}' FieldType='{1}' CachedName='{2}' CachedDisplayName='{3}' />", field3.Id, field3.TypeAsString, field3.InternalName, field3.InternalName);
        sb.Append("</NavigationHierarchies><KeyFilters /></MetadataNavigationSettings>");

        docLib.RootFolder.Properties["client_MOSS_MetadataNavigationSettings"] = sb.ToString();
        docLib.RootFolder.Update();
        docLib.Update();
        ctx.ExecuteQuery();
    }
}
4
  • Thanks @GauramSheth, i've found something similar here on stack. I'm keeping it as a workaround, but i'm looking forward for this implementation to be done in xml schemas.
    – Gaelle
    Commented Dec 8, 2016 at 11:05
  • @GauramSheth : Finally, and because it is not yet enabled into pnp provisioning via xml templates, We tried it your way and damn ! It is fully working ! Thanks a lot ! I still have a question, though : how to add several Fields to the metadata nav ? Like, the code you showed will provide the "Folders" and "MetadataFieldName" nodes. But if I would like to add like the "MetadataFieldName2" and "MetadataFieldName3", how should i process? Thanks a lot !
    – Gaelle
    Commented Dec 20, 2016 at 12:44
  • @Gaelle - check the second edit code. Using this way, you can add as many fields that you want. Commented Dec 20, 2016 at 12:56
  • Ha, now i see where i was mistaking : I re-done the steps for every field, so the ctx was somehow overwritten... Thanks @gautam-sheth !
    – Gaelle
    Commented Dec 20, 2016 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.