Monday, May 23, 2011

Create a SharePoint List Programmatically with SharePoint SDK

One of the major feature in the SharePoint platform is the Enterprise Content Management (ECM). ECM have2 major components: Document Management and Record Management.

Today i will discuss on how to simplify the way to create a SharePoint Lists in your solution?
Since we are using SharePoint 2010 platform for this, so we will create a Visual Web Part to Create a SharePoint List Programmatically with SharePoint SDK.

Below is the steps to create the SharePoint List Programmatically:

1. Open the Visual Studio

2. Create New Project -> Visual C# -> Visual Web Part , name the project as CreateSharePointList and click OK.

3. Validate the local site you going to debug -> Click Finish

4. Delete the visualwebpart file, add the new Visual Web Part file and name as CreateList.

5. In the Visual Web Part User Control, Design it as below:

Name: Name of the list
Descripiton: List Description
Type: Type of List to generate. i just choose few default SharePoint List like Document Library, Issue Tracking, Calendar, and Tasks.

6. Double click the Create List Button to create the event handler and insert the code as below:
SPWeb web = SPContext.Current.Site.OpenWeb();
SPUserToken token = web.SiteUsers[ADMIN_USER].UserToken;
web.Close(); web.Dispose();
using (SPSite impersonatedSiteCollection = new SPSite(“http://sp2010-dev“, token))
{
using (SPWeb impersonatedSite = impersonatedSiteCollection.OpenWeb())
{
impersonatedSite.AllowUnsafeUpdates = true;
SPListCollection lists = impersonatedSite.Lists;
SPListTemplateType type = new SPListTemplateType();
if (ddlType.SelectedValue == “Document Library”)
{
type = SPListTemplateType.DocumentLibrary;
}
if (ddlType.SelectedValue == “Issue Tracking”)
{
type = SPListTemplateType.IssueTracking;
}
if (ddlType.SelectedValue == “Calendar”)
{
type = SPListTemplateType.Events;
}
if (ddlType.SelectedValue == “Tasks”)
{
type = SPListTemplateType.Tasks;
}
SPFile defaultPage = impersonatedSite.GetFile(impersonatedSite.Url + “/default.aspx”);
Guid listGuid = lists.Add(txtName.Text, txtDescription.Text, type);
SPList newList = web.Lists[listGuid];
newList.OnQuickLaunch = true;
newList.Update();
impersonatedSite.AllowUnsafeUpdates = false;
string url = HttpContext.Current.Request.Url.AbsoluteUri;
Response.Redirect(url);
}
}

7. In the Solution file, Right Click and Click Deploy.

8. Once the deployment finished.
Go to your SharePoint Site -> click page -> click edit -> Insert -> Web Part -> Custom ->Select CreateList-> click Add.

9. Click Save and Close Button. now you home page will display the Visual Web Part you deployed.

10. Insert the List Name, Description in the textbox, Select the List Type, and click the Create List Button.

The Document Library will be created and show in the Navigation Button List as below:

11.This is another sample to create the Issue Tracking List as below:

12. You can try yourself to create the Calendar and Tasks List.

SharePoint is POWERFUL, Developer Friendly and this is one of the reason i’m LOVING SharePoint!!!!!!

No comments:

Post a Comment