Programmatically Inheriting Default Field Values in Windows SharePoint Services 3.0

Summary: Learn how to set a default document metadata value by using a custom field type and control in Windows SharePoint Services 3.0.

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

Microsoft Corporation

March 2008

Windows SharePoint Services 3.0 provides an out-of-the-box way to set a default value for a field in a list. However, there may be situations where you want to conditionally set a default value based on information from another location. In this Microsoft Office Visual How To, you learn how to set a default document metadata value based on a field from the containing folder by using a custom field type and control. This approach to setting a default value, as opposed to using something like an event handler, allows the value to appear on the New Item page and lets the user see and change the value, if necessary.

This example assumes that you have a document library that contains folder and document content types. The folder content type has been extended to contain a Yes/No Choice field named Vital Folder, and a custom DefaultingYesNo field named Vital Item has been added to the document content type. When a new document is added to the library, the default value for Vital Item is the same value as for the Vital Folder field set on the parent folder.

In creating the custom DefaultingYesNo field, instead of creating a new field, you can extend the existing choice field from the SPFieldChoice class. The first step is to create a class that derives from SPFieldChoice. In the constructor, set the EditFormat property so that the field displays radio buttons, and then add Yes and No as choices. You also override the FieldRenderingControl method to call the custom field control, DefaultingYesNoFieldControl. These steps are shown in the following code.

The field control defines how the data for the field is displayed. To create the custom control, create a class that derives from the RadioButtonChoiceField class. This provides you with the user interface (UI) for the choice field and lets you insert the logic that is needed to set the default value when the field is rendered. You override the CreateChildControl method to add custom logic to set the default field value.

After checking to ensure that the base Field object is not null, you evaluate the control mode of the control. Each field control has three control modes: New, Display, and Edit. In this case, you check for a mode of New or Edit. This means that the user is on the corresponding New or Edit form for this item. Next, you determine whether this field (that is, the metadata field on the corresponding list item) already has a value. If it does not, you obtain the value that you want to use as the default, which is the value for the Vital Folder metadata field on the parent folder of this item. At this point, the control displays the defaulted value, but the value is not committed to the list item until the user clicks OK on the page.

To register the custom DefaultingYesNoField, you must create a field type definition file. This is an XML file that must have a file name that begins with FLDTYPES (for example, FLDTYPES_DefaultingYesNoField.xml). The file contains the settings for the custom field type that allow SharePoint Server to create an instance of this field. After you create the file, put it in the same location as the other FLDTYPES*.xml files, which are located in the ..\12\TEMPLATE\XML directory.

Many other field types and controls are available that you can use and extend in a similar manner. These fields and controls derive from SPField and BaseFieldControl, which you can also use to provide completely new functionality. By creating custom fields and controls, developers have a lot of control over how information is captured and displayed.

In this example, the custom field is based on SPFieldChoice because it provided most of the functionality that was needed. The out-of-the-box choice field is flexible and can be rendered as radio buttons, a drop-down list, or as check boxes. This example uses radio buttons, which you define by setting the EditFormat property in the DefaultingYesNoField class, and creating a custom field control that is based on RadioButtonChoiceField.

The value that is retrieved for the default came from a specific metadata field on the containing folder. However, you can obtain it just as easily from another source, such as an item in another list, a property in the web property bag, or an evaluation based on the current user.

Watch the Video

Length: 6:46 | Size: 4.5 MB | Type: WMV