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

Share this post :

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();

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;

//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’);

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); }


IT 4

HR 3

Operations 2

Share this post :
  • vasu rao

    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