How to get distinct column data of SharePoint list using REST API? Is there a way to achieve it without looping?
Thanks!
How to get distinct column data of SharePoint list using REST API? Is there a way to achieve it without looping?
Thanks!
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.
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);
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));
}
);
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