Second Life of a Hungarian SharePoint Geek

December 20, 2009

Injecting an invisible custom field to a form

Filed under: Custom fields, SharePoint — Tags: , — Peter Holpar @ 05:44

If you want to inject a custom field to a form just to interact with other fields on the form or do something that is not typical to do with custom columns (see my earlier post in this topic) one of the first problems you will face how to hide your field so that it will be included in the form but only in an invisible way.

Let’s see some alternatives that do not work, just to learn, what the problem with them is.

There are the ShowInDisplayForm, ShowInNewForm, ShowInEditForm properties of the SPField class. These are great ways to hide your field, but if you set these properties to false, the field will be not included to the form at all. That is not very useful for us if we need the code of the field to run in the context of the form.

Our next try is to override the Visible property of the field control to return always false.

  1. public override bool Visible
  2. {
  3.     get
  4.     {
  5.         return false;
  6.     }
  7.     set
  8.     {
  9.         base.Visible = false;
  10.     }
  11. }

But it does not help, our field is still visible on the form.

It has the same effect (= nothing) if you set the Visible property of the field control in the OnLoad method to false:

this.Visible = false;

But we never give up!

The solution to the issue is to set the Visible property of the TemplateContainer that contains the field control. The Parent of the field control is a FormField, Parent of the Parent is the TemplateContainer we need. So our code that works in the OnLoad method seems like this:

this.Parent.Parent.Visible = false;

Setting the Visible property of the Parent’s Parent will set the Visible property of the control itself too.

Important note, that if you need to override the default save event by overriding the UpdateFieldValueInItem method of the field control as described in my former post, then you should set the Visible property first to true, then set back to false, otherwise the UpdateFieldValueInItem method will not be called. That is because the OnLoad method of the BaseFieldControl checks for the visibility of the control, and call the UpdateFieldValueInItem only if the control is visible.

  1. protected override void OnLoad(EventArgs e)
  2. {
  3.     this.Parent.Parent.Visible = true;
  4.     base.OnLoad(e);
  5.     this.Parent.Parent.Visible = false;
  6. }

We typically do not want this kind of field to be included in any view of the list. When the field is added to the list, you should clear the Add to default view check box.

image

Unfortunately, I have not figured that out how to make this setting to the default one that user cannot change on the user interface.

To disable users to add the field to views we can set the ShowInViewForms property to false when the field is added to the list, that is in the OnAdded method, as shown below:

  1. public override void OnAdded(SPAddFieldOptions op)
  2. {            
  3.     base.OnAdded(op);
  4.     if ((!this.ShowInViewForms.HasValue) || ((this.ShowInViewForms.HasValue) && (!this.ShowInViewForms.Value)))
  5.     {
  6.         this.ShowInViewForms = false;
  7.         this.Update();
  8.     }
  9. }

If this property is set to false, the field will be not included in the Edit View page in the list of available fields.

Advertisements

2 Comments »

  1. […] Second Life of a Hungarian SharePoint Geek If your sword is too short, take one step forward « Injecting an invisible custom field to a form […]

    Pingback by Interacting with other fields from a custom field « Second Life of a Hungarian SharePoint Geek — December 25, 2009 @ 19:29


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

Blog at WordPress.com.

%d bloggers like this: