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){
- // data.d will yield the required results
- },
- error: function (data){
- console.log("Error in query");
- }
- });

Jason Clark (Mbcs)Posted Jul 26, 2020, 7:53 AM
This does not work in SP2019 Online. Get this error in the console; {"readyState":4,"responseText":"{\"error\":{\"code\":\"-1, Microsoft.SharePoint.Client.InvalidClientQueryException\",\"message\":{\"lang\":\"en-US\",\"value\":\"An entry without a type name was found, but no expected type was specified. To allow entries without type information, the expected type must also be specified when the model is specified.\"}}}","responseJSON":{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"An entry without a type name was found, but no expected type was specified. To allow entries without type information, the expected type must also be specified when the model is specified."}}},"status":400,"statusText":"error"}/nerror :