SharePoint: temporary solution for GetCustomProperty and SetCustomProperty errors

Another day, another kinky problem with SharePoint. This time I struggled with custom properties of my custom field. I tried out different code samples to make GetCustomProperty and SetCustomProperty to work but no luck at all – nothing worked for me. There was no errors when adding field to list or changing field properties but custom properties were never saved.

I don’t like solutions like my version comments solution but some (not only some, sorry) parts of SharePoint need brutality and violence to work like expected. But I have solution.

There is nice project called iLove SharePoint in CodePlex. I found nice trick from picker field control. And guess what, it works like charm! So, you can use these methods in field class to make custom properties to work.


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

    baseType =
typeof(LookupFieldWithPicker
);
    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(LookupFieldWithPicker
);
    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();
}

Well, this solution is not nicest one but I hope it works until custom property errors are removed from SharePoint.


5 thoughts on “SharePoint: temporary solution for GetCustomProperty and SetCustomProperty errors

Leave a Reply

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