SharePoint 2013 CAML Query For Item ID With jQuery

Introduction

This article explains how to get items from a list based on the value of "ID".

In SharePoint the best approach to get the data from a SharePoint list is to query those using either Server Object Model or Client Object Model. Using queries you guarantee that only list items you really need are retrieved from the database and constructed to objects by SharePoint.

Most of the time you don't need the full collection of list items, so it is not necessary to ask for all the items and then use only a few of them. Let's see how to query SharePoint lists using the Client Object Model.

Use a CAML query to find an item from a SharePoint 2013 List based on the "ID" value that is being retrieved using JavaScript.

The SharePoint web service interface Lists.asmx provides a set of simple remote method calls.

ID Type deprecated in 2013

SharePoint 2010

  1. <Query><Where><Eq><FieldRef Name=”ID” /><Value Type="Counter ">' + ID + '</Value></Eq></Where></Query> 

SharePoint 2013

  1. <Query><Where><Eq><FieldRef Name=”ID” LookupId="TRUE"/><Value Type="Text">' + ID + '</Value></Eq></Where></Query> 

I am using an ajax / jQuery script calling the getlistitems() method to filter the results using a query.

query

Use the following procedure to create a sample.

Step 1: Navigate to your SharePoint 2013 site.

Step 2: From this page select Site Actions | Edit Page:

Edit the page, go to the "Insert" tab in the Ribbon and click the "Web Part" option. In the "Web Parts" picker area, go to the "Media and Content" category, select the "Script Editor" Web Part and press the "Add button".

Step 3: Once the Web Part is inserted into the page, you will see an "EDIT SNIPPET" link; click it. You can insert the HTML and/or JavaScript as in the following:

Script

  1. <script src="/Style%20Library/Scripts/jquery-1.10.1.min.js" type="text/javascript"></script>  
  2. <script language="javascript" type="text/javascript">  
  3. $(document).ready(function () {  
  4. findtext(8);  
  5.     });  
  6. function findtext(id) {  
  7.     var soapEnv =  
  8. "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \  
  9. <soapenv:Body> \  
  10. <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \  
  11. <listName>companyInfo</listName> \  
  12. <query><Query><Where><Eq><FieldRef Name='ID' LookupId='TRUE'/><Value Type='Text'>"+id+"</Value></Eq></Where></Query></query>\  
  13. <viewFields> \  
  14. <ViewFields> \  
  15. <FieldRef Name='Company' /> \  
  16. </ViewFields> \  
  17. </viewFields> \  
  18. </GetListItems> \  
  19. </soapenv:Body> \  
  20. </soapenv:Envelope>";  
  21.     $.ajax({  
  22.         url: " /_vti_bin/Lists.asmx",  
  23.         type: "POST",  
  24.         dataType: "xml",  
  25.         data: soapEnv,  
  26.         complete: processResult,  
  27.         contentType: "text/xml; charset=\"utf-8\""  
  28.     });  
  29. }  
  30.   
  31. function processResult(xData, status) {  
  32.     $(xData.responseXML).find("z\\:row").each(function () {                          
  33.         var companyName = $(this).attr("ows_Company");  
  34.  $(‘#ResultDiv’).html(companyName);  
  35.     });     
  36. }  
  37. </script> 

Final Output

Output
Conclusion

In this article I have explained in detail how to retrieve List items from SharePoint 2013 based on “ID” column value.