Creating SharePoint Lists and Libraries Programmatically

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 Programmatically. 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 Programmatically.

 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.
SHARE

Sudheer Gangumolu

I'm a Software Analyst / Developer / Blogger, having good experience in working with different flavors & Customization of SharePoint (i.e.,SP O365 / 2013 / 2010 / 2007) and .Net Apps. Currently, working on SharePoint 2013 / SharePoint O365. Guiding people on my technologies.

    Blogger Comment
    Facebook Comment

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