0

I'm trying to create a function that runs every 5 seconds to check if a list item (setting) has changed. This will then reload the page (if setting is true) and sets the setting to false.

Here's what I currently have:

<html>
<head>
<script type="text/JavaScript">
<!--
function loopScript(){
    setInterval(checkSetting, 5000);
}
function checkSetting() {
 var clientContext = SP.ClientContext.get_current();
 var oList = clientContext.get_web().get_lists().getByTitle('Biz.WebPartSettings');
 var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<Query> \
   <Where> \
         <Eq> \
            <FieldRef Name='Title' /> \
            <Value Type='Text'>PS-ImageSliderRefresh-ImageSlider</Value> \
         </Eq> \
   </Where> \
</Query>");
    this.collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem);

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

}

function onQuerySucceeded(sender, args) {

    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
    //console.log(oListItem.get_fieldValues());  
        if(oListItem.get_item('Setting_x0020_Value') == 'Yes'){
            oListItem.set_item('Setting_x0020_Value', 'No');
                console.log(oListItem.get_fieldValues());  
                oListItem.update();

        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySuccess), Function.createDelegate(this, this.onQueryFailure));

        }
    }
    //alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {

    alert('Request Failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
function onQuerySuccess(sender, args){
alert('Setting changed');
location.reload(true);
}
function onQueryFailure(sender, args) {

    alert('Request Failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

//   -->
</script>
</head>
<body onload="JavaScript:loopScript();">
</body>
</html>

The error I currently have is that **loopScript() is undefined** when I have given a definition.

The reason behind this is that the page content may have changed but won't be accessible to the user to perform a refresh. Feel free to ask for more info if needed. Thanks.

1 Answer 1

1

Your problem is your string literal for set_viewXml. You need to escape newline to continue a string literal through several lines. You also need to swap your ' for "".

Example:

camlQuery.set_viewXml("<Query> \
  <Where> \
    <And> \
       <Eq> \
          <FieldRef Name='Title' /> \
          <Value Type='Text'>PS-ImageSliderRefresh-ImageSlider</Value> \
       </Eq> \
       <Eq> \
          <FieldRef Name='Setting_x0020_Value' /> \
          <Value Type='Note'>Yes</Value> \
       </Eq> \
    </And> \
 </Where> \
 </Query>");

And instead of the endless loop with while (true) which will fail horribly you could just wrap it in setInterval. Like this:

function loopScript() {
  setInterval(checkSetting, 5000);
}

You also need to call get_item with internal name, not field title. I see you use get_item('Setting Value') which won't work, since internal name can't contain space.
You can call this in your enumerator loop to see all values if you are unsure:

console.log(oListItem.get_fieldValues());
7
  • ok that makes sense. the success while loop doesn't seem to function correctly stating that the property or field has not been initialised
    – matt
    Nov 17, 2014 at 12:32
  • @matt updated answer
    – eirikb
    Nov 17, 2014 at 12:37
  • that's great, the field is now accessible as 'Setting_x0020_Value'. When set_item is used it works but on update it doesn't seem to actually change the record? updated solution
    – matt
    Nov 17, 2014 at 13:20
  • Try calling oListItem.update();, and executeQueryAsync after that
    – eirikb
    Nov 17, 2014 at 13:22
  • is that clientContext.executeQueryAsync(..)?
    – matt
    Nov 17, 2014 at 13:25

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.