Forum Discussion

Sean_Su's avatar
Sean_Su
Copper Contributor
Mar 22, 2023

CAML query on managed metadata field with nested terms

In the REST service, I tried to put a filter on the managed metadata field. The term set in the Term Store has 2 levels.  The CAML query only returns the value from the top level, not the second lervel. 

for instance, Seattle city is nested under Washington state. When the query value is 'Washington', it returns records.  When it is 'Seattle', it returns nothing.

 

Here is the code.  Thanks

 

 

	var caml="<View><Query><Where><Eq><FieldRef Name='TermTag'/><Value Type='TaxonomyFieldType'>" + filtervalue + "</Value></Eq></Where></Query></View>";

	var siteUrl = _spPageContextInfo.siteAbsoluteUrl;
       
             var data = { "query" : {"__metadata": { "type": "SP.CamlQuery" }, "ViewXml":caml}};          

		$.ajax({
		        url: siteUrl+"/_api/web/lists/getbytitle('"+ listName +"')/GetItems",
                method: "POST",
                data: JSON.stringify(data),
                headers: {
                   "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                   'content-type': 'application/json;odata=verbose',
                   'accept': 'application/json;odata=verbose'
                },
		
		success: function (data) {
            alert(data.d.results.length);
			 $.each(data.d.results,function(i,item){

 

the 

1 Reply

  • Sean_Su's avatar
    Sean_Su
    Copper Contributor
    OK, I changed <EQ> to <Contains>, sharepoint managed data field automatically inserts the parent term.

Resources