I received this fix via StackExhange Sharepoint but I can't seem to locate it again for my post (sorry)...but it did work using CAML Query to count items in a list.

I want to change one thing...I want to display this on a page instead of a pop up. So when I change the "alert" to a "document.write". The problem is that it gives me the correct number however, it redirects to it's on page. Here is the code:

<script type="text/javascript">
    var clientContext = null;
    var web = null;
    ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
    function Initialize()
        clientContext = new SP.ClientContext.get_current();
        web = clientContext.get_web();
        var list = web.get_lists().getByTitle("Testing");
        var camlQuery = new SP.CamlQuery();
        var q = "<View><Query><Where><Eq><FieldRef Name='State' /><Value Type='Text'>Open</Value></Eq></Where></Query><RowLimit>0</RowLimit></View>";
        this.listItems = list.getItems(camlQuery);
        clientContext.load(listItems, 'Include(ID)');
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess), 
        Function.createDelegate(this, this.onQueryFailed));
    function onListItemsLoadSuccess(sender, args) {

        var count = this.listItems.get_count();

        document.write("Open:  " + count);

    function onQueryFailed(sender, args) {
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());


I changed the



document.write("Open:  " + count);

Is there a way to place it on the current page instead of redirecting to it's own page?

Is there another code I need to add?

  • Where do you want show this count on Page? at the top or somewhere else? – Ganesh Sanap May 15 '19 at 12:56
  • Somewhere else in a table cell in the main body of the SP page. I just need to know who to call the value of the function...sorry not used to use total Javascript to do my developing. – adams-j May 15 '19 at 13:02
  • See my answer below. you need to create and simple html element like paragraph or span and append it to the element where you want to show the count. – Ganesh Sanap May 15 '19 at 13:07
  • @GaneshSanap - Thank you. So add this within the function or does this have to be called? – adams-j May 15 '19 at 13:19

You can append the count to any existing html element on page.

Using JavaScript:

var paragraph = document.createElement("P");
paragraph.innerHTML = "Count:  " + count;

Using jQuery:

var paragraph = "<p>Count:  " + count + "</p>";
  • So add this within the function or does this have to be called? – adams-j May 15 '19 at 13:12
  • Remove the line with document.write() and add this code there. – Ganesh Sanap May 15 '19 at 13:55
  • It worked! counting this as an "Answer". Many thanks! – adams-j May 15 '19 at 14:18

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.