1

So this is about the third script I've copied (this time from C# Corner) in an attempt to get a CAML query working but as with the others, I can't get it to work. Starting to get pretty upset about it tbh.

I need it to pick a single item from 'DSHaveYouMet' based on the number field 'scriptIndex' (this will be passed in as a variable but doing it with manual values atm). I've set up a page with the list in an App Part and a Script Editor WP with the code below in it. Every time I hit the button to trigger the async function, it returns the onFailedCallBack error; no errors in the F12 console either before or after clicking the button, not even a warning.

Code I'm using below:

<div><button onclick=" queryListItems ()">Click here to Create a List</button></div>  

<div id="displayDiv"></div>  
   <script type="text/javascript">  
      //This example gets all the items in the Announcements list that have a title that begins with 'T'.  
      //If your site doesn't include a list called Announcements you must make the changes indicated  

      //This variable will hold a reference to the Announcements list items collection  
      var returnedItems = null;  

      //This function loads the list and runs the query asynchronously  
      function queryListItems() {  
         //Get the current context  
         var context = new SP.ClientContext();  
         //Get the Announcements list. Alter this code to match the name of your list  
         var list = context.get_web().get_lists().getByTitle('DSHaveYouMet');  
         //Create a new CAML query  
         var caml = new SP.CamlQuery();  
         //Create the CAML that will return only items with the titles that begin with 'T'  
         caml.set_viewXml("<View><Query><Where><FieldRef Name='ID' /><Value Type='Number'>234</Value></Where></Query></View>");  
         //Specify the query and load the list oject  
         returnedItems = list.getItems(caml);  
         context.load(returnedItems);  
         //Run the query asynchronously, passing the functions to call when a response arrives  
         context.executeQueryAsync(onSucceededCallback, onFailedCallback);  
      }  

      //This function fires when the query completes successfully  
      function onSucceededCallback(sender, args) {  
         //Get an enumerator for the items in the list  
         var enumerator = returnedItems.getEnumerator();  
         //Formulate HTML from the list items  
         var markup = 'Items in the Announcements list that start with "T": <br><br>';  
         //Loop through all the items  
         while (enumerator.moveNext()) {  
         var listItem = enumerator.get_current();  
         markup += 'Item Title: ' + listItem.get_item('hymDisplayName') + '<br>';  
      }  
      //Display the formulated HTML in the displayDiv element  
      displayDiv.innerHTML = markup;  
   }  
   //This function fires when the query fails  
   function onFailedCallback(sender, args) {  
      //Formulate HTML to display details of the error  
      var markup = '<p>The request failed: <br>';  
      markup += 'Message: ' + args.get_message() + '<br>';  
      //Display the details  
      displayDiv.innerHTML = markup;  
   }  
</script>  

Any ideas?

3

You have missed <Eq></Eq>

try following query

<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Number'>234</Value></Where></Eq></Query></View>

Or If you want to get Item using ID Instated of caml Query getItemById() method.

Try following code.

function queryListItems() {  
         //Get the current context  
         var context = new SP.ClientContext();  
         //Get the Announcements list. Alter this code to match the name of your list  
         var list = context.get_web().get_lists().getByTitle('DSHaveYouMet');  
         var item=  list.getItemById(234);          
         context.load(item);  
         //Run the query asynchronously, passing the functions to call when a response arrives  
         context.executeQueryAsync(onSucceededCallback, onFailedCallback);  
      }  
  • Ahh I knew it'd be something basic like that. Thanks, working fine now. :) – Thomas Gass Jul 6 '17 at 11:55

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.