Export (0) Print
Expand All
Expand Minimize

Creating SharePoint 2010 List Definitions in Visual Studio 2010

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

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

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

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

    <?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">
        <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>
      </ListInstance>
    </Elements>
    
    

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.

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <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>
      <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>
    
      <!-- Do not change the value of the Name attribute below. 
      If it does not match the folder name of the List Definition 
      project item, an error will occur when the project is run. -->
      <ListTemplate
            Name="ListDefinition1"
            Type="10001"
            AllowDeletion="FALSE"
            DisallowContentTypes="FALSE"
            BaseType="0"
            OnQuickLaunch="FALSE"
            SecurityBits="11"
            Sequence="410"
            DisplayName="Equipment Booking"
            Description="My List Definition"
    Image="/_layouts/images/itgen.gif"/>
    </Elements>
    
    

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.

    <?xml version="1.0" encoding="utf-8"?>
    <List xmlns:ows="Microsoft SharePoint" 
          EnableContentTypes="TRUE" 
          Title="Equipment Availability" 
          FolderCreation="FALSE" 
          Direction="$Resources:Direction;" 
          Url="Lists/EquipmentAvailability-ListDefinition1" 
          BaseType="0" 
          xmlns="http://schemas.microsoft.com/sharepoint/">
      <MetaData>
        <ContentTypes>
          <ContentTypeRef ID="0x01004d360004c8074de6a25f4d8cdd3be8dd" />
          <ContentTypeRef ID="0x01">
            <Folder TargetName="Item" />
          </ContentTypeRef>
          <ContentTypeRef ID="0x0120" />
        </ContentTypes>
        <Fields>
          <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>
        </Fields>
        <Views>
          <View BaseViewID="0" Type="HTML" MobileView="TRUE" 
                TabularView="FALSE" 
                MobileDefaultView="TRUE" >
            <Toolbar />
            <XslLink>main.xsl</XslLink>
            <ViewBody>
              <HTML><![CDATA[<tr class="]]></HTML>
              <GetVar Name="AlternateStyle" />
              <HTML><![CDATA["]]></HTML>
              <Switch>
                <Expr>
                  <GetVar Name="TabularView" />
                </Expr>
                <Case Value="1">
                  <HTML><![CDATA[ iid="]]></HTML>
                  <Counter Type="View" />
                  <HTML>,</HTML>
                  <Column Name="ID" HTMLEncode="TRUE" />
                  <HTML><![CDATA[,]]></HTML>
                  <LookupColumn Name="FSObjType" HTMLEncode="TRUE" />
                  <HTML><![CDATA["><td class="ms-vb-itmcbx">
                  <input type="checkbox" class="s4-itm-cbx"/></td>]]></HTML>
                </Case>
                <Default>
                  <HTML><![CDATA[>]]></HTML>
                </Default>
              </Switch>
              <IfEqual>
                <Expr1>
                  <GetVar Name="AlternateStyle" />
                </Expr1>
                <Expr2>ms-alternating</Expr2>
                <Then>
                  <SetVar Scope="Request" Name="AlternateStyle"></SetVar>
                </Then>
                <Else>
                  <SetVar Scope="Request" 
                          Name="AlternateStyle">ms-alternating</SetVar>
                </Else>
              </IfEqual>
              <Fields>
                <HTML><![CDATA[<td class="]]></HTML>
                <FieldSwitch>
                  <Expr>
                    <Property Select="ClassInfo" />
                  </Expr>
                  <Case Value="Menu">
                    <HTML>
                      <![CDATA[ms-vb-title" 
                      onmouseover="OnChildItem(this)" 
                      height="100%]]>
                    </HTML>
                  </Case>
                  <Case Value="Icon">ms-vb-icon</Case>
                  <Default>
                    <FieldSwitch>
                      <Expr>
                        <Property Select="Type" />
                        <PresenceEnabled />
                      </Expr>
                      <Case Value="UserTRUE">ms-vb-user</Case>
                      <Case Value="UserMultiTRUE">ms-vb-user</Case>
                      <Default>ms-vb2</Default>
                    </FieldSwitch>
                  </Default>
                </FieldSwitch>
                <HTML><![CDATA[">]]></HTML>
                <Field />
                <HTML><![CDATA[</td>]]></HTML>
              </Fields>
              <HTML><![CDATA[</tr>]]></HTML>
            </ViewBody>
            <RowLimit>20</RowLimit>
            <ViewFields>
              <FieldRef Name="LinkTitleNoMenu"></FieldRef>
              <FieldRef Name="EquipmentAvailable"></FieldRef>
            </ViewFields>
            <Query>
              <OrderBy>
                <FieldRef Name="Modified" Ascending="FALSE"></FieldRef>
              </OrderBy>
            </Query>
            <ParameterBindings>
              <ParameterBinding Name="AddNewAnnouncement" 
                                Location="Resource(wss,addnewitem)" />
              <ParameterBinding Name="NoAnnouncements" 
                                Location="Resource(wss,noXinviewofY_LIST)" />
              <ParameterBinding Name="NoAnnouncementsHowTo" 
                                Location="Resource(wss,noXinviewofY_ONET_HOME)" />
            </ParameterBindings>
          </View>
          <View BaseViewID="1" Type="HTML" 
                WebPartZoneID="Main" 
                DisplayName="$Resources:core,objectiv_schema_mwsidcamlidC24;" 
                DefaultView="TRUE" MobileView="TRUE" 
                MobileDefaultView="TRUE" 
                SetupPath="pages\viewpage.aspx" 
                ImageUrl="/_layouts/images/generic.png" 
                Url="AllItems.aspx">
            <Toolbar />
            <XslLink>main.xsl</XslLink>
            <ViewBody>
              <HTML><![CDATA[<tr class="]]></HTML>
              <GetVar Name="AlternateStyle" />
              <HTML><![CDATA["]]></HTML>
              <Switch>
                <Expr>
                  <GetVar Name="TabularView" />
                </Expr>
                <Case Value="1">
                  <HTML><![CDATA[ iid="]]></HTML>
                  <Counter Type="View" />
                  <HTML>,</HTML>
                  <Column Name="ID" HTMLEncode="TRUE" />
                  <HTML><![CDATA[,]]></HTML>
                  <LookupColumn Name="FSObjType" HTMLEncode="TRUE" />
                  <HTML><![CDATA["><td class="ms-vb-itmcbx">
                  <input type="checkbox" class="s4-itm-cbx"/></td>]]></HTML>
                </Case>
                <Default>
                  <HTML><![CDATA[>]]></HTML>
                </Default>
              </Switch>
              <IfEqual>
                <Expr1>
                  <GetVar Name="AlternateStyle" />
                </Expr1>
                <Expr2>ms-alternating</Expr2>
                <Then>
                  <SetVar Scope="Request" Name="AlternateStyle"></SetVar>
                </Then>
                <Else>
                  <SetVar Scope="Request" 
                          Name="AlternateStyle">ms-alternating</SetVar>
                </Else>
              </IfEqual>
              <Fields>
                <HTML><![CDATA[<td class="]]></HTML>
                <FieldSwitch>
                  <Expr>
                    <Property Select="ClassInfo" />
                  </Expr>
                  <Case Value="Menu">
                    <HTML><![CDATA[ms-vb-title" 
                    onmouseover="OnChildItem(this)" 
                    height="100%]]></HTML>
                  </Case>
                  <Case Value="Icon">ms-vb-icon</Case>
                  <Default>
                    <FieldSwitch>
                      <Expr>
                        <Property Select="Type" />
                        <PresenceEnabled />
                      </Expr>
                      <Case Value="UserTRUE">ms-vb-user</Case>
                      <Case Value="UserMultiTRUE">ms-vb-user</Case>
                      <Default>ms-vb2</Default>
                    </FieldSwitch>
                  </Default>
                </FieldSwitch>
                <HTML><![CDATA[">]]></HTML>
                <Field />
                <HTML><![CDATA[</td>]]></HTML>
              </Fields>
              <HTML><![CDATA[</tr>]]></HTML>
            </ViewBody>
            <RowLimit Paged="TRUE">30</RowLimit>
            <ViewFields>
              <FieldRef Name="Attachments"></FieldRef>
              <FieldRef Name="LinkTitle"></FieldRef>
              <FieldRef Name="EquipmentDescription"></FieldRef>
              <FieldRef Name="EquipmentAvailable"></FieldRef>
            </ViewFields>
            <Query>
              <OrderBy>
                <FieldRef Name="ID"></FieldRef>
              </OrderBy>
            </Query>
            <ParameterBindings>
              <ParameterBinding Name="NoAnnouncements" 
                                Location="Resource(wss,noXinviewofY_LIST)" />
              <ParameterBinding Name="NoAnnouncementsHowTo" 
                                Location="Resource(wss,noXinviewofY_DEFAULT)" />
            </ParameterBindings>
          </View>
        </Views>
        <Forms>
          <Form Type="DisplayForm" Url="DispForm.aspx" 
                SetupPath="pages\form.aspx" WebPartZoneID="Main" />
          <Form Type="EditForm" Url="EditForm.aspx" 
                SetupPath="pages\form.aspx" WebPartZoneID="Main" />
          <Form Type="NewForm" Url="NewForm.aspx" 
                SetupPath="pages\form.aspx" WebPartZoneID="Main" />
        </Forms>
      </MetaData>
    </List>
    

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="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>
See It

Watch the video

Watch video

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.

Show:
© 2014 Microsoft