6

I am currently writing a JSON query for office 365 (sharepoint 2013) where the filter is suppose to filter depending on values returned from a managed metadata field

I am trying to filter based on status field equal to a certain value. Status is the managed metadata column so how do I write this in the filter

The filter currently has no criteria therefore reads as follows :

https://site collection/sites/SiteName/_api/web/lists/getbytitle('listname')/items?  $select=Title,AuthorId,,LastReplyById,DiscussionLastUpdated,Modified,Status, Created&$filter= "WHAT DO I PLACE HERE TO FILTER BY STATUS".

See the picture of what the returned data is.

enter image description here

I need to know what the filter will be for the status field.

7

It is not possible to use a $filter= and specify a managed metadata field - this is not supported by SharePoint. The only way to filter a REST query using a managed metadata field is to use a CAML query, which requires a POST, not a GET.

Since you mention JSON, I assume you are developing it in Javascript. This is a small snippet of code to get you started:

var taxoField = "Status";
var taxoValue = "Completed";
restUrl = appWebUrl + "/_api/web/lists/getbytitle('" + listName + "')/GetItems(query=@v1)?@v1=" +
            "{\"ViewXml\":\"<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef%20Name='" +
            taxoField + "'/><Value%20Type='TaxonomyFieldType'>" + taxoValue + "</Value></Eq></Where></Query></View>\"}" +
            select;
$.ajax(
    {
        url: restUrl,
        method: "POST",
        headers: {
            "Accept": "application/json; odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        }
    }
);

The appWebUrl, is your site URL such as https://contoso.sharepoint.com. The CAML query is built using 2 variables taxoField (name of the field), and taxoValue (value to look for). Note that the AJAX call uses the POST method (required to execute a CAML query), and the header then must have an X-RequestDigest.

You should put error checking in the AJAX call, but it is pretty close to this.

Hope it helps.

  • Great post...does this work on a managed meta data field with "Allow Multiples" checked or only single values? I need to check if the any of the values contains a value. – Stephen Dec 15 '14 at 11:10
  • Not 100% sure, but pretty confident that it also works with multi value metadata field. – pierrebo Dec 17 '14 at 3:51
0

As mentioned by Pierrebo, $filter does not support Managed Metadata Column(MMC) in REST API - GET method, I have tested below code with REST API POST Method & used for getting the filtered row count.

  • Set the listname, MMD column name, MMD column value var listName = "Custom_TaskList" var taxoField = "Status"; var taxoValue = "Completed"; GetTaxanomyItemsLength(listName, taxoField, taxoValue);

  • Helper function to retrieve filtered value

    function GetTaxanomyItemsLength(listName, taxoField, taxoValue)
    {
     $.ajax({  
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/lists/getByTitle('" + listName + "')/GetItems(query=@v1)?@v1={'ViewXml':"<View><Query><Where><Eq><FieldRef%20Name='"+ taxoField + "'/><Value%20Type='TaxonomyFieldType'>" + taxoValue + "</Value></Eq></Where></Query></View>"}",  
    type: type,  
    data: data,  
    headers:  
    {  
        "Accept": "application/json;odata=verbose",  
        "Content-Type": "application/json;odata=verbose",  
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),              
        "X-HTTP-Method": "POST"   
    },  
    cache: false,  
    success: function(data)   
    {  
       alert("Success"+data.d.results.length);
    },  
    error: function(data)  
    {  
        console.log(data.responseJSON.error);  
    }  
    });  
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.