Export (0) Print
Expand All
Expand Minimize
5 out of 7 rated this helpful - Rate this topic

Programmatically Inheriting Default Field Values in Windows SharePoint Services 3.0

Windows SharePoint Services 3

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

Visual How To

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

Microsoft Corporation

March 2008

Overview

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.

Code It

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.

    public class DefaultingYesNoField : SPFieldChoice
    {
        public DefaultingYesNoField(SPFieldCollection fields, 
        string fieldName) : base(fields, fieldName)
        {

            // Set this choice field to yes/no radio buttons.
            base.EditFormat = SPChoiceFormatType.RadioButtons;
            base.Choices.Add("Yes");
            base.Choices.Add("No");
        }

        public DefaultingYesNoField(SPFieldCollection fields, 
        string typeName, string displayName) : base(fields, typeName, displayName)
        {

            // Set this choice field to yes/no radio buttons.
            base.EditFormat = SPChoiceFormatType.RadioButtons;
            base.Choices.Add("Yes");
            base.Choices.Add("No");
        }

        public override BaseFieldControl FieldRenderingControl
        {
            get
            {

                // Use our custom control when the field is rendered.
                BaseFieldControl control 
                = new DefaultingYesNoFieldControl();
                control.FieldName = base.InternalName;
                return control;
            }
     }

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.

   public class DefaultingYesNoFieldControl : RadioButtonChoiceField
   {
      protected override void CreateChildControls()
      {
         base.CreateChildControls();
         if (base.Field != null)
         {
            if (base.ControlMode.Equals(SPControlMode.New) || 
            base.ControlMode.Equals(SPControlMode.Edit))
            {
               string currentValue = 
               (base.ListItemFieldValue == null ? string.Empty : 
               base.ListItemFieldValue.ToString());
               if (string.IsNullOrEmpty(currentValue))
               {
                  try
                  {
                     string parentValue = string.Empty;
                     SPFolder parentFolder = base.ListItem.File.ParentFolder;
                     if (parentFolder.Item["Vital_x0020_Folder"] != null)
                     {
                        parentValue = parentFolder.Item["Vital_x0020_Folder"].ToString();
                     }
                  }
                  catch
                  { }
                  base.ListItemFieldValue = parentValue;
               }
            }
         }
      }
   } 

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.

<?xml version="1.0" encoding="utf-8"?>
<FieldTypes>
  <FieldType>
    <Field Name="TypeName">DefaultingYesNoField</Field>
    <Field Name="ParentType">Choice</Field>
    <Field Name="TypeDisplayName">Defaulting Yes/No Field</Field>
    <Field Name="TypeShortDescription">Defaulting Yes/No Field</Field>
    <Field Name="FieldTypeClass">DefaultingFields.DefaultingYesNoField,
    DefaultingFields,Version=1.0.0.0,Culture=neutral,
    PublicKeyToken=2d4b846fea72d4fb</Field>
  </FieldType>
</FieldTypes>
Read It

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.

See It

Programmatically Inheriting Default Field Value

Watch the Video

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

Explore It
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.