Filter Library based on TaxonomySingleValue field

Hello guys,
Could anyone help me to filter the list or library control based on column "ProcessGroup" based on the TermName or TermGuid? TermGuid should be unique, so the unique GUID is:

{
    "Label": "6",
    "TermGuid": "c0ba954f-6113-422b-ba0e-d36c4735089e",
    "WssId": 6
}

I have the term translated to 4 languages.
Tried this approach, unsuccessful.

  fd.control("libraryCorporateManagement").ready(function () {
    fd.control(
      "libraryCorporateManagement"
    ).filter = `<Eq><FieldRef Name='ProcessGroup' /><Value Type='Value'>F1 Corporate Management</Value></Eq>`;
    fd.control("libraryCorporateManagement").refresh();
  });

Also tried with - the same result.

Working solution - unpractical - In one Term I have around 100 records. Working solution is to set it by ID which I have to find out with this script

pnp.sp.web.lists
  .getByTitle("Published Documents")
  .items.select("Title", "ProcessGroup")()

That returns the items from the document library and select TaxonomyField and when I click to show the data, I see what I posted in script section at the top of the topic.

fd.spRendered(() => {
  // Set Corporate Management F1 - ID === 6
  fd.control("libraryCorporateManagement").ready(function () {
    fd.control(
      "libraryCorporateManagement"
    ).filter = `<Eq><FieldRef Name='ProcessGroup' LookupId='TRUE' /><Value Type='Lookup'>${6}</Value></Eq>`;
    fd.control("libraryCorporateManagement").refresh();
  });
  // Set the Corporate Management F1.01 Organizational Chart
  fd.control("libraryOrganizationalCharts").ready(function () {
    fd.control(
      "libraryOrganizationalCharts"
    ).filter = `<Eq><FieldRef Name='ProcessGroup' LookupId='TRUE' /><Value Type='Lookup'>${30}</Value></Eq>`;
    fd.control("libraryOrganizationalCharts").refresh();
  });
  // Label Document Type - 10,31,7,1,18
});

If you would know about different approach, I would be grateful. Please, do not send me anything about MultipleValues. My TaxonomyField is a single choice.

Thank you in advance
Stepan

Does anybody know different approach how to?
Thanks
Stepan

No guarantees @StepanS,

You can try to filter the list or library control based on the column "ProcessGroup" using the TermGuid, you can use the approach of building a CAML query. The CAML (Collaborative Application Markup Language) query language can be used to filter SharePoint lists and libraries effectively.

Here’s a step-by-step approach to filter based on TermGuid:

Step-by-Step Solution:

  1. Build the CAML Query:

    • Use the TermGuid to filter the items in the library.
  2. Apply the Filter:

    • Use the filter property to apply the CAML query to the control.

Example Implementation:

fd.spRendered(() => {
    // Function to apply CAML query filter based on TermGuid
    function applyCamlFilter(controlName, termGuid) {
        fd.control(controlName).ready(function () {
            fd.control(controlName).filter = `
                <View>
                    <Query>
                        <Where>
                            <Eq>
                                <FieldRef Name='ProcessGroup' />
                                <Value Type='Text'>${termGuid}</Value>
                            </Eq>
                        </Where>
                    </Query>
                </View>`;
            fd.control(controlName).refresh();
        });
    }

    // Apply the filter to the 'libraryCorporateManagement' control
    applyCamlFilter('libraryCorporateManagement', 'c0ba954f-6113-422b-ba0e-d36c4735089e');

    // Apply the filter to the 'libraryOrganizationalCharts' control
    applyCamlFilter('libraryOrganizationalCharts', 'c0ba954f-6113-422b-ba0e-d36c4735089e');
});

Explanation:

  1. applyCamlFilter Function:

    • This function takes two parameters: the control name and the TermGuid you want to filter by.
    • It constructs a CAML query with the <Eq> condition to match the ProcessGroup field to the given TermGuid.
  2. Applying the Filter:

    • The function is called twice to apply the filter to libraryCorporateManagement and libraryOrganizationalCharts controls.
  3. Field Type:

    • Ensure that the ProcessGroup field type matches with the type specified in the CAML query (Type='Text'). If your field type is different, adjust the Type attribute accordingly.

Notes:

  • CAML Query Syntax:

    • CAML queries are XML-based and need to be structured correctly. The <Where> clause filters items based on specified criteria.
  • Control Readiness:

    • The ready method ensures that the control is fully initialized before applying the filter and refreshing it.

Debugging Tips:

  • Check Field Internal Names:

    • Ensure the internal name of the ProcessGroup field is correct.
  • Field Types:

    • Verify that the field types in the CAML query match the actual types of the fields in your SharePoint list/library.
  • Log CAML Query:

    • You can log the CAML query to the console for debugging purposes to ensure it’s constructed correctly.
console.log(fd.control(controlName).filter);

By following this approach, you should be able to filter the list or library control based on the TermGuid effectively. If there are any issues with this example post the errors and we can try to assist.

1 Like

Hi @TN.DVS ,

Thank you for the complex answer.
Unfortunately, the behaviour of filtering based on TermGuid is strange.

when I use:

pnp.sp.web.lists.getByTitle("DocumentLibraryWithManagedMetadata").items.get();

I also see in XML, that there is a record:
image

But not chance to filter on it.
I have also tried the solution with extending properties

Filter List with Managed Metadata
(It looks like it is working for a list/not library and maybe for only one managed metadata column - not sure)

In this situation I hoped I was quite close:

_api/lists/getbytitle(‘{MyDocumentLibrary}’)/Items?$filter=Processgroup/TermGuid eq ‘{GUID of the Term}’

The result:

<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code>-1, Microsoft.SharePoint.SPException</m:code>
<m:message xml:lang="en-US">The field 'ProcessGroup' of type 'TaxonomyFieldType' cannot be used in the query filter expression.</m:message>
</m:error>

And tried this:

https://{name}.sharepoint.com/sites/{NameOfTheSite}/_api/web/lists/getbytitle('Published Documents')/GetItems(query=@v1)?@v1={"ViewXml":"<View><Query><Where><Eq><FieldRef Name='ProcessGroup' LookupId='TRUE'/><Value Type='Lookup'>c0ba954f-6113-422b-ba0e-d36c4735089e</Value></Eq></Where></Query></View>"}

No was, nothing worked :slight_smile:
Never mind, I will have to do it differently.

Thank you
Stepan

@StepanS,

We apologize for the complexity and ineffectiveness of our previous attempt.

Given the constraints and peculiar behavior of filtering with Managed Metadata fields directly through CAML or REST API, we would recommend retrieving the items first and then filtering them client-side. This approach should provide more reliable results and avoid the issues you've encountered.

This method has worked for us and may be effective for your scenario.

Hello @TN.DVS ,

no worries. For some situation it would be perfect to get all the items from the list and work with the data with destructuring in Javascript etc...

Issues may come out when a document library has over 2 500 documents and every document has different managed metadata columns - up to 4 columns.

In my scenario, I have a page where Plumsail Forms is a webpart. In the Plumsail Form I have about 20 controls "List or Library" and every control shows different documents based on managed metadata.

So the first list or library must show data for a specific Country, for example "Germany", and the Group must be equals 'Technical Documentation".

But yeah, I see some possibilities to make a call for all items in the document library and then work with them in the javascript and process them before.

Never mind, I am glad you responded to my question :slight_smile:
Kind regards
Stepan