Condividi tramite


How to: Create a Custom List Definition

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

This task demonstrates how to create a custom list template feature and add it to the selection of lists available for creating on the Create Page. Adding a custom list feature requires that you provide at least the following three files:

  • Feature.xml registers the list template.

  • Elements.xml adds the list template to the Create Page.

  • Schema.xml defines the list schema, which includes content type associations, field and view definitions, and form and toolbar references.

Procedures

To add a custom list definition to a Web site

  1. Create a folder in Local_Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATES\FEATURES and give it an appropriate name, such as SimpleListFeature.

  2. In the new folder create a Feature.xml file that specifies the title, ID, and scope for the Feature, as well as the location of an Elements.xml file, such as follows:

    <Feature 
      Title="Simple List Feature" 
      Id="80E23615-3145-4f43-BD5E-8524C02BD6CD" 
      Scope="Web" 
      xmlns="http://schemas.microsoft.com/sharepoint/">
      <ElementManifests>
        <ElementManifest Location="Elements.xml" />
      </ElementManifests>
    </Feature>
    
  3. To replace the GUID placeholder in the previous Id attribute, generate a GUID by running guidgen.exe, located in Local_Drive:\Program Files\Microsoft Visual Studio 8\Common7\Tools.

  4. In the new folder create an Elements.xml file that identifies the list template and specifies information to display on the Create Page. Give it a Type value that is above 10000 and different from any other custom list definitions used in your deployment:

    <Elements 
      xmlns="http://schemas.microsoft.com/sharepoint/">
      <ListTemplate 
        Name="SimpleList" 
        DisplayName="Simple List" 
        Type="10001" 
        Description="This is my simple list." 
        BaseType="0" 
        OnQuickLaunch="FALSE" 
        SecurityBits="11" />
    </Elements>
    
  5. Create a subfolder that has the same name as that assigned to the list template in the previous step, for example, SimpleList.

  6. Create a Schema.xml file in the new subfolder to define the list. You can copy and modify the Schema.xml file of an existing list Feature to define special fields for a custom list definition.

  7. At a command prompt, type the following commands to install the Feature in the deployment, and then activate the Feature on a specified subsite.

       a. stsadm -o installfeature -filename SimpleListFeature\Feature.xml
       b. stsadm -o activatefeature -name SimpleListFeature -url http://Server/Site/Subsite
    
  8. To test the new Feature, navigate to the Create Page and create a list through the template.

Schema.xml File

Define any custom fields that are required for your list near the beginning of the Schema.xml file. The following example, which copies and modifies the Schema.xml file of the default CustomList Feature, defines a special Choice field for the SimpleList Feature and adds this field to the Item content type so that the field is displayed in item forms.

To customize the Item content type definition, find the following content type declaration near the beginning of your copied Schema.xml file:

<ContentTypeRef ID="0x01">
  <Folder TargetName="Item" />
</ContentTypeRef>

Replace this content type declaration with the Item content type definition whose ID equals 0X01 in the ctypeswss.xml file that is located at C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\ctypes.

Add a reference for your custom field to the content type definition, which in this example is called FavoriteColor. Also include the default Folder element to specify the resource folder for the content type. Create a unique ID for your content type as described in Content Type IDs. Use guidgen.exe as describe previously in step 3 to create GUIDs for the content type ID and for the field reference.

<List Name="SimpleList" Title="Simple List" BaseType="0" Direction="" Url="">
  <MetaData>
    <ContentTypes>

  <!--Copy the referenced ContentType definition from ctypeswss.xml.-->
      <ContentType 
        ID="0x010099FE4F3ACD494e30A36693F9EE65BAF2"
        Name="FavoriteColor Item"
        Group="$Resources:List_Content_Types"
        Description="Favorite color item content type."
        Version="0">
        <FieldRefs>
          <FieldRef 
            ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" 
            Name="Title" 
            Required="TRUE" 
            ShowInNewForm="TRUE" 
            ShowInEditForm="TRUE"/> 

          <!--Add a field reference for the custom field.-->
          <FieldRef
            ID="{AD22016D-BC8B-4631-A0A3-5E84C6CFA7EC}" 
            Name="FavoriteColor" 
            Required="TRUE" 
            ShowInNewForm="TRUE" 
            ShowInEditForm="TRUE"/>

        </FieldRefs>
        <XmlDocuments>
          <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
            <FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
              <Display>ListForm</Display>
              <Edit>ListForm</Edit>
              <New>ListForm</New>
            </FormTemplates>
          </XmlDocument>
        </XmlDocuments>

        <!--Include the relative folder path for the content type resource folder.-->
        <Folder 
          TargetName="Item" />
      </ContentType>

      <ContentTypeRef 
        ID="0x0120" />
    </ContentTypes>

In the empty Fields tag that follows the ContentTypes section of Schema.xml, add a field definition for the FavoriteColor Choice field, as follows:

    <Fields>
      <Field 
        Type="Choice" 
        Name="FavoriteColor" 
        DisplayName="Favorite Color">
        <CHOICES>
          <CHOICE>Red</CHOICE>
          <CHOICE>Green</CHOICE>
          <CHOICE>Blue</CHOICE>
        </CHOICES>
      </Field>
    </Fields>
    <Views>
      ...

Add a reference to the custom field in the ViewFields section of a view definition so that the field is displayed in the view. The following example adds a FavoriteColor field reference to the standard all items view.

<ViewFields>
  <FieldRef Name="Attachments"/>
  <FieldRef Name="LinkTitle"/>
  <FieldRef Name="FavoriteColor"/>
</ViewFields>

See Also

Concepts

List Template Files

Working with Features

Working with Site Templates and Definitions

Schema.xml