Retrieve List items from SharePoint 2013 based on ID column using CAML Query

InfoPath alternatives for form designing SharePoint

In this article I have explained how to get items from a list based on the value of “ID”. In SharePoint the best approach to get the data from SharePoint a 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 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 point to ask all the items and then use only few of these. Let’s see how we can query SharePoint lists using Client object model.

Read some SharePoint tutorials on:

CAML query to find an item from a SharePoint 2013 List based on the “ID” value that is being retrieved through JavaScript SharePoint web service interface Lists.asmx that provides a set of simple remote methods calls ID Type deprecated in 2013.




SharePoint 2010:
<Query><Where><Eq><FieldRef Name=”ID” /><Value Type=”Counter “>’ + ID + ‘</Value></Eq></Where></Query>

SharePoint 2013:
<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

 Retrieve List items from SharePoint 2013 based on ID column using CAML Query
Retrieve List items from SharePoint 2013 based on ID column using CAML 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:
<script src=”/Style%20Library/Scripts/jquery-1.10.1.min.js” type=”text/javascript”></script>
<script language=”javascript” type=”text/javascript”>
$(document).ready(function () {
findtext(8);
});
function findtext(id) {
var soapEnv =
“<soapenv:Envelope xmlns:soapenv=’http://schemas.xmlsoap.org/soap/envelope/’> \
<soapenv:Body> \
<GetListItems xmlns=’http://schemas.microsoft.com/sharepoint/soap/’> \
<listName>companyInfo</listName> \
<query><Query><Where><Eq><FieldRef Name=’ID’ LookupId=’TRUE’/><Value Type=’Text’>”+id+”</Value></Eq></Where></Query></query>\
<viewFields> \
<ViewFields> \
<FieldRef Name=’Company’ /> \
</ViewFields> \
</viewFields> \
</GetListItems> \
</soapenv:Body> \
</soapenv:Envelope>”;
$.ajax({
url: ” /_vti_bin/Lists.asmx”,
type: “POST”,
dataType: “xml”,
data: soapEnv,
complete: processResult,
contentType: “text/xml; charset=\”utf-8\””
});
}

function processResult(xData, status) {
$(xData.responseXML).find(“z\\:row”).each(function () {
var companyName = $(this).attr(“ows_Company”);
$(‘#ResultDiv’).html(companyName);
});
}
</script>


Final O/p:
Amazon.com

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


You May Also like the Following SharePoint Online Tutorials:

About Sagar Pardeshi

I am Developer working on Microsoft Technologies for the past 6+years. I am very much passionate about programming and my core skills are SharePoint, ASP.NET & C#,Jquery,Javascript,REST. I am running this blog to share my experience & learning with the community I am an MCP, MCTS .NET & Sharepoint 2010, MCPD Sharepoint 2010, and MCSD HTML 5,Sharepoint 2013 Core Solutions. I am currently working on Sharepoint 2010, MOSS 2007, Sharepoint 2013,Sharepoint 2013 App Dev, C#, ASP.NET, and SQL Server 2008.

View all posts by Sagar Pardeshi →

Leave a Reply