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

Sathish Nadarajan
 
Solution Architect
December 13, 2015
 
Rate this article
 
Views
28004

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.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment