Friday, October 5, 2012

How read data from List in sharepoint 2010

In previous posts i have given how to read data from the excel file in document library.Here i have given a customised code to read the data from share point list. In the below code we are first get the data(ordername,orderamount,orderarea) from "orders" list using QAMLquery .Then the resultant data in to list item collection and read the each values through loop condition
SPList spl =SPContext.Current.Web.Lists.TryGetList("Orders");
SPQuery spq = new SPQuery();
spq.ViewFields =string.Concat("<FieldRef Name='OrderName' />","<FieldRef Name='OrderAmount' />","<FieldRef Name='OrderArea' />");
SPListItemCollection splc = spl.GetItems(spq);
foreach(SPListItem sitem in splc){
if(sitem != null)
Oname = item["OrderName"].ToString(); 
Oamt = item["OrderAmount"];
Oarea = item["OrderArea"];

I have used condition to check whether the list item(sitem) is null or not.Because the variables (oname and etc) has used in another method  here .
Post a Comment