Partilhar via


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

  1. Start Microsoft Visual Studio 2010.

  2. On the File menu, point to New, and then click Project.

  3. In Project Types, under Visual Basic or C#, select Empty SharePoint Project.

  4. Type FavoriteColorList as the project name. Click OK.

  5. In the SharePoint Customization Wizard, choose Deploy as a sandboxed solution. Click Finish.

  6. In the Solution Explorer, right-click the FavoriteColorList project and select Add and then New Item.

  7. In the Add New Item dialog box, choose the Empty Element template and type FavoriteColorField as the Name. Click Add.

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

  1. In the Solution Explorer, right-click the FavoriteColorList project and select Add and then New Item.

  2. In the Add New Item dialog box, choose the Content Type template and type FavoriteColorContentType as the Name. Click Add.

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

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

  1. In the Solution Explorer, right-click the FavoriteColorList project and select Add and then New Item.

  2. In the Add New Item dialog box, choose the List Definition From Content Type template and type FavoriteColorListDefinition as the Name. Click Add.

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

  4. Check the Add a list instance for this list definition option. Click Finish.

  5. Next, open the Schema.xml file that was created.

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

  1. Press F5 to deploy the solution.

  2. When the browser is open, click FavoriteColorList - ListInstance1 in the left navigation pane.

  3. After the list opens, observe the Favorite Color field that is now visible. Click Add new item.

  4. Observe the Favorite Color field in the form.

See Also

Concepts

List Template Files

Using Features in SharePoint Foundation

Site Types: WebTemplates and Site Definitions

Understanding Schema.xml Files