2

I am running a function on page load to update list item. Here's my complete code -

jQuery(document).ready(function(){
    updatePageViewCount();
});
function updatePageViewCount() {
    var clContext = new SP.ClientContext.get_current();
    var topicList = clContext.get_web().get_lists().getByTitle('Topics');
    clContext.load(topicList);
    clContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySuccess), Function.createDelegate(this, this.onQueryFail));
}

function onQuerySuccess() {
    var listItemEnum = topicList.getEnumerator();
    while (listItemEnum.moveNext()) {
       var topicListItem = listItemEnum.get_current();
       var url = window.location.href;
       alert(url);
       var topicId = url.split("ID=")[1].split("&")[0];
       alert(topicId);
       if(topicListItem.get_Item('Topic_x0020_ID') == topicId){
           var oldData = topicListItem.get_item('ViewCount');
           var newData = parseInt(oldData) + 1;
           alert(newData);
           topicListItem.set_item('ViewCount', newData);
           topicListItem.update();
           clContext.executeQueryAsync(Function.createDelegate(this, this.onItemUpdated), Function.createDelegate(this, this.onQueryFail));  
      }
   }
 }

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

 function onQueryFail(sender, args) {
       alert('Request failed.');
 }

Can anyone help me find a fix for this?

4
  • What is the problem? What does or does not work?
    – Nils
    Aug 2, 2017 at 13:56
  • I am unable to read the list item and make an update.
    – Kay
    Aug 3, 2017 at 8:02
  • Is there any error?
    – Nils
    Aug 3, 2017 at 8:16
  • It's not throwing any error
    – Kay
    Aug 3, 2017 at 8:32

5 Answers 5

3

Running your code yields an error: topicList.getEnumerator() is not possible, as topicList is an SP.List and thus, no function getEnumerator is available.

If you incorporate the answer from Venkat Konjeti and additionally modify your topicsList to:

topicList = clContext.get_web().get_lists().getByTitle('Topics').getItems(SP.CamlQuery.createAllItemsQuery());

it should work. At least I'm getting your alert(url)-messages. That is only if the list "Topics" exists and has at least one item.

4
  • After modifying the code I'm now able to read only the first item in the list. I've added a alert inside the while loop. It's only checking the first item. here's my code (added my comments for the alerts) - jsfiddle.net/np0j9Lop
    – Kay
    Aug 3, 2017 at 10:06
  • try adding the columns (Topic_x0020_ID and ViewCount) as includes in the load like so: context.load(topicList, 'Topic_x0020_ID,ViewCount');
    – Nils
    Aug 3, 2017 at 10:27
  • I'm calling the load method without any additional parameter. So, it's loading all columns in the list. And I'm able to alert these two column's values for an item. But the if condition is still failing.
    – Kay
    Aug 3, 2017 at 11:36
  • I updated the code again. Now, I'm able to see the updated value after set_item operation inside if statement, but it's not getting saved in the list. Update method is failing. My code jsfiddle.net/np0j9Lop/5
    – Kay
    Aug 3, 2017 at 12:46
3

make the couple of changes as below.

  1. Use ExecuteorDelayUntilScriptLoaded to make sure you are executing logic after sp.js file loaded.
  2. Your topLists variable make it global as you are using in other function.

Make the corrections as shown below


jQuery(document).ready(function(){
    ExecuteOrDelayUntilScriptLoaded(updatePageViewCount, "sp.js");
});
var topicList
function updatePageViewCount() {
    var clContext = new SP.ClientContext.get_current();
    topicList = clContext.get_web().get_lists().getByTitle('Topics');
    clContext.load(topicList);
    clContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySuccess), Function.createDelegate(this, this.onQueryFail));
}

function onQuerySuccess() {
    //topicList is an object. so try to put some alert like this.
    alert(topicList.Title);
}

2
  • I made the corrections as you suggested. But, it did not work. What am I doing wrong? I also tried to run the function on a button click. That too did not work.
    – Kay
    Aug 3, 2017 at 5:40
  • check your browser console to see if there are errors in the scripts. Aug 3, 2017 at 15:09
2

This could be because your function is called even before loading SP.js file. Try updating your code by using the Script On Demand method call as :

jQuery(document).ready(function(){

 SP.SOD.executeFunc('sp.js','SP.ClientContext',updatePageViewCount);
});

This way your main function will run only after SP.js load.

6
  • @VenkatKonjeti - Nope. It works in SP 2010 as well. Check MSDN msdn.microsoft.com/en-us/library/office/… Aug 2, 2017 at 13:07
  • More over the best practise is Execute Function and not ExecuteorDelayUntilScriptLoaded as the later will cause an indefinite lock and never call the call back function if SP.js is not loaded . red-gate.com/simple-talk/blogs/… Aug 2, 2017 at 13:09
  • deleted my comment. But will try this. Aug 2, 2017 at 13:30
  • @Priyan It does not work for me. Alternately, I tried to run the function on button click. It also failed.
    – Kay
    Aug 3, 2017 at 8:05
  • 1
    Try putting debugger/alert in your code and let us know what is failing at what point in the code Aug 3, 2017 at 8:07
1

It may be possible "SP.js" is not fully loaded when you called your function in document ready function.

Please try below code:

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', updatePageViewCount);
2
1

I know it is almost a one year old question, but it might help others too.

If you are using Internet Explorer, and the loaded page URL contains # like in http://sharepoint/_layouts/start.aspx?#/sitepages/home.aspx you should read about MDS (Minimal Download Strategy). This blocks your JavaScript what is not registered with RegisterModuleInit() function.

Here is a good article: https://blogs.technet.microsoft.com/sharepointdevelopersupport/2013/02/08/register-csr-override-on-mds-enabled-sharepoint-2013-site/

Deactivating MDS could also help.

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.