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();
}