I've a page which uses query string. I want to find how many times the page is being opened. So, I thought of running a javascript function to update a list column (Page Views) every time the page loads. I first needed to identify the item ID which needs to be updated. I was able to do this by running the below function -

<script type="text/javascript">
jQuery(document).ready(function () {               
ExecuteOrDelayUntilScriptLoaded(updatePageViews, "sp.js");          

function updatePageViews() {

    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getByTitle('Topics');
    var query = SP.CamlQuery.createAllItemsQuery();
    allItems = list.getItems(query);
    context.executeQueryAsync(Function.createDelegate(this, this.onItem1Updated), Function.createDelegate(this, this.onItemUpdateFailed));

function onItem1Updated() {

    var listEnumerator = this.allItems.getEnumerator();
    var idVar = '';
    var location = window.location.href;
    if(location.indexOf("ID=") > -1){
               var topicIdUrl = location.split("ID=")[1].split("&")[0];
    while (listEnumerator.moveNext()) {
        var currentItem = listEnumerator.get_current();
            //currentItem.set_item('Page_x0020_Views', '666');
                        idVar = currentItem.get_item('ID');

    var testItem = this.allItems.getItemById(idVar);
    testItem.set_item('Page_x0020_Views', '123');
    context.executeQueryAsync(Function.createDelegate(this, this.onItemUpdated), Function.createDelegate(this, this.onItemUpdateFailed));


function onItemUpdated() {
 alert('Item has been updated!');


function onItemUpdateFailed(sender, args) {


I now want to update a column (Page Views) in the Topics list where ID equals idVar. I tried the above approach but it's not updating the list item. This column's value should be incremented every time the function loads.

1 Answer 1


I've done something like that, but I used SharePointPlus.js to get/update list items and it is working perfectly. Maybe you should try to use SharePointPlus.js.

  • I haven't worked with SharePointPlus.js. I'm curious to know why my code is not working.
    – Kay
    Jun 24, 2017 at 11:36

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.