Inconvenient GetCustomProperty and SetCustomProperty

So, I’m working on a custom field definition for SharePoint. My field has a few custom properties that you need to fill in when you create it. However, after creation, those fields were empty – they were simply never set. You could then update them, and that worked fine, but creation was broken.

The code I’d inherited was trying to solve this problem (it’s a known issue) this way - which is a bit ugly. It also holds memory for the Dictionary of values that, as far as I could see, was only being freed on IISReset. Yuck. And it didn’t work in our code, despite matching the example in that thread quite closely.

Fortunately, I came across a neat post on Gunnar Peipman’s blog – Temporary Solution for GetCustomProperty an SetCustomProperty Errors. I don’t like using reflection to invoke the methods I need to use, but at least it’s a solution, even if not ideal. And it works!

Please go to Gunnar’s post – but just in case his blog goes down, I’m going to shamelessly plagiarise his code below…

Thanks Gunnar!

Uses the System.Reflection API.

        private void SetFieldAttribute(string attribute, string value)
        {
            Type baseType;
            BindingFlags flags;
            MethodInfo mi;

            baseType = typeof(SemaphoreField);
            flags = BindingFlags.Instance | BindingFlags.NonPublic;
            mi = baseType.GetMethod("SetFieldAttributeValue", flags);
            mi.Invoke(this, new object[] { attribute, value });
        }

        private string GetFieldAttribute(string attribute)
        {
            Type baseType;
            BindingFlags flags;
            MethodInfo mi;

            baseType = typeof(SemaphoreField);
            flags = BindingFlags.Instance | BindingFlags.NonPublic;
            mi = baseType.GetMethod("GetFieldAttributeValue",
                                        flags,
                                        null,
                                        new Type[] { typeof(String) },
                                        null);

            object obj = mi.Invoke(this, new object[] { attribute });

            if (obj == null)
                return "";
            else
                return obj.ToString();
        }

4 thoughts on “Inconvenient GetCustomProperty and SetCustomProperty

  1. I too have used this approach in WSS 3.0 / MOSS. There is a problem with the use of Field Attributes as they are persisted to the element as attributes which are unknown to SharePoint’s schema for Field.

    This is a big problem is SharePoint Foundation and 2010 when saving site templates as these are now wsp solutions and that fail to activate id any unknown attributes are present on Field elements.

    I’ve just re-tested SetCustomProperty under 2010 and it still does not persist any value?

  2. Back on this again and determined to understand the issues:

    1. Get and SetCustomProperty do work but you have to declare the properties in your _fldtypes… xml using .

    2. The persistence issue fully explained in the Storing Field Settings section of this article:

    http://msdn.microsoft.com/en-us/library/cc889345(v=office.12).aspx#CreatingWSS3CustomFields_StoringFieldSetting

    The article clearly explains how and where any metadata should be stored and more importantly gives a SharePoint farm safe method of working; enjoy otherwise it will drive you nuts.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>