Read a Sharepoint List Using Object Model

No.of Views2187
Bookmarked0 times
Downloads 
Votes0
By  Ravish Kumar Sindhwani   On  18 Jun 2010 10:06:10
Tag : SharePoint , Miscellaneous
In this article we will see how to read from a sharepoint list using sharepoint object model.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In 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.

Step

Create a Sharepoint List named Products and Add two columns in it Product ID and Product Name as shown in the below figure. 

Image Loading

Add items in the list by clicking on New button.

Step 1

Open Visual Studio and create console application. 

Image Loading

Step2

Add reference to Microsoft.Sharepoint.dll.

Image Loading

Step 3

Create one SPSite object and pass site url as a parameter to it.

//SPSite object SPSite oSpSite = new SPSite("http://spdevserver:1002/");

 Step 4

Create SPWeb Object to open the Web.

//Connect to the web using SPWeb objectSPWeb oSPWeb = oSpSite.OpenWeb();

 Step 5

Create 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 6

Create SPListItemCollection to get all the list items.

//Item Collection Object getting all the items form the listSPListItemCollection oSpListCln = oSpList.Items;

 Step 7

Now 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();

 
Output

Image Loading

that's all enjoy.

Sample Project Source

Download source files -407 kb

 
Sign Up to vote for this article
 
About Author
 
Ravish Kumar Sindhwani
Occupation-Software Engineer
Company-
Member Type-Fresh
Location-India
Joined date-11 Jun 2010
Home Page-
Blog Page-
I born and brought up in Haryana and working as a software developer in Trivandrum. I am passionate for learning new Microsoft technologies and I believe if you are learning any new thing, just write it some where. You will never forget.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top