Display Lists and List Items info of a Sharepoint site Programmaticaly

In sharepoint, you will encounter most often will most likely to be lists and list Items.There are number of default Lists in Sharepoint site. You can read info about sharepoint lists and even you can create your own sharepoint Lists.

In this article I am explaining about how to read Lists and List Items for a specified sharepoint site.

Here I am reading Lists information using Visual studio console application.
To create Console application go to Visual studio Framework - File - New Project
Select C# from the left side panel and Console application from the right side panel as shown below, then name the project as DisplayLists.

Sharepoint List


Now you need to add the Reference to the sharepoint Assembly.To Add reference right click on Solution Explorer and select Add Reference, browse for the Sharepoint DLL file.

You will find the Microsoft.Sharepoint.dll file at following location.

 
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\ Microsoft.SharePoint.dll


Sharepoint List

You are successfully added the sharepoint dll to your project. Now you need to write code to display list and list item information for a specified sharepoint site.

The code to display Sharepoint List and List Item information is given below.


using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; namespace DisplayLists { class Program { static void Main(string[] args) { SPSite site = new SPSite("http://home-pt72rbbzr0"); SPWeb web= site.AllWebs[0]; foreach (SPList list in web.Lists) { Console.WriteLine("List Title: {0}; List Item Count: {1}",list.Title, list.ItemCount); Console.WriteLine("List Created By: {0}; List Description: {1}", list.Author.Name, list.Description); Console.WriteLine("........................................."); foreach (SPListItem item in list.Items) { Console.WriteLine("Item Title: \t{0} for the List: \t{1}", item.Title, list.Title); } Console.WriteLine ("-------------------------------------------------------------------"); } Console.WriteLine("Total Number of Lists Found: {0}", web.Lists.Count); Console.ReadLine(); } } }

First we need to create object to our Sharepoint site using SPSite class. Here my site is http://home-pt72rbbzr0. You need to mention your sharepoint site URL here.
After that we have to select single web from the list of web’s in for a specified site. You can select web by mentioning index or name, here I use the index. Then we are displaying each list information of given web using foreach statement.
Inside this foreach statement we have another foreach statement to display information about each item for each List.
Now you need to execute the code by pressing F5, then you can get all Lists and List Item information as shown below.

tags: