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