SIGN UP MEMBER LOGIN:    
ARTICLE

Creating List Using List Definition - Code behind approach

Posted by Bhushan Gawale Articles | C# Language January 20, 2011
In this article you will learn how to create a list using list definition code behind approach.
Reader Level:


Let's have a look at how we can create a custom list by using our own list definition.

There is a <ListTemplate> element which we can use to easily create our own list definition. Andrew Connel has explained this very well in this post

But this is a different approach I wanted to use with our own list definitions.

After creating a list definition I didn't want to add a list instance to the solution because if we do, then it creates a list with based definition automatically when feature gets activated.

I wanted to have control over creation of this list which will be based on our custom list definition. Which puzzled me..

Then I found a way to achieve this by using a object model. I don't know yet If there are multiple ways to do this but this one was a perfect choice for me and worked very well.

When we create a list definition then we have a feature which provisions list definition.

Note that GUID used here in code is Feature Id of the feature which provisions the list definition.

Also please take into consideration that there are some methods in server object model available for creating a list using list templates directly, but this was something different than list templates, so this approach was preferred because List Templates are stored in the content database while list definitions resides on the actual file system of the server

try
{
  SPList list = null;
  using (SPSite site = new SPSite("http://yoursite/"))
  {
     using (SPWeb web = site.RootWeb)
     {
      //List Will be Created Based on this ListDefinition
- OOB Custom List Definition
      //00BFEA71-DE22-43B2-A848-C05709900100
 
        foreach (SPList _list in web.Lists)
        {
          if (_list.Title.Equals("TestList"))
          {
              list = _list;
          }
        }

        if (list == null)
        {
         web.AllowUnsafeUpdates = true;
         Guid listID = web.Lists.Add("TestList", //List Title
                      "This is Test List",      //List Description
                      "Lists/TestList",         //List Url
                      "00BFEA71-DE22-43B2-A848-C05709900100", //Feature Id of List definition Provisioning Feature - CustomList Feature Id
                      100,                     //List Template Type
                     "101");      //Document Template Type .. 101 is for None

           web.Update();
           web.AllowUnsafeUpdates = false;

         }
        }

     }
    }
    catch (Exception ex)
    {

    }
 

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor