2

I have a list with column status. Some of the items in the list are approved. I need to count the Total number of approved Items. I have to do this using javascript..

7
  • You could create a view with a filter that only shows items when the columns value is approved and then in the view set a count on that column. Jul 1, 2015 at 6:42
  • I have to do it using javascript
    – KumarV
    Jul 1, 2015 at 6:45
  • You mean to say: The column status is of type choice field and its noticed that approved is added more than once. And now you want to count the number of approved in the column. Correct me if I am wrong.
    – Asad Refai
    Jul 1, 2015 at 6:49
  • yes,like that only..
    – KumarV
    Jul 1, 2015 at 7:06
  • @vikashkumar What have you tried till now?
    – Asad Refai
    Jul 1, 2015 at 8:03

3 Answers 3

1

You can use REST API along with CAML query.

First create a CAML query that returns all item where Status equals Approved.

<Query>
<Where>
<Eq>
  <FieldRef Name="Status" />
  <Value Type="Text">Approved</Value>
</Eq>
</Where>
</Query>

Now use REST query using ajax. On Success method get the count of items.

http://chuvash.eu/2014/03/25/using-caml-with-sharepoint-rest-api/

3
  • Is it not possible by just using javascript
    – KumarV
    Jul 1, 2015 at 12:15
  • What you mean by just JavaScript? You need to use either JavaScript Object Model or REST API Jul 1, 2015 at 12:38
  • I don't know anything about REST API
    – KumarV
    Jul 1, 2015 at 12:39
1

http://www.learningsharepoint.com/2012/09/22/get-count-of-distinct-values-in-a-column-of-sharepoint-list-with-javascript-client-object-model/

It will count the total number of distinct values in the column of a list.

This is what i wanted to do, we just need to replace the listname and columnname in the code with the actual list name and column name.

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); }
}
3
  • If you have found a solution to your problem from some blog. Then I would recommend instead of posting link if you post the code it will be much helpful for other visitors if they have a similar problem. There are chances that external links may break at some point. :)
    – Asad Refai
    Jul 2, 2015 at 6:54
  • Hello, What modifications should i need to do to save the noOfItems to a column?
    – AXA240778
    Apr 2, 2018 at 15:06
  • This solution was really helpful, so trying to save the noOfItems to associated columns, so i can do further validations. Please help.
    – AXA240778
    Apr 2, 2018 at 16:05
1

Just modified little bit, so it can be used as such

ExecuteOrDelayUntilScriptLoaded(getDistinctItemsFromList,  "sp.js");

function getDistinctItemsFromList(listname) {

var context = new SP.ClientContext.get_current();

var web = context.get_web();

var list = web.get_lists().getByTitle('listname');

var query = SP.CamlQuery.createAllItemsQuery();

allItems = list.getItems(query);

context.load(allItems);

context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));

}

function success() {

var itemCount = allItems.get_count();

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

var ListEnumerator = this.allItems.getEnumerator();

for (var i = 0; i < itemCount; i++) {

itemsarry[i] = new Array(0);

itemsarry[i][0] = allItems.get_item(i).get_item('Columnname');

}

var uniqueItemsCount = 0;

var uniqueItems = {};

$.each(itemsarry, function () {

var num = this[0];

uniqueItems[num] = uniqueItems[num] + 1 || 1;

uniqueItemsCount++;

});


var j = 0;

$.each(uniqueItems, function (itemValue, noOfItems) {

if (itemValue != 'undefined');

{
alert(itemValue);

alert(noOfItems);

j++;

}
});

}

function failed(sender, args) {

alert("failed. Message:" + args.get_message());

}
2
  • can it be done out of the box? Jan 9, 2018 at 12:35
  • Hello, What modifications should i need to do to save the noOfItems to a column?
    – AXA240778
    Apr 2, 2018 at 15:05

Your Answer

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

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