IntroductionIn this article we will see how to read from a sharepoint list using sharepoint object model. • To access the items of a sharepoint list first of all we need to connect to the Sharepoint Site or in other words we can say that we need to open that Sharepoint site and for this purpose we will use SPSite and SPWeb Objects. • To access a List we will use SPList object. • To get all the list items from a list we will use SPlistItemCollection object. • To read the Item Collection obtained from SPlistItemCollection object we will use SPListItem object. StepCreate a Sharepoint List named Products and Add two columns in it Product ID and Product Name as shown in the below figure. Add items in the list by clicking on New button. Step 1Open Visual Studio and create console application. Step2Add reference to Microsoft.Sharepoint.dll. Step 3Create one SPSite object and pass site url as a parameter to it. //SPSite object SPSite oSpSite = new SPSite("http://spdevserver:1002/"); Step 4Create SPWeb Object to open the Web. //Connect to the web using SPWeb objectSPWeb oSPWeb = oSpSite.OpenWeb(); Step 5Create SPList object to access the List. Here we need to pass List Name which we want to access from Sharepoint site. If you know the ID than instead of name we can pass ID also. //List Object to get the list from a sharepoint siteSPList oSpList = oSPWeb.Lists["Products"]; Step 6Create SPListItemCollection to get all the list items. //Item Collection Object getting all the items form the listSPListItemCollection oSpListCln = oSpList.Items; Step 7Now we have all the list items in the itemcollection object. Iterate through each and every item in the ItemCollection object //iterate through all the items in itemcollection objectforeach (SPListItem item in oSpListCln)
{
Console.WriteLine(item["Product Name"] + "\n");
}
Console.ReadLine(); Outputthat's all enjoy. Sample Project SourceDownload source files -407 kb |