Home > DeveloperSection > Articles > Create a list in SharePoint and Read list Data from SharePoint using C#

Create a list in SharePoint and Read list Data from SharePoint using C#


SharePoint SharePoint 
Ratings:
0 Comment(s)
 18019  View(s)
Rate this:

Create a list in SharePoint and Read list Data from SharePoint using C#

In this article I am going to explain and show you how to create list in SharePoint server and how to access list data in an application using C#.

To create a document library, follow these steps:


1) Open SharePoint 2010 Central Administration
2) Select More Options… option from the Site Actions menu.

 

Create a list in SharePoint and Read list Data from SharePoint using C#

3)    Prepare the custom list with the name as Products and click Create to create a list as shown in the below figure:

Create a list in SharePoint and Read list Data from SharePoint using C#

4)    After creating a list you can add items in a list by selecting Add new item option.

Create a list in SharePoint and Read list Data from SharePoint using C#

5)    But before inserting items add columns in a list. You can add columns in a list by selecting Create Column option. Here I am adding two columns i.e. Product ID and Product Name.

Create a list in SharePoint and Read list Data from SharePoint using C#

6)    After adding the columns add rows in a list by inserting item.

Create a list in SharePoint and Read list Data from SharePoint using C#

Here I am inserted a five items in a list as shown in the below figure:

Create a list in SharePoint and Read list Data from SharePoint using C#

In the next step, we have to access a list data in a C# application for which we have created a list in SharePoint Server.

1)    Open Visual Studio and create a Console Application.

Create a list in SharePoint and Read list Data from SharePoint using C#

2)    Browse Microsoft.SharePoint.dll and it to the References.

Create a list in SharePoint and Read list Data from SharePoint using C#

3)    In the next step you have to change the properties of an application.

Create a list in SharePoint and Read list Data from SharePoint using C#

4)    In the build option change the platform target into 64 bit (x64).

Create a list in SharePoint and Read list Data from SharePoint using C#

5)    Then write the below code in the Program.cs file.

using System;

using System.Text;

using Microsoft.SharePoint;

 

namespace ReadingSharePointList

{

    class Program

    {

        static void Main(string[] args)

        {

            //SPSite object

            SPSite oSpSite = new SPSite("http://rohit:34143/");

 

            //Connect to the web using SPWeb object

            SPWeb oSPWeb = oSpSite.OpenWeb();

 

            //List Object to get the list from a sharepoint site

            SPList oSpList = oSPWeb.Lists["Products"];

 

            //Item Collection Object getting all the items form the list

            SPListItemCollection oSpListCln = oSpList.Items;

 

            //iterate through all the items in itemcollection object

            foreach (SPListItem item in oSpListCln)

            {

                Console.Write(item["Product ID"] + "\t\t");

                Console.WriteLine(item["Product Name"] + "\n");

            }

        }

    }
}

Output:

Create a list in SharePoint and Read list Data from SharePoint using C#

Thanks for reading this article. I think this will help you a lot.


Don't want to miss updates? Please click the below button!

Follow MindStick