Creating SharePoint 2010 List Definitions in Visual Studio 2010
Summary: Learn how to create list definitions and list instances for Microsoft SharePoint 2010 by using Microsoft Visual Studio 2010.
Last modified: September 12, 2012
Applies to: Office 2010 | SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2008 | Visual Studio 2010
Provided by: Ben Hedges, Point8020
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:
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
To modify the list instance Elements.xml file
To modify the list definition Elements.xml file
To modify the Schema.xml file
To deploy and test the project
List Definition Elements.xml In this example, the list definition Elements.xml file is edited so that is has a unique Type value. The Type value should be above 10,000 and unique in this feature. Type="10001" 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. AllowDeletion="FALSE" DisallowContentTypes="FALSE" 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.
<ContentType
ID="0x01004d360004c8074de6a25f4d8cdd3be8dd"
Name="Equipment Availability"
Group="Custom Content Type"
Description="Equipment Availability"
Version="0">
<FieldRefs>
<FieldRef ID="{d3a92d97-2b77-4a25-9698-3ab54874bc10}" />
<FieldRef ID="{d3a92d97-2b77-4a25-9698-3ab54874bc12}" />
</FieldRefs>
</ContentType>
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.
<Field ID="{d3a92d97-2b77-4a25-9698-3ab54874bc10}"
Name="EquipmentDescription"
Type="Note"
DisplayName="Equipment Description"
Required="FALSE"
NumLines="6">
</Field>
<Field ID="{d3a92d97-2b77-4a25-9698-3ab54874bc12}"
Name="EquipmentAvailable"
Type="Boolean"
DisplayName="Equipment Available"
Required="FALSE">
</Field>
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. EnableContentTypes="TRUE" Between the <Fields> </Fields> tags that appear near the top of the Schema.xml file, the following Field tags are required.
<Field ID="{d3a92d97-2b77-4a25-9698-3ab54874bc10}"
Name="EquipmentDescription" Type="Note"
DisplayName="Equipment Description"
Required="FALSE" NumLines="6">
</Field>
<Field ID="{d3a92d97-2b77-4a25-9698-3ab54874bc12}"
Name="EquipmentAvailable" Type="Boolean"
DisplayName="Equipment Available" Required="FALSE">
</Field>
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. <ContentTypeRef ID="0x01004d360004c8074de6a25f4d8cdd3be8dd" /> 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. <FieldRef Name="EquipmentDescription"></FieldRef> <FieldRef Name="EquipmentAvailable"></FieldRef> 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 Title of the list instance is appropriate for the site.
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<ListInstance Title="Equipment Availability"
OnQuickLaunch="TRUE"
TemplateType="10001"
Url="Lists/EquipmentAvailability-ListInstance1"
Description="My List Instance">
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.
<Data>
<Rows>
<Row>
<Field Name ="Title">Projector</Field>
<Field Name ="EquipmentDescription">
High Definition Projector with built-in speaker.
</Field>
<Field Name ="EquipmentAvailable">TRUE</Field>
</Row>
<Row>
<Field Name ="Title">Wired Microphone</Field>
<Field Name ="EquipmentDescription">
Dynamic Voice Microphone.
</Field>
<Field Name ="EquipmentAvailable">FALSE</Field>
</Row>
<Row>
<Field Name ="Title">Wireless Microphone System</Field>
<Field Name ="EquipmentDescription">
Two hand held microphones and VHF receiver system.
</Field>
<Field Name ="EquipmentAvailable">TRUE</Field>
</Row>
</Rows>
</Data>
|
Watch the video
Length: 00:06:09
About the Author
|
Ben Hedges is Senior Vice President, Research and Development at