Get Count of Distinct values in a column of SharePoint list with Javascript Client Object model

Summary : Get Count of Distinct values in a column of SharePoint list with JavaScript Client Object model,Get Unique values for a Field in SharePoint list + JavaScript + Jquery

As the title suggest this is a JavaScript & Jquery code that you can use when you need to get Count of Distinct values in a Column of SharePoint list. By Count of Distinct Values I mean, If you have a column “Department” with values “IT”, “HR” and “Operations” the script below will get the Count of the Items where the value of the column is IT or HR or Operations. So the result will be an array with

IT 4(items)

HR – 3(items)

Operations – 2(items)

Lets look at the method getDistinctItemsFromList(). You need pass the “Listname” and specify “ColumnName” in the code.

function getDistinctItemsFromList(Listname) {

try {

var context = new SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle(Listname);

var items = list.getItems();

context.load(items);
context.executeQueryAsync(
function () {

var itemCount = items.get_count();

var itemsarry = new Array(parseInt(itemCount) – 1);

var ListEnumerator = items.getEnumerator();

//adding values to array
for (i = 0; i < itemCount; i++) {
itemsarry[i] = new Array(0);
itemsarry[i][0] = items.get_item(i).get_item(ColumnName);
}

//gettig count of unique values from array
var uniqueItemsCount = 0;
var uniqueItems = {};
$.each(itemsarry, function () {
var num = this[0];
uniqueItems[num] = uniqueItems[num] + 1 || 1;
uniqueItemsCount++;
});

//uniqueItems is your array with Column value and the associated Count.

//uniqueItemsCount is how many of these distinct values exist which is 3 in our case (IT, HR and Operations)

//Now to extract the values from this array use the snippet below

var j = 0;
$.each(uniqueItems, function (itemValue, noOfItems) {
if (itemValue != ‘undefined’);
{
alert(itemValue);
alert(noOfItems);
j++;
}
});

},
function (sender, args) { alert(“error in inner request: ” + args.get_message()); }
);
}
catch (e) { alert(“Please check the WebPart Properites and the values in the specified list. Error :”+ e); }
}

Result

IT 4

HR 3

Operations 2


Comments

  1. vasu rao says:

    Hi iam using this script, i placed one anchor tag to call the method but is showing some error at
    var itemsarry = new Array(parseInt(itemCount) – 1);
    View Items
    in the above two lines

Speak Your Mind

*

Disclaimer: The contents of this site are provided “AS IS”. Information and views expressed including URL(s) and other Internet website references, may change without notice. You bear the risk of using it. SharePoint, MOSS, SharePoint 2010, SharePoint 2013 and Microsoft are trademarks of the Microsoft Corporation.Learning SharePoint only provides Tips,Tricks and Tutorials for using Microsoft SharePoint, Microsoft Office and related Softwares.
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.