3

How to get distinct column data of SharePoint list using REST API? Is there a way to achieve it without looping?

Thanks!

1
  • 1
    Better do some research before asking, since this is not a tutorial site Commented Sep 9, 2014 at 12:36

2 Answers 2

7

According to Use OData query operations in SharePoint REST requests, such operation as grouping is not supported.

The solution, is to apply grouping after the JSON results are returned from SharePoint REST service.

How to get distinct values from an array using jQuery

function groupBy(items,propertyName)
{
    var result = [];
    $.each(items, function(index, item) {
       if ($.inArray(item[propertyName], result)==-1) {
          result.push(item[propertyName]);
       }
    });
    return result;
}


var catalog = { products: [
   { category: "Food & Dining"},
   { category: "Techonology"},
   { category: "Retail & Apparel"},
   { category: "Retail & Apparel"}
]};

var categoryNames = groupBy(catalog.products, 'category'); //get distinct categories
console.log(categoryNames);

JSFiddle

Example

Suppose the following function is used for getting list items via SharePoint REST API:

function getListItems(url, listname, query, complete, failure) {
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items" + query,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            complete(data.d); 
        },
        error: function (data) {
            failure(data);
        }
    });
}

Then the following example demonstrates how to print distinct task names:

getListItems('https://tenant.sharepoint.com/project','Tasks','?select=Title',
    function(items){    
       var taskNames = groupBy(items,'Title');
       console.log(taskNames);
    },
    function(error){
       console.log(JSON.stringify(error));
    }
);
Sign up to request clarification or add additional context in comments.

1 Comment

This will not work, if you have more than 100 results.
0

From what I understand that you are talking about http:///_vti_bin/Lists.asmx.

Well Yes, you should be able to query the data through simple SQL - select Distinct...

I have been using the same concept for Users web service. However, it is just a lead for you.

reference: http://msdn.microsoft.com/en-us/library/office/ms429658(v=office.14).aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.