How to: Create a Custom List Definition
Applies to: SharePoint Foundation 2010
This task demonstrates how to create a custom list template Feature by using SharePoint development tools in Microsoft Visual Studio 2010. The best practice is to create a content type and base a list definition on that content type.
Create the Field Definition
A field definition defines the columns that appear inside of the list template. For more information about the XML that is used to define a field type, see Field Types XML.
To create the field definition
Start Microsoft Visual Studio 2010.
On the File menu, point to New, and then click Project.
In Project Types, under Visual Basic or C#, select Empty SharePoint Project.
Type FavoriteColorList as the project name. Click OK.
In the SharePoint Customization Wizard, choose Deploy as a sandboxed solution. Click Finish.
In the Solution Explorer, right-click the FavoriteColorList project and select Add and then New Item.
In the Add New Item dialog box, choose the Empty Element template and type FavoriteColorField as the Name. Click Add.
Insert the following XML inside the Elements element. This XML defines a field for the content type.
Important
You must replace FieldId in the ID attribute with a GUID. To generate a GUID, click Tools and then click Create GUID.
<Field ID="FieldId" Type="Choice" Name="FavoriteColor" DisplayName="Favorite Color" ShowInDisplayForm="TRUE" ShowInNewForm="TRUE" ShowInEditForm="TRUE"> <CHOICES> <CHOICE>Red</CHOICE> <CHOICE>Green</CHOICE> <CHOICE>Blue</CHOICE> <CHOICE>Purple</CHOICE> <CHOICE>Magenta</CHOICE> </CHOICES> </Field>
Create the Content Type
A content type defines a collection of items to use inside of the list. For more information about content types, see Content Types.
To create the content type
In the Solution Explorer, right-click the FavoriteColorList project and select Add and then New Item.
In the Add New Item dialog box, choose the Content Type template and type FavoriteColorContentType 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 FieldRefs element, you must add a FieldRef element that references the field that you previously defined. The following XML adds a reference to the field.
Important
The ID attribute of the FieldRef element must match the ID attribute of the Field element.
<FieldRef ID="FieldId" Name="FavoriteColor" />
Create the List Definition from the Content Type
To create the list definition from the content type
In the Solution Explorer, right-click the FavoriteColorList project and select Add and then New Item.
In the Add New Item dialog box, choose the List Definition From Content Type template and type FavoriteColorListDefinition as the Name. Click Add.
In the Choose List Definition Settings dialog box, ensure that FavoriteColorContentType is selected in the What content type would you like to use for your list definition? drop-down list box.
Check the Add a list instance for this list definition option. Click Finish.
Next, open the Schema.xml file that was created.
In the ViewFields element, add the same FieldRef element that you added to the content type. The following XML adds a reference to the field.
Important
The ID attribute of the FieldRef element must match the ID attribute of the Field element.
<FieldRef ID="FieldId" Name="FavoriteColor" />
Deploy the List Definition
To deploy the list definition
Press F5 to deploy the solution.
When the browser is open, click FavoriteColorList - ListInstance1 in the left navigation pane.
After the list opens, observe the Favorite Color field that is now visible. Click Add new item.
Observe the Favorite Color field in the form.
See Also
Concepts
Using Features in SharePoint Foundation