Creating SharePoint 2010 List Definitions in Visual Studio 2010

SharePoint Visual How To

Summary:  Learn how to create list definitions and list instances for Microsoft SharePoint 2010 by using Microsoft Visual Studio 2010.

Applies to: Office 2010 | SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2008 | Visual Studio 2010

Provided by:  Ben Hedges, Point8020

Overview

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.

Code It

This SharePoint Visual How To describes the following steps for creating and deploying a list definition in Visual Studio 2010:

  1. Creating a SharePoint 2010 list definition application solution in Visual Studio 2010.

  2. Editing the list instances Elements.xml file to specify details for an instance of the list.

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

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

  1. Start Visual Studio 2010. On the File menu, click New, and then click Project.

  2. In the New Project dialog box, in the Installed Templates section, expand either Visual Basic or Visual C#, expand SharePoint, and then click 2010.

  3. In the template list, click List Definition.

  4. In the Name box at the bottom, type EquipmentAvailability.

  5. Leave the default values in the other fields, and click OK.

  6. Under What local site do you want to use for debugging?, select your site.

  7. Click Deploy as a farm solution, and then click Next.

  8. On the Choose List Definition Settings page, in the list definition display name box, type Equipment Availability.

  9. In the What is the type of the list definition? list, select Custom List.

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

To modify the list definition Elements.xml file

To modify the Schema.xml file

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.

Read It
  • 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 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="https://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>
See It

Watch the video

> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/5dc5a521-3451-4445-a829-ad5a9339642c]

Length: 00:06:09

Click to grab code

Grab the Code

Explore It

About the Author

Community Contributor  Ben Hedges is Senior Vice President, Research and Development at Point8020. Ben is particularly interested in modeling Talent Management, Compliance, and Learning and Development solutions on SharePoint. Ben has a wealth of experience fulfilling customer requirements by using Microsoft technologies.