0

Total items count within a list reference in another list column.

I understand its possible the total count of a column from the settings of a list. But how to get the total of all items in a list, such that the data can be referenced on another list as a column data.

0

this can be done using custom code. Basically, you can get the list item count and then update the field of the other list.

var ctx = SP.ClientContext.get_current();
this.list = ctx.get_web().get_lists().getByTitle("MyList");
ctx.load(list);

ctx.executeQueryAsync(Function.createDelegate(this, OnSuccess),
            Function.createDelegate(this, OnFail));

function onSuccess(sender, args)
{
   var count = this.list.ItemCount;
   //now you can use count to update the column on the other list
}

To update a list item, you can do something like that:

var siteUrl = '/sites/MySiteCollection';

function updateListItem(count) {

    var clientContext = new SP.ClientContext(siteUrl);
    var oList = clientContext.get_web().get_lists().getByTitle('MyOtherList');

    this.oListItem = oList.getItemById(3);

    oListItem.set_item('ListCountColumn', Count);

    oListItem.update();

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    alert('Item updated!');
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
  • Ahmed, you are Maverick! I`m not a coder by nature. Pls suggest the first set of code to be added to the List site where I want to perform the count? IF yes, do it need to be added in a script editor webpart? how to show the total number count as one of the column item in Reference List, is it via Calculated column? – Jason May 9 '16 at 23:43
  • Yes! First, you need to need to add a Script Editor web part in the list page. every time a user lands on this page, the script will execute and get the list item count and update or create a list item in the other list with this number (the field it will update can be a single line of text or number)... – Ahmed Mahmoud May 10 '16 at 2:00
  • Ahmed, I test and confirm back.... Please keep the window open for further discussion and thanks for your assistance. – Jason May 10 '16 at 2:30
  • Note that provided all your required Items are displayed in the ListView, all these Items are available in global object ctx.ListData, so the count of the Items is: ctx.ListData.Row.length – Danny '365CSI' Engelman May 10 '16 at 7:57
  • @AhmedMahmoud, sorry to disturb, but I was testing it now and had few dumb questions. I was trying to follow your suggestion and had few questions. a) code for MyList to be used, to show the copy the count of items in that list. Will it update the value when the page is accessed? b) code for otherlist to show the value from mylist, will that run every time the page is loaded and run the query against the page: mylist c) code to be added on the respective pages part of CEWP? – Jason Jun 21 '16 at 5:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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