Custom List Definition in SharePoint 2010

Written by Kerem Ozen. Posted in SharePoint

In this blog I’ll explain how to create a custom list definition in SharePoint 2010 that contains the following parts;
  • creation of custom fields and a content type for the list
  • creation of list definition
  • creation of a list instance
I’ll demonstrate a list definition for an abbreviation list which will have two columns, “Abbreviation” and “Full Form”. For abbreviation field I’ll rename the title field. So features like link and menu that are provided by title field will be applicable for abbreviation field. So let’s start by creating new SharePoint project. Open Visual Studio 2010 and create an Empty SharePoint Project. In the SharePoint Customization Wizard, type your SharePoint Server 2010 URL and select trust level depending on your needs. In the Solution Explorer, right-click the ListDefinitionProject and select Add and then New Item. In the Add New Item dialog box, choose the Content Type template and type AbbreviationContentType as the Name. Click Add. In the Choose Content Type Settings dialog box, choose Item in the Which base content type should this content type inherit from? drop-down list box. Click Finish. In the Elements.xml file of the content type, there are 3 parts you need to add/edit. First part is the custom field definition for content type. ID attribute is custom GUID for the field. You can generate new GUID by Visual Studio. You have to define the type of the custom field. In this case it is text. It is a good practice to give a group for custom fields. Since Abbreviation field will be the Title field of list, I’m not going to create it here. Second part of Elements.xml is the content type definition part. This part will be generated automatically when you add Content Type to your project. You may want to rephrase Name and Description attributes. Similar to the custom fields, you can give content type groups to custom content types. Third part of Elements.xml is referencing custom field in custom content type. Add FieldRef element in FieldRefs element for each field that you want to add to the content type. ID in FieldRef element must match with the ID of the field you’ve created. Now you need to create list definition. In the Solution Explorer, right-click the ListDefinitionProject and select Add and then New Item. In the Add New Item dialog box, choose the List Definition From Content Type template and type AbbreviationListDefinition as the Name. Click Add. In the Choose List Definition Settings dialog box, give a proper display name for list definition (in our case Abbreviation List Definition) and ensure that AbbreviationContentType is selected in the What content type would you like to use for your list definition? drop-down list box. Uncheck the Add a list instance for this list definition option if you don’t want to create an instance of list definition by feature activation. Click Finish. Open the Elements.xml file that was created. You can give proper decription for list template. Open Schema.xml file under AbbreviationListDefinition. First you have to add EnableContentTypes=”TRUE” to the list element. In order to rename Title field of the list with Abbreviation you need to add following elements in Fields element. Next you need to arrange default view of the list. Add FullForm field as FieldRef element to the default view element. You can deploy the project to your SharePoint and create lists from the custom list definition. UPDATE: This blog had to be updated according to the comments. I’d like to thank Ozan Bora Fikir for contribution. It’s a good practice to seperate the features of list instance and its definition. In this case you have to make sure that list definition feature is activated before the list instance feature. Cannot complete this action. Please try again! You might get this self-explanatory (!) exception while trying this example. Here is the solution that Ozan submitted.
This is a very helpful description for an exception, but I found root of this problem. First of all, we need to understand the behaviour of SharePoint for content types. Let’s assume that we create a custom site content type. We have to choose a base content type for out newly custom site content type. Further assume that you’re going to use your custom content type in a custom list definition. In your custom list definition you need to add to the list element EnableContentType=”TRUE” attribute. But your list do not directly use your site content type. It redefines your custom site content type as a “list content type”. Since you define a ContentTypeRef.   Now the problem, as in your example, is that you just rename a column of the your “list content type”. These are “Title” as “Abrreviation” and “LinkTitleNoMenu” as “Abbreviation”. This is the cause of the problem. “Title” column inherits from the Item Content Type. But SharePoint does not found your renamed field. So you have to refine your content type schema, modify the attribute Inherits=”False”.

Tags: , , , , , , ,

Trackback from your site.

Comments (11)

  • Ozan Bora FİKİR

    |

    Very nice post but I just want to discuss a little bit real world example. In most cases, you should separate the list instance from its definition. Consider the situation where you have to define a custom list definition across the site collection, or just in a site collection. In either cases what you have to do is simply bind the definition to a farm feature or site feature respectively. However, your list instance might have seen in at least two different site collection or sites in a site collection. Of course in this case you have to bind your list instance to a site feature or web feature respectively.
    The only thing that you have to care about is putting a feature dependency on the list instance feature to guarantee that the list definition feature is activated before the list instance feature.

    Reply

    • Kerem Ozen

      |

      You’re absolutely right. Thanks for additional information. I’m going to add this note to the post.

      Reply

  • Ozan Bora FİKİR

    |

    Today I have discovered a weird problem. Your example gives an error “Cannot complete this action. Please try again!”. This is a very helpful description for an exception, but I found root of this problem.

    First of all, we need to understand the behaviour of SharePoint for content types. Let’s assume that we create a custom site content type. We have to choose a base content type for out newly custom site content type. Further assume that you’re going to use your custom content type in a custom list definition. In your custom list definition you need to add to the list element EnableContentType=”TRUE” attribute. But your list do not directly use your site content type. It redefines your custom site content type as a “list content type”. Since you define a ContentTypeRef.

    Now the problem, as in your example, is that you just rename a column of the your “list content type”. These are “Title” as “Abrreviation” and “LinkTitleNoMenu” as “Abbreviation”. This is the cause of the problem. “Title” column inherits from the Item Content Type. But SharePoint does not found your renamed field. So you have to refine your content type schema, modify the attribute Inherits=”False”.

    Reply

  • Sid

    |

    Hi I have created a Content Definition the code for same is as follows:

    <!—->

    Part of Scema for List definition is:

    ————————————————-

    No Changes have been made in the rest of the code.
    Now my problem is , when i deploy my code and try to make a new list out of this definition i get the “Cannot complete this action. Please try again!” Error. On drilling down , i found that my content types are not present in the list that was created(Inspite of error).On manually adding the content type every thing works fine.But some how my custom content type is not getting attached to the created list. Please Help me out on this.
    Thanks in advance.

    Reply

    • Kerem Ozen

      |

      Hi,
      It seems like your content s not deployed. Please check whether your content type is in your package. You can also check the EnableContentType=”TRUE” attribute in your custom list definition. Hope these helps. If not, please send me your code package, I can try to find the problem.
      Thanks.

      Reply

  • cyril

    |

    Hi,

    I want to create sharepoint list definition using visual studio 2010, i created list definition first then i created content type, also given list data programmatically and its not affecting my sharepoint site.. please do the needful

    Reply

  • cyril

    |

    Hi

    am creating a list definition with name IssueTracking with the following columns

    IssueID should be uniqueId
    IssueDescription should be multi string
    Project should be string
    AssignedTo should people picker control
    Status(pending,resloved,closed) should be Choice

    Reply

  • cyril

    |

    Hi,
    This is my Schema.xml file

    Pending
    Resolved
    Closed

    This is my IssueContentType Elements.Xml

    Pending
    Resolved
    Closed

    this is my list instance Elements.Xml

    1001
    Create community
    Freescale
    cyril
    Resolved

    1002
    Display List using Gridview
    Freescale
    John
    Pending

    1003
    Create a DashBoard
    Freescale
    Mark
    Closed

    I have done this please let me know what i made is right or wrong..Please help me

    Reply

  • cyril

    |

    hi

    sorry i posted the entire program but its not visible to you..

    Reply

Leave a comment