Introduction
In this blog, you will see how to filter out the documents from SharePoint document libraries, using metadata tags filters with the help of REST API calls.
Building CAML query
The items can be filtered, using filter queries but when it comes to metadata tags, the querying the document libraries will be bit complicated. The CAML query is passed, using REST API with POST method to get the actual documents.
CAML query looks, as shown below.
- '<View Scope="RecursiveAll"><Query><Where><Eq><FieldRef Name="TaxKeyword"/><Value Type="TaxonomyFieldType">sharepoint</Value></Eq></Where></Query></View>';
The code snippet given below shows the data retrieval, using REST API with the help of CAML query.
- var thisUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Documents')/GetItems";
- var caml = '<View Scope="RecursiveAll"><Query><Where><Eq><FieldRef Name="TaxKeyword"/><Value Type="TaxonomyFieldType">sharepoint</Value></Eq></Where></Query></View>';
- $.ajax({
- url: thisUrl,
- type: "POST",
- headers: {
- "Accept": "application/json; odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "Content-Type": "application/json; odata=verbose"
- },
- data: JSON.stringify({"query": { "__metadata": { "type": "SP.CamlQuery" }, "ViewXml": caml}}),
- success: function (data){
-
- },
- error: function (data){
- console.log("Error in query");
- }
- });