List Definitions Field Definition

 

 

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.



<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>="Hello "&"World"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>


      
<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>NorthCHOICE>
    <CHOICE>SouthCHOICE>
    <CHOICE>EastCHOICE>
    <CHOICE>WestCHOICE>
  CHOICES>
  <Default>NorthDefault>
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-30CHOICE>
    <CHOICE>41-60CHOICE>
    <CHOICE>71-90CHOICE>
    <CHOICE>91+CHOICE>
  CHOICES>
  <Default>10-30Default>
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>BlueCHOICE>
    <CHOICE>GreenCHOICE>
    <CHOICE>WhiteCHOICE>
    <CHOICE>RedCHOICE>
  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: " &[CustomMultiChoice2]Formula>
  <FieldRefs>
    <FieldRef ID="{BB66D2E7-712D-427a-AF98-D12770E9EB95}" Name="CustomMultiChoice2"/>
  FieldRefs>
Field>


      

Field>


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] + 4Formula>
  <FieldRefs>
    <FieldRef ID="{BAC1B985-2AE5-4a3b-A163-09FC4BE62E0B}" Name="CustomNumber1"/>
  FieldRefs>
Field>


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

  <Default>1.53679Default>
  <Formula>=[CustomCurrency2] * [CustomNumber2]Formula>
  <FieldRefs>
    <FieldRef ID="{62576F23-3C54-4706-BDB2-DFCDAACD8A62}" Name="CustomCurrency2"/>
  FieldRefs>
Field>



<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>=TodayDefault>
Field>


  <Formula>=[CustomDate] + 0.0416666Formula>
  <FieldRefs>
    <FieldRef ID="{64F842AB-9CC6-4904-B41D-AC1A526B47D7}" Name="CustomDate"/>
  FieldRefs>
Field>



<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.comDefault>
Field>
      



  <FieldRefs>
    <FieldRef ID="{98C36F20-3F3B-43c9-8502-7DF16E0F1404}" Name="CustomURL" />
  FieldRefs>
  <DisplayPattern>
    <HTML>]]>HTML>
    <Column Name="CustomURL"/>
    <HTML>">]]>HTML>
    <Column Name="CustomSingleLineOfText"/>
    <HTML>]]>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"/>

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

    
    <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" />

    
    <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" />

    
    <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" />

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

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

 

 

 

Search site

© 2010 All rights reserved.