SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

The solution to saving properties in Custom Field Types!

I have finally managed to put Anton's code to the test in my own custom field type with great results.

The underlying issue is that the Update method does not seem to get called when adding a new field, and because there are a number of initializations of the field object the values in the custom propery editor controls get lost before you get to the OnAdded event where you could set them!

So, Anton's plan (which I confirm works very well) is to store the values of the properties in a static string dictionary, keyed with the hash of the current SharePoint context SPContext.Current.GetHashCode() so we get the right property back out in the OnAdded event and can set the correct values.

You can get the full code in the thread linked to above, but I'll summarize as best I can. I'm using a property called Application that is set by selecting a value from a combo box - which is actually part of a parent child combination used in the Property Editor, but that's another story...

First we create a new dictionary in the Field Class (the one that inherits from the SPField... class) to store the properties:

private static Dictionary<int, string> updatedApplicationProperty = new Dictionary<int, string>(); 

Create a new method that the Property Editor Control can call to update the dictionary to the value in the property editor control:

        public void UpdateApplicationProperty(string value)
        {
            updatedApplicationProperty[ContextId] = value;
        } 

Then, in the get for the property, return the dictionary stored property value if it exists or the internal value if not:

public string Application

get

{

                if (updatedApplicationProperty.ContainsKey(ContextId))
                {

                    //If we saved a value away and we're in the middle of the save field process, then pull it out.
                    return  updatedApplicationProperty[ContextId];
                }
                else
                {

                    //Return the existing value for the application property. This will be the case if we already have a field.
                    return sApplication;
                }

}

In the Update method of the Field Control set the custom property value and remove the stored property value:

this.SetCustomProperty("Application", this.Application);

if (updatedApplicationProperty.ContainsKey(ContextId))
     updatedApplicationProperty.Remove(ContextId);

And, lastly, in the OnSaveChange event in the class for the Property Editor Control store the value of the control that represents the property into the dictionary via the Update...Property method for a new field, or set the property as normal if the field already exists:

                BDCField currentBDCField = field as BDCField;

                if (isNewField)
                {
                    currentBDCField.UpdateApplicationProperty(this.cboApplications.SelectedValue);
                }
                else
                {
                    currentBDCField.Application = this.cboApplications.SelectedValue;
                }

Hopefully that's a reasonable description of the required steps. Anton has a good description of the why he chose the static dictionary in the forum post.

 Thanks again Anton and sorry it took me sooooo long to try it out!!!


Posted 05-25-2007 6:20 AM by adrh

Comments

DocWattsMan wrote re: The solution to saving properties in Custom Field Types!
on 11-01-2007 1:30 PM

I found a repost of your blog that is similar to this article but not quire the same at jack.whyyoung.com/.../Creating-Custom-Field-Types.htm. Wanted to let you know that I had the same issue with OOTB custom properties in custom field types. I came up with a simple workaround to the issue that should hold over until MS releases a fix. Here's my post: A Workaround for SharePoint Property Bug in Custom Field Types at thomascarpe.com/.../Post.aspx.

andrew robertson customs wrote andrew robertson customs
on 05-13-2008 9:50 AM

Pingback from  andrew robertson customs

Jaime wrote re: The solution to saving properties in Custom Field Types!
on 07-07-2008 6:36 AM

Hi, I have created my own custom field type following this post and others like this.

I have problem and It's that my field can save modified values. If I create a new column of my type, properties are stored properly. But, when I want to modify these properties, it doesn't work as I expected.

I saw your post and it uses the solution when the column is added and not when is modified. Can you explain a little more why of my behaviour and the reason of your solution???

Thanks.

Kevin wrote re: The solution to saving properties in Custom Field Types!
on 09-24-2008 12:09 PM

Here is a MSDN article that also shows how to persist these custom properties:

msdn.microsoft.com/.../cc837956.aspx

Jason wrote re: The solution to saving properties in Custom Field Types!
on 10-24-2008 11:14 PM

Great post, I just wanted to add that there is a way to push all of the work around logic into the Field class itself, without having to involve the editor control at all.

Create one static dictionary per custom property you wish to expose on your custom field.

In the custom property get accessor, check to see if the property exists in its corresponding dictionary, if so, return that one, otherwise return the value obtained from the GetCustomProperty call.

in the custom property set accessor, always save the value to the corresponding dictionary.

Override the custom field's Update method and call SetCustomProperty for each of the new custom properties.

it does the same thing that you've outlined above, but keeps all of the logic hidden in the field itself.  Thus, in your editor control's OnSave method, you don't need to check the isNewField property.

a minor change, but I feel keeps things a lot cleaner.

cheers!

Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.