Creating List Definitions with Custom List Columns for SharePoint Server 2007

Summary: Learn to use Visual Studio 2008 Extensions for Windows SharePoint Services to create a SharePoint list definition that implements custom list columns. (13 printed pages)

Community Member Icon Joel Krist, iSoftStone

December 2009

Applies to: Microsoft Office SharePoint Server 2007, Windows SharePoint Services 3.0

Contents

  • Introduction to Creating List Definitions with Custom List Columns

  • Adding Custom List Columns to the List Definition

  • Adding a Content Type to the List Definition

  • Adding Columns to the List Definition Default View

  • Deploying the List Definition

  • Conclusion

  • Additional Resources

Introduction to Creating List Definitions with Custom List Columns

Microsoft Office SharePoint Server 2007 supports the creation of custom list templates for those situations where the base list definitions do not provide the required functionality. Custom list templates are developed and deployed as list definitions that define the fields, views, forms, toolbar, and associated content types for the list.

This article walks you through using Microsoft Visual Studio 2008 Extensions for Windows SharePoint Services to create a SharePoint list definition, which is then modified to support custom columns that show a variety of column types. The end result is a list definition that contains custom list columns and a modified default view that can be used as a simple test harness for creating and testing additional column definitions. This article breaks the process down into the following steps:

  1. Creating a list definition project in Visual Studio.

  2. Adding custom list columns to the list definition.

  3. Adding a custom content type to the list definition.

  4. Modifying the default view of the list definition.

  5. Deploying the list definition to a SharePoint site.

The steps in this article assume that the development computer is running Windows SharePoint Services 3.0, and that Visual Studio 2008 Extensions for Windows SharePoint Services 3.0, Version 1.3 has been installed. However, the same basic process could be used to create a list definition with custom list columns by using Windows SharePoint Services 3.0 Tools: Visual Studio 2008 Extensions, Version 1.2 or Windows SharePoint Services 3.0 Tools: Visual Studio 2005 Extensions, Version 1.1.

Creating a List Definition Project in Visual Studio

The following are the steps to create a list definition project in Microsoft Visual Studio.

To create a SharePoint list definition in Visual Studio

  1. Start Visual Studio.

  2. On the File menu, click New, and then click Project. In the New Project dialog box, on the Project Types pane, choose Visual C# or Visual Basic, and then select the SharePoint category.

  3. On the Templates pane, click List Definition. Type CustomColumnsList for the project Name, enter a value for the project Location, and then click OK. Visual Studio opens the List Definition Settings dialog box, as shown in Figure 1.

    Figure 1. New list definition project

    New list definition project

  4. In the List Definition Settings dialog box, select Custom List for the Base List Definition and select the Create an instance of this list option, as shown in Figure 2.

    Figure 2. List Definitions Settings dialog box

    List Definitions Settings dialog box

  5. Click OK to close the dialog box and have Visual Studio generate the List Definition project.

The next four steps are not required but help to keep things neat in the Visual Studio solution and to make the name, title, description, and URL of the custom list instance and template more accurate and meaningful than the defaults generated by Visual Studio.

To change the defaults for the project

  1. In the Visual Studio Solution Explorer, rename the ListDefinition1 folder that contains the list definition solution files to Custom Columns List Definition (see Figure 3).

    Figure 3. Rename the List Definition folder

    Rename List Definition folder

  2. In Solution Explorer, open the Instance.xml file that defines the attributes of the list instance feature, and change the values of the ListInstance element's Title and Url attributes to Custom Columns List and Lists/CustomColumnsList (see Figure 4). Save the file.

    Figure 4. Edit Instance.xml

    Edit Instance.xml

  3. In Solution Explorer, open the ListDefinition.xml file that defines the attributes of the list template and change the values of the ListTemplate element's Name, DisplayName, and Description attributes to something more meaningful than the defaults (see Figure 5). Save the file.

    Figure 5. Edit ListDefinition.xml

    Edit ListDefinition.xml

  4. In Solution Explorer, open the Schema.xml file and add a DefaultDescription element as a child of the MetaData element. Set the value of the DefaultDescription element to something meaningful and then save the file (see Figure 6).

    Figure 6. Edit Schema.xml

    Edit Schema.xml

Adding Custom List Columns to the List Definition

The following XML uses the Field element to provide definitions for several different types of columns and shows concepts such as calculated columns and computed columns. To add the columns to the list definition, edit the Schema.xml file and add the following field definitions to the Fields element.

<!-- Single Line of Text Columns -->

<Field ID="{E15F4D45-C76A-458c-91DC-1914BA02B5F3}"
  Name="CustomSingleLineOfText"
  DisplayName="Single Line of Text"
  Group="Custom Fields"
  Type="Text"
  Description ="A required single line of text field with a default value and a maximum length of 50."
  Required="TRUE"
  MaxLength="50"
  ShowInNewForm="TRUE"
  ShowInEditForm="TRUE">
  <DefaultFormula>=&quot;Hello &quot;&amp;&quot;World&quot;</DefaultFormula>
</Field>

<Field ID = "{6EB7313B-B05F-4159-A7B1-FE3678369C15}"
  Name="CustomCalculatedText"
  DisplayName="Calculated Text"
  Group="Custom Fields"
  Type="Calculated"
  Description="A calculated text field based on Custom Single Line of Text."
  Required="FALSE"
  ResultType="Text"
  ShowInNewForm="TRUE"
  ShowInEditForm="TRUE">
  <Formula>=[CustomSingleLineOfText]</Formula>
  <FieldRefs>
    <FieldRef ID="{2D6B3E65-9015-4ee4-96C0-74B2B927A738}" Name="CustomSingleLineOfText"/>
  </FieldRefs>
</Field>

<!-- Choice Columns -->
      
<Field ID = "{830D342E-E41B-4f00-8285-7B2A33BD31B6}"
  Name="CustomMultiChoice1"
  DisplayName="Multi-Choice with No Edit"
  Group="Custom Fields"
  Type="MultiChoice"
  Description="A multi-choice field with FillInChoice set to FALSE."
  FillInChoice="FALSE"
  Required="FALSE">
  <CHOICES>
    <CHOICE>North</CHOICE>
    <CHOICE>South</CHOICE>
    <CHOICE>East</CHOICE>
    <CHOICE>West</CHOICE>
  </CHOICES>
  <Default>North</Default>
</Field>

<Field ID = "{BB66D2E7-712D-427a-AF98-D12770E9EB95}"
  Name="CustomMultiChoice2"
  DisplayName="Multi-Choice with Edit"
  Group="Custom Fields"
  Type="Choice"
  Description="A required drop-down, multi-choice field with FillInChoice set to TRUE."
  FillInChoice="TRUE"
  Required="TRUE"
  Format= "Dropdown">
  <CHOICES>
    <CHOICE>10-30</CHOICE>
    <CHOICE>41-60</CHOICE>
    <CHOICE>71-90</CHOICE>
    <CHOICE>91+</CHOICE>
  </CHOICES>
  <Default>10-30</Default>
</Field>
      
<Field ID = "{E1C05F69-973E-46e3-B52E-625FB267651D}"
  Name="CustomChoice"
  DisplayName="Radio Button Choice"
  Group="Custom Fields"
  Type="Choice"
  Description="A radio button choice field."
  Required="FALSE"            
  Format= "RadioButtons">
  <CHOICES>
    <CHOICE>Blue</CHOICE>
    <CHOICE>Green</CHOICE>
    <CHOICE>White</CHOICE>
    <CHOICE>Red</CHOICE>
  </CHOICES>
</Field>

<Field ID = "{FEF30AE7-F252-4ed0-B3D2-A220BF272C24}"
  Name="CustomCalculatedChoice"
  DisplayName="Calculated Sub Field"
  Group="Custom Fields"
  Type="Calculated"
  Description="A calculated field based on Custom Multi-Choice with Edit."
  Required="FALSE"
  ResultType="Text">
  <Formula>="The selected age range is: " &amp;[CustomMultiChoice2]</Formula>
  <FieldRefs>
    <FieldRef ID="{BB66D2E7-712D-427a-AF98-D12770E9EB95}" Name="CustomMultiChoice2"/>
  </FieldRefs>
</Field>

<!-- Number Columns -->
      
<Field ID="{BAC1B985-2AE5-4a3b-A163-09FC4BE62E0B}"
  Name="CustomNumber1"
  DisplayName="Number Between 0 and 99 No Decimals"
  Group="Custom Fields"
  Type="Number"
  Description="A required custom number field with a minimum of 0 
  and a maximum of 99 with no decimals displayed."
  Required="TRUE"   
  Decimals="0"
  Min="0"
  Max="99"
  Percentage="FALSE">
</Field>

<Field ID="{ACC27183-AFBF-484b-8212-0A398F41AED8}"
  Name="CustomNumber2"
  DisplayName="Number Between 1 and 10 With Decimals"
  Group="Custom Fields"
  Type="Number"
  Description="A required custom number field with a minimum of 1 
  and a maximum of 10 with decimals displayed."
  Required="TRUE"
  Min="1"
  Max="10"
  Percentage="FALSE">
</Field>        
  
<Field ID="{282306F7-F833-4985-A603-58F5AD5E3AE9}"
  Name="CustomCalculatedNumber"
  DisplayName="Calculated Number"
  Group="Custom Fields"
  Type="Calculated"
  Description="A calculated field based on Custom Number Between 0 and 99."       
  ResultType="Number">
  <Formula>=[CustomNumber1] + 4</Formula>
  <FieldRefs>
    <FieldRef ID="{BAC1B985-2AE5-4a3b-A163-09FC4BE62E0B}" Name="CustomNumber1"/>
  </FieldRefs>
</Field>

<!-- Currency Columns-->
      
<Field ID="{9B0438B3-16A9-4385-8426-D56C92336129}"
  Name="CustomCurrency1"
  DisplayName="Currency Using Turkish Locale"       
  Group="Custom Fields"
  Type="Currency"
  Description="A custom currency field using the LCID of 1055 for the Turkish locale."
  Required="FALSE"             
  LCID="1055">
</Field>

<Field ID="{62576F23-3C54-4706-BDB2-DFCDAACD8A62}"
  Name="CustomCurrency2"
  DisplayName="Currency Using Italian Locale"
  Group="Custom Fields"
  Type="Currency"
  Description="A custom currency field using the LCID of 1040 for the Italian locale."
  Required="TRUE"
  LCID="1040">
</Field>
              
<Field ID="{9F9D2708-79A6-4a3d-9C9A-7E9ECFD5E9BD}"
  Name="CustomCalculatedCurrency"
  DisplayName="Calculated Currency"
  Group="Custom Fields"
  Type="Calculated"
  Description="A calculated currency field based on Custom Currency 
  Using Italian Locale and Custom Number Between 1 and 10 With Decimals."
  Required="FALSE"
  LCID="1033"
  ResultType="Currency">
  <Default>1.53679</Default>
  <Formula>=[CustomCurrency2] * [CustomNumber2]</Formula>
  <FieldRefs>
    <FieldRef ID="{62576F23-3C54-4706-BDB2-DFCDAACD8A62}" Name="CustomCurrency2"/>
  </FieldRefs>
</Field>

<!-- Date Columns -->

<Field ID="{64F842AB-9CC6-4904-B41D-AC1A526B47D7}"
  Name="CustomDate"
  DisplayName="Date"
  Group="Custom Fields"
  Type="DateTime"              
  Description="A required date column."
  Required="TRUE"
  StorageTZ="UTC" >
  <Default>=Today</Default>
</Field>

<Field ID="{E5D8FCE8-2236-4820-AB29-8A40F93FF313}"
  Name="CustomCalculatedDate"
  DisplayName="Calculated Date"
  Group="Custom Fields"
  Type="Calculated"
  Description="A calculated read-only date column that adds 1 hour 
  to the value in Custom Date."
  Format="DateTime"
  ResultType="DateTime"
  ReadOnly="TRUE"
  AllowDeletion="TRUE">
  <Formula>=[CustomDate] + 0.0416666</Formula>
  <FieldRefs>
    <FieldRef ID="{64F842AB-9CC6-4904-B41D-AC1A526B47D7}" Name="CustomDate"/>
  </FieldRefs>
</Field>

<!-- URL Column -->

<Field ID="{98C36F20-3F3B-43c9-8502-7DF16E0F1404}"
  Name="CustomURL"
  DisplayName="URL"
  Group="Custom Fields"
  Type="URL"
  Description="A required sealed URL column with a default value."
  Sealed="TRUE"
  AllowDeletion="TRUE"
  Format="Hyperlink"
  Required="TRUE">
  <Default>https://www.microsoft.com</Default>
</Field>
      
<!-- Computed Column -->

<Field ID="{8988B0FD-D51D-436e-AF6E-2760F18A3F4F}"
  Name="CustomComputedURL"
  DisplayName="Computed URL"
  Group="Custom Fields"
  Type="Computed"
  Description="A computed column based on the values in Custom URL 
  and Custom Single Line of Text."
  ReadOnly="TRUE">
  <FieldRefs>
    <FieldRef ID="{98C36F20-3F3B-43c9-8502-7DF16E0F1404}" Name="CustomURL" />
  </FieldRefs>
  <DisplayPattern>
    <HTML><![CDATA[<a href="]]></HTML>
    <Column Name="CustomURL"/>
    <HTML><![CDATA[">]]></HTML>
    <Column Name="CustomSingleLineOfText"/>
    <HTML><![CDATA[</a>]]></HTML>
  </DisplayPattern>
</Field>

Figure 7 shows a partial listing of the custom column Field elements added to the Schema.xml file.

Figure 7. Field elements for custom columns

Field elements for custom columns

Adding a Content Type to the List Definition

Edit the Schema.xml file and replace the contents of the list definition's ContentTypes element with the following ContentType element to enable the previously defined columns to be displayed on the list item View, Edit, and New pages. The following XML defines a content type named Custom Column List Item that is based on the SharePoint Item content type, as specified by the base 0x01 part of the content type ID. For more information about creating content type IDs, see Content Type IDs.

<ContentType ID="0x010096D7CB5CF2EE4804B70397A9A7F24B69"
  Name="Custom Column List Item"
  Group="Custom Columns Content Type"
  Description="Create a new item to test custom list columns."
  Version="0">
  <FieldRefs>
    <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" 
    Name="Title" DisplayName="Title" Sealed="TRUE"/>

    <!-- Single Line of Text Columns -->
    <FieldRef ID="{E15F4D45-C76A-458c-91DC-1914BA02B5F3}" 
    Name="CustomSingleLineOfText" Required="TRUE" />
    <FieldRef ID="{6EB7313B-B05F-4159-A7B1-FE3678369C15}" 
    Name="CustomCalculatedText" Required="FALSE" />

    <!-- Choice Columns -->
    <FieldRef ID="{830D342E-E41B-4f00-8285-7B2A33BD31B6}" 
    Name="CustomMultiChoice1" Required="FALSE" />
    <FieldRef ID="{BB66D2E7-712D-427a-AF98-D12770E9EB95}" 
    Name="CustomMultiChoice2" Required="TRUE" />
    <FieldRef ID="{E1C05F69-973E-46e3-B52E-625FB267651D}" 
    Name="CustomChoice" Required="FALSE" />
    <FieldRef ID="{FEF30AE7-F252-4ed0-B3D2-A220BF272C24}" 
    Name="CustomCalculatedChoice" Required="FALSE" />

    <!-- Number Columns -->
    <FieldRef ID="{BAC1B985-2AE5-4a3b-A163-09FC4BE62E0B}" 
    Name="CustomNumber1" Required="TRUE" />
    <FieldRef ID="{ACC27183-AFBF-484b-8212-0A398F41AED8}" 
    Name="CustomNumber2" Required="TRUE" />
    <FieldRef ID="{282306F7-F833-4985-A603-58F5AD5E3AE9}" 
    Name="CustomCalculatedNumber" Required="FALSE" />

    <!-- Currency Columns-->
    <FieldRef ID="{9B0438B3-16A9-4385-8426-D56C92336129}" 
    Name="CustomCurrency1" Required="FALSE" />
    <FieldRef ID="{62576F23-3C54-4706-BDB2-DFCDAACD8A62}" 
    Name="CustomCurrency2" Required="TRUE" />
    <FieldRef ID="{9F9D2708-79A6-4a3d-9C9A-7E9ECFD5E9BD}" 
    Name="CustomCalculatedCurrency" Required="FALSE" />

    <!-- Date Columns -->
    <FieldRef ID="{64F842AB-9CC6-4904-B41D-AC1A526B47D7}" 
    Name="CustomDate" Required="TRUE" />
    <FieldRef ID="{E5D8FCE8-2236-4820-AB29-8A40F93FF313}" 
    Name="CustomCalculatedDate" Required="FALSE" />
   
    <!-- URL Columns -->
    <FieldRef ID="{98C36F20-3F3B-43c9-8502-7DF16E0F1404}" 
    Name="CustomURL" Required="TRUE" />

    <!-- Computed Columns -->
    <FieldRef ID="{8988B0FD-D51D-436e-AF6E-2760F18A3F4F}" 
    Name="CustomComputedURL" Required="FALSE" />

  </FieldRefs>
</ContentType>

Figure 8 shows the content type added to the Schema.xml file.

Figure 8. Adding the content type to the list definition

Adding the content type to the list definition

Adding Columns to the List Definition Default View

Edit the Schema.xml file and add the following FieldRef elements to the ViewFields element of the list definition's All Items view. The All Items view is the View element with its BaseViewID attribute equal to 1. The following FieldRef elements point to the previously defined custom columns.

<FieldRef Name="CustomSingleLineOfText"/>
<FieldRef Name="CustomCalculatedText"/>
<FieldRef Name="CustomMultiChoice1"/>
<FieldRef Name="CustomMultiChoice2"/>
<FieldRef Name="CustomChoice"/>
<FieldRef Name="CustomCalculatedChoice"/>

<FieldRef Name="CustomNumber1"/>
<FieldRef Name="CustomNumber2"/>
<FieldRef Name="CustomCalculatedNumber"/>

<FieldRef Name="CustomCurrency1"/>
<FieldRef Name="CustomCurrency2"/>
<FieldRef Name="CustomCalculatedCurrency"/>

<FieldRef Name="CustomDate"/>
<FieldRef Name="CustomCalculatedDate"/>

<FieldRef Name="CustomURL"/>
<FieldRef Name="CustomComputedURL"/>

Figure 9 shows the custom columns added to the list's default view.

Figure 9. Adding the columns to the default view

Adding the columns to the default view

Deploying the List Definition

At this point, the list definition can be deployed to SharePoint. To do this, on the Visual Studio Build menu, click Deploy CustomColumnsList. Visual Studio builds the solution and then deploys the list definition and list instance features to the SharePoint site designated by the Start Browser with URL setting on the Debug tab of the project Properties page. After the deployment is complete, the list and its columns can be tested.

Conclusion

This article describes how list definitions enable custom list templates to be created and deployed to a SharePoint site. A list definition contains the elements that define the content types, fields, views, and forms for the list. Visual Studio 2008 Extensions for Windows SharePoint Services provides a SharePoint list definition project template that can be used to create list definitions that can be easily modified to support custom list columns.

Additional Resources

For more information, see the following resources: