Second Life of a Hungarian SharePoint Geek

December 30, 2009

Interacting with other fields from a custom field, 2nd part

Filed under: Custom fields, SharePoint — Tags: , — Peter Holpar @ 22:21

In the first part of this post I left an open issue. There was a problem with setting the fields read only.

Since then I’ve found the source of the incorrect value in the display (read only) mode of the field on the new form and provide a workaround for that issue here, that is unfortunately a bit of hacking.

First, I should note, that as I observed, the value displayed in this case has no effect on the value saved into the item, but default values have also no effect. Furthermore, when the item is opened and saved in the edit mode, the former field values might be also lost.

I found that when the field control mode is set to display on the new form in our case, the value of the PreviewValueTyped property of the related field is displayed. That is FieldName field value. for the text fields (replace FieldName with the actual field name), 1,234.56 for numeric fields, the actual date and time for date type fields, default value or if there is no default value, the first option for choice fields.

The source of the issue seems to be is that the ItemFieldValue property and the ListItemFieldValue property of the BaseFieldControl return the value of the PreviewValueTyped if the ItemId property is zero, that is the case for a new item. I should note that the obfuscated RenderFieldForDisplay method also references the PreviewValueTyped property. I’ve tried a hack and set the ItemId to –1, and it seems to solve part of the issue, the field value is displayed correctly.

But the values of the affected fields in the item are still not saved correctly, since setting the ItemId to –1 has a side effect that it resets the ItemFieldValue and the ListItemFieldValue properties to null. To correct this behavior I backed up the value of the ListItemFieldValue property before setting the ItemId to –1, and restore it at the end. I cannot omit setting the ItemId to –1, since while the ItemId is zero, setting the value of the ListItemFieldValue property has no effect.

  1. protected void SetFieldReadOnly(String fieldName)
  2. {
  3.     BaseFieldControl fieldControl = GetFieldControlByName(fieldName);
  4.     Object value = fieldControl.ListItemFieldValue;
  5.     fieldControl.ControlMode = SPControlMode.Display;
  6.     if (ControlMode == SPControlMode.New)
  7.     {
  8.         fieldControl.ItemId = -1;
  9.         fieldControl.ListItemFieldValue = value;
  10.     }
  11. }

After these modifications the new form works as expected, but there is an issue with the edit form. Even the correct values are displayed, empty text is saved for text field, and the first item is set for the choice field when saving the item.

This issue was solved by placing the code from the OnLoad method to the OnInit method of the field control.

An important note finally. It is mandatory to place the custom field that tries to access other fields using the GetFieldControlByName method introduced in the former post after the last field it references. You can change the field order using the Change Field Order page (List Settings / Column ordering), or you can do it using code. If you don’t do that this way, the method will not find the field on the page, as it is added only after the current field is loaded.

Advertisements

3 Comments »

  1. […] is too short, take one step forward « Injecting an invisible custom field to a form Interacting with other fields from a custom field, 2nd part […]

    Pingback by Interacting with other fields from a custom field « Second Life of a Hungarian SharePoint Geek — December 30, 2009 @ 22:23

  2. […] Second Life of a Hungarian SharePoint Geek If your sword is too short, take one step forward « Interacting with other fields from a custom field, 2nd part […]

    Pingback by Cross field, cross item, cross list or even more complicated validations on SharePoint forms « Second Life of a Hungarian SharePoint Geek — January 4, 2010 @ 03:00

  3. […] Interacting with other fields from a custom field, 2nd part […]

    Pingback by Creating real multi column fields for SharePoint « Second Life of a Hungarian SharePoint Geek — January 9, 2010 @ 01:35


RSS feed for comments on this post. TrackBack URI

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

Create a free website or blog at WordPress.com.

%d bloggers like this: