Reading from a SharePoint List using ASP.Net

You may wish to consult this article first to see how to add a reference to the SharePoint library.  This post describes reading from a SharePoint List using ASP.Net.

This example shows how we can define a CAML query to return a set of data from a SharePoint list.

try
{
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite site = new SPSite(ConfigurationManager.AppSettings["ApplicationPortal"].ToString()))
        {
            using (SPWeb oweb = site.OpenWeb())
            {
                    
                SPList appTrackV2 = oweb.Lists[ConfigurationManager.AppSettings["TrackerV2List"].ToString()];

                SPQuery oQuery = new SPQuery();
		//Specify row limit defining how many rows (max) you want to return
                oQuery.RowLimit = 999;
		//specify fields you want to return in dataset here
                oQuery.ViewFields = "<FieldRef Name='Client'/><FieldRef Name='Requester'/>";
                oQuery.ViewFieldsOnly = true;
                oQuery.IncludeMandatoryColumns = false;
                oQuery.DatesInUtc = true;
                oQuery.ViewAttributes = "Scope='RecursiveAll'";

                string caml = "";
		//caml query to define what we want to return
                caml = "<Where><Eq><FieldRef Name='Title'></FieldRef><Value Type='Text'>" + uamid + "</Value></Eq></Where>";
                oQuery.Query = caml;

                SPListItemCollection collListItems3 = appTrackV2.GetItems(oQuery);

                string clientstr = "";

		//loop through each item in list and do something
                foreach (SPListItem oListItem in collListItems3)
                {
                    clientstr = oListItem["Client"].ToString();
                }
            }
        }
   });
}
catch (SPException ex)
{
    //Handle Exception          
}