In this article, I am going to explain and show you how to create a list in the
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.
3) Prepare the custom list with the name as Products and click Create to create a list as shown in the below figure:
4) After creating a list you can add items in a list by selecting Add new item option.
5) But before inserting items add columns in a list. You can add columns in a list by selecting the Create Column option.
Here I am adding two columns i.e. Product ID and Product Name.
6) After adding the columns add rows in a list by inserting an item.
Here I have inserted five items in a list as shown in the below figure:
In the next step, we have to access a list of data in a C# application for which we have created a list in SharePoint Server.
1) Open Visual Studio and create a Console Application.
2) Browse Microsoft.SharePoint.dll and it to the References.
3) In the next step, you have to change the properties of an application.
4) In the build option change the platform target into 64 bit (x64).
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:
Thanks for reading this article. I think this will help you a lot.
Leave Comment