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.



See also

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

  • samuel sandeep says:

    Nice Article,thanks for sharing this information .

    Sharepoint Developers

  • Caleb says:

    So, you can use these methods in field class to make custom properties to work.

    I’m so new in this so this is what I’ve tried. I wrote a new CustomField : SPField class, with its default constructors, then I put these methods into it. I initialized a field then run the functions. But it didn’t work and I think I was wrong somehow.

    Can you please kindly show me the details how to get it working. By the way I’m working on a SPServer 2013 visual webpart.

  • Gunnar says:

    I hope this anomaly is already solved in newer versions of SharePoint and you can follow documentation when developing custom field. I applied the hack introduced here when I was sure that there is no other way to get my field work.

  • […] was refering to the following article which said to add the SetFieldAttribute() method. So I added it as a public method in the field […]

Leave a Reply

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