Search This Blog

2009-05-18

Create SPList programmatically

Creating a new list instance using the WSS object model :
To Create the SPList programmatically,first take a console application "AccessList" and take a reference of Microsoft.Sharepoint.dll

Write the following code and to create a Custom List "CustList2" which has a two column EmpID and EmpName.

Please modify the SPSite according to your own SPSite

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace AccessList
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://manab:36069/sites/DevSite/"))
{
using (SPWeb web = site.OpenWeb())
{
string listName = "CustList2";
SPList list = null;
foreach (SPList currentList in web.Lists)
{
if (currentList.Title.Equals(listName,
StringComparison.InvariantCultureIgnoreCase))
{
list = currentList;
break;
}
}
if (list == null)
{
Guid listID = web.Lists.Add(listName,
"List for big news items",
SPListTemplateType.GenericList);
list = web.Lists[listID];
list.OnQuickLaunch = true;
list.Fields.Add("EmpID", SPFieldType.Text, true);
list.Fields.Add("EmpName", SPFieldType.Text, false);
list.Update();
}
// add a few news items
SPListItem newItem;

newItem = list.Items.Add();
newItem["Title"] = "Title1";
newItem["EmpID"] = "160040";
newItem["EmpName"] = "Manab";
newItem.Update();


newItem = list.Items.Add();
newItem["Title"] = "Title 2";
newItem["EmpID"] = "160041";
newItem["EmpName"] = "Ranjan";
newItem.Update();
}
}
}
}
}

Now Run your console application and then go to your Site.Refresh Your Site.You will find your list "CustList2" which you have just created



Click on your List to open it and then click on the New->New Item

Now You will get the page where you can Put the values for your list.Click Ok To Save your Value

No comments: