SharePoint Tip of the day : Manage default value in your custom field type


I took me quite a long time figure out why my <defaultvalue> property of my custom field type was not taken into account. It turned out that I need to do a little bit of work in my overriding of the CreateChildControls of my field control.
Basically, we are checking the field has actually no value (to avoid overriding a previously set value) through base.ListItemFieldValue and also if a default value was provided in the field xml definition (or programmatically).

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            if(ControlMode== SPControlMode.New || ControlMode == SPControlMode.Edit)
            {
                string currentValue = (base.ListItemFieldValue == null ? string.Empty : base.ListItemFieldValue.ToString());
                if (string.IsNullOrEmpty(
currentValue) && !string.IsNullOrEmpty(Field.DefaultValue))
                {
                    base.ListItemFieldValue = Field.DefaultValue;
                }
            }
        }

Sweet !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s