Forum Discussion
Sean_Su
Mar 22, 2023Copper Contributor
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_SuCopper ContributorOK, I changed <EQ> to <Contains>, sharepoint managed data field automatically inserts the parent term.