How to do a CAML Query on SharePoint 2013 List by JavaScript


Sathish Nadarajan
SharePoint MVP
Published On :   13 Dec 2015
Visit Count
Today :  18    Total :   10131



Sometime back, we saw the importance of Configuration List in this article. In the same article, we saw how to do a CAML Query from the CSOM C#.

The same list, in some scenario, we may need to query from the JavaScript as well.

 function GetConfigData(Category, Key) {
 
         var ctx = new SP.ClientContext.get_current();
         var web = ctx.get_site().get_rootWeb();
         this.list = web.get_lists().getByTitle("Configuration List");
 
         var queryString = "<View> <Query> <Where><And><Eq><FieldRef Name='ConfigurationCategory'/><Value Type='Text'>" + Category + "</Value></Eq><Eq><FieldRef Name='ConfigurationKey'/><Value Type='Text'>" + Key + "</Value></Eq></And></Where> </Query></View>";
 
         var query = new SP.CamlQuery();
         query.set_viewXml(queryString);
         this.listItems = list.getItems(query);
         ctx.load(listItems);
         ctx.executeQueryAsync(Function.createDelegate(this, GetConfigDataSuccess), Function.createDelegate(this, GetConfigDataFailure));
     }
 
     function GetConfigDataSuccess(sender, args) {
         var configValue = listItems.itemAt(0).get_item("ConfigurationValue");
 // Do the actual functionality here..
     }
 
     function GetConfigDataFailure(sender, args) {
         alert("Fail" + "|" + args.get_message());
     }
 

A simple straight forward call, but thought of keeping it handy.

Happy Coding,

Sathish Nadarajan.

Categories