Sunday, 17 February 2013

Create Custom List Definition using custom Content Type using .Net in SharePoint 2010



Please follow the link to create custom content type & custom site columns.


After creating custom content type it is now time to create a custom list definition & bind the custom content type to this List Definition so that on creating a list using the custom List Definition, the default Content type of this list should be our custom content type created & the site columns attached with it.

    1.  In the given solution we need to add a List Definition to create a custom List Template. Right Click on the Solution to add a new List Definition as shown below.

     2.   Now select the List Definition & give the List Definition a proper name & click add as shown below.

     3.  Now the popup will appear, provide the appropriate Display Name to the List Definition & select the type of list definition we want to create to add the default content type. Here select aye type as later on we will remove its default content type & add our custom content type & click “Finish” as shown below.

     4.  The Default solution will look as shown below

Now as we can see a default List Instance is created in the List Definition, we will give it a proper name & URL as shown below

      5.  Now open the Schema.XML file of the List Definition. We can see the default code which binds the default content type & its columns as shown below

    6.  Now in this solution at the beginning we can see the <ContentType></ContentaType> tag. It contains the default content type defined with its ID. Now we will comment the existing content type & insert our custom content type with ite appropriate ID as given below

<!--Define the Custom Content Type to the List Defination(List Template)with its ID-->
<ContentTypeRef ID="0x01004c89ffc4ed8242a886ff715d9da4f04d" />

The ID here is the ID of the Custom Content Type which we can get from the Elements.XML file of that particular Content type. Comment the default content type as shown below.

    7.  Now below in the Schema.XML file we can find the <ViewFields></ViewFields> which displays the default View. The columns defined here will be displayed in the default view.

     8.  Now in this <ViewFields></ViewFields> tag we will add our custom fields defined in the custom content type.

<FieldRef Name="DemoName"></FieldRef>
   <FieldRef Name="DemoValue"></FieldRef>
   <FieldRef Name="DemoChoiceType"></FieldRef>
   <FieldRef Name="DemoAmount"></FieldRef>
   <FieldRef Name="DemoDate"></FieldRef>
   <FieldRef Name="DemoLookupField"></FieldRef>

      The solution will look as shown below

    9.  Now build & deploy the solution in your SharePoint Web Application. After activating the feature the custom site columns will get created, also the Custom Content type is created & a default list with given List Definition is created.
      10.  Now to check whether our custom List Template is available for us to create list using this list template go the SharePoint site & click More Options in Site Actions. Filter by List from Right side Navigation. Here we can see our custom List template as shown below

No comments:

Post a Comment