Filtering the Sharepoint List Taxonomy Column using Rest API
I ran into a strange issue today when using the SharePoint 2013 REST API for Lists with Managed Metadata columns but I just had a ‘clever workaround’ moment with SharePoint’s oData/REST implementation when it comes to filtering list items based on taxonomy (managed metadata) columns. Now I do not consider myself a developer, so this article is probably a little verbose for some readers, but should be helpful to power users or IT pros.
1. To filter the Taxonomy columns in SharePoint list using Rest API call we need to use the Post method.
2. The Rest API call will give errors some times when the Rest API Query string crosses limit 260 characters that time we will use the Query in the body. Those things are clearly explained in the below example clearly.
3. WE can use the CAMEL Query in the Rest API call this example come under that.
1. To filter the Taxonomy columns in SharePoint list using Rest API call we need to use the Post method.
2. The Rest API call will give errors some times when the Rest API Query string crosses limit 260 characters that time we will use the Query in the body. Those things are clearly explained in the below example clearly.
3. WE can use the CAMEL Query in the Rest API call this example come under that.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
getListItemsTS();
});
function getListItemsTS()
{
var restURL= _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('SPFcReact')/GetItems";
//The below camel query is used to filter single taxonomy column
// var caml="<View><Query><Where><Eq><FieldRef Name='AssetFunctionTS'/>
<Value Type='TaxonomyFieldType'>Commercial</Value></Eq></Where></Query></View>";
var caml="<View><Query><Where><And><Eq><FieldRef Name='AssetFunctionTS'/>
<Value Type='TaxonomyFieldType'>Commercial</Value></Eq><Eq><FieldRef Name='BusinessSegmentTS'/>
<Value Type='TaxonomyFieldType'>LNG</Value></Eq></And></Where></Query></View>";
//The requestData variable we are binding with camel query and we will use this in the rest api call body in place of data.
var requestData = { "query" :
{
"__metadata":
{ "type": "SP.CamlQuery" }
, "ViewXml": caml
}
};
var requestBody = JSON.stringify(requestData);
$.ajax
({
url: restURL,
type: "POST",
data: requestBody,
headers:
{
"Accept": "application/json; odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"content-length":requestBody.length
},
success: function(data)
{
for(i=0; i< data.d.results.length; i++){
if(data.d.results[i].AssetFunctionTS != null)
{
console.log("Title : " + data.d.results[i].Title);
console.log("AssetFunctionTS : "+data.d.results[i].AssetFunctionTS.Label);
console.log("BusinessSegmentTS : "+data.d.results[i].BusinessSegmentTS.Label)
}
}
},
error: function(data)
{
console.log(data.responseText);
alert(data.responseText);
}
});
}
</script>
Comments
Post a Comment