Creating SharePoint Lists and Libraries using C#.Net

In SharePoint, Lists and Libraries plays important role for storing data in the form of list items and documents.

Developers often look for creating Lists in SharePoint sites using C#.Net code for SharePoint Customization. I have a requirement where i have to create a List Programmatically from a SharePoint Feature. So, here i am sharing my code below for creation of SharePoint Lists Using C#.Net for Customizing SharePoint.

 public void createSPList(string strListName)
 {
  SPSecurity.RunWithElevatedPrivileges(delegate
  {
   try
   {
   //Open your site
   SPSite site = new SPSite(Constants.strSiteURL);
   SPWeb web = site.OpenWeb();
   web.AllowUnsafeUpdates = true;
   SPListCollection lists = web.Lists;

   // create new Generic list
   lists.Add(strListName, "My Description", SPListTemplateType.GenericList);

   //Add Columns to SP List
   SPList newSPList = web.Lists[strListName];

   // create new column "User Name" and Field Type “Text”
   newSPList.Fields.Add("User Name", SPFieldType.Text, true);
   //creating lookup type column to this New List
   //Get information from the "Title" column from “Employee Roles” List
   SPList List2 = web.Lists["Employee Roles"];
   newSPList.Fields.AddLookup("Lookup Column", List2.ID, false);
   SPFieldLookup spLookup = (SPFieldLookup) newSPList.Fields["Lookup Column"];
   spLookup.LookupField = List2.Fields["Title"].InternalName;
   spLookup.Update();

   // Adding newly added column to default view
   SPView view = newSPList.DefaultView;
   view.ViewFields.Add("User Name");
   view.ViewFields.Add("Lookup Column");
   view.Update();
   newSPList.Update();
   web.AllowUnsafeUpdates = false;
  }
  catch
  {
    //Your Code
  }
  finally
  {
   site.Dispose();
 }
});
}

SharePoint List generation depends on the List Template type that we are specifying during list creation. one can create lists of type Custom Lists(Generic Lists), Document Library, Announcements, Picture Library, Tasks, Survey, Discussion Board, Contacts, Links, etc.

You can specify list template type in the below code
  lists.Add(strListName, "My Description", SPListTemplateType.GenericList);

Hope this information is useful for you.
Creating SharePoint Lists and Libraries using C#.Net Creating SharePoint Lists and Libraries using C#.Net Reviewed by Sudheer Gangumolu on November 28, 2014 Rating: 5

5 comments:

  1. what about content types and metadata?

    ReplyDelete
    Replies
    1. Hi, You can create and add Content types and Managed metadata properties to the List Programmatically. Please take a look at https://msdn.microsoft.com/en-us/library/ff798370.aspx and http://blogs.msdn.com/b/sharepointdev/archive/2012/04/16/programmatically-create-a-managed-metadata-list-column-mohammed-faizan.aspx

      Delete
  2. And, what about disposing SPSite/SPWeb in case when exception occurred? :)

    ReplyDelete
    Replies
    1. Thanks for your feedback, I have updated according by using try catch finally blocks :). And similarly you can also use "using" blocks.

      Delete
  3. Great work Sudheer keep writing best blogs on SharePoint 2013
    SharePoint 2013 Administrator Training

    ReplyDelete