List definitions and list instances that are based on those definitions are a fundamental part of Microsoft SharePoint 2010. Microsoft Visual Studio 2010 provides a project type that makes it easier to create list definitions in XML and to create list instances that are based on those definitions. In addition, tight integration with SharePoint 2010 enables you to deploy list definitions and list instances to a SharePoint site directly from Visual Studio 2010.
This SharePoint Visual How To describes the following steps for creating and deploying a list definition in Visual Studio 2010:
- Creating a SharePoint 2010 list definition application solution in Visual Studio 2010.
- Editing the list instances Elements.xml file to specify details for an instance of the list.
- Editing the list definition Elements.xml file to define the content type and fields that are displayed on the create page for items created in an instance of this list.
- Editing the list definition Schema.xml file to define the content and columns that appear in the views for this list.
In this example, you create a list definition for Equipment Availability that lets users create equipment items and specify whether the equipment is currently available.
To create a SharePoint 2010 list definition application solution in Visual Studio 2010
- Start Visual Studio 2010. On the File menu, click New, and then click Project.
- In the New Project dialog box, in the Installed Templates section, expand either Visual Basic orVisual C#, expand SharePoint, and then click 2010.
- In the template list, click List Definition.
- In the Name box at the bottom, type EquipmentAvailability.
- Leave the default values in the other fields, and click OK.
- Under What local site do you want to use for debugging?, select your site.
- Click Deploy as a farm solution, and then click Next.
- On the Choose List Definition Settings page, in the list definition display name box, type Equipment Availability.
- In the What is the type of the list definition? list, select Custom List.
- Ensure that the Add a list instance for this list definition check box is selected, and then click Finish.
To modify the list instance Elements.xml file
- In Solution Explorer, under the Listinstance1 node, open the Elements.xml file. Replace the contents of the Listinstance1\Elements.xml file with the following markup.
To modify the list definition Elements.xml file
- In Solution Explorer, under the ListDefinition1 node, open the Elements.xml file. Replace the contents of the ListDefinition1\Elements.xml file with the following markup.
To modify the Schema.xml file
- In Solution Explorer, under the ListDefinition1 node, open the Schema.xml file. Replace the contents of the ListDefinition1\Schema.xml file with the following markup.
To deploy and test the project
- In Solution Explorer, right-click the project, and then select Deploy.
- Open the SharePoint Home page.
- On the Quick Launch menu, click Equipment Availability. The items that are defined in the list instance appear.
- Visual Studio 2010 provides a project type that enables you to create list definitions and list instances from Visual Studio.
- A list definition enables site users to create new list instances based on the definition by selecting the definition in the Create dialog box on the SharePoint site.
- Optionally, you can create a list instance in Visual Studio at the same time as the list definition so that when the project is deployed, the instance is created based on the list definition.
List Definition Elements.xml
In this example, the list definition Elements.xml file is edited so that is has a unique Type value. The Typevalue should be above 10,000 and unique in this feature.
You must add the following attributes to the List Elements XML tag to enable content types and disable the deletion of lists that are created based on this definition.
The Equipment Availability content type is added to the definition. It specifies the parent item content type and has a unique content ID that is created by concatenating the parent ID and a GUID.
For more information about creating IDs for content types, see Content Type IDs.
Within the content type, you add FieldRef tags with GUIDs that identify columns that will be added to those already provided by the parent content type. The GUIDs for each field are generated by the developer. The detail for these columns is added in the same file.
It is essential that the GUIDs match those that are provided in the ContentType in the previous step. The additional attributes specify the field type and on-screen behavior.
List Definition Schema.xml
The List Definition schema has the following attribute added to it.
Between the <Fields> </Fields> tags that appear near the top of the Schema.xml file, the following Field tags are required.
These fields match those that were added to the Elements.xml file.
The schema file must also have a reference to the content type that is required for this list. In the <ContentTypes> element, you must reference the Content Type ID as follows.
The Schema.xml file also contains the definition for view fields that are used when you view the list on the site. In the <ViewFields> tag in the DefaultView section, add the following lines.
List Instance Elements.xml
You must edit the list instances Elements.xml file so that is has a unique Template Type, and so that the Titleof the list instance is appropriate for the site.
You configure the list instance so that it appears on the Quick Launch menu. This overrides the settings on the list definition for this list instance. You can add data with the list instance by adding XML to the list instance XML file. The following XML adds three rows of data to the list.