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.

Liked this post? Empower your friends by sharing it!

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

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

    • February 11, 2009 at 1:30 pm
      Permalink

      Thanks, i have been looking for this all day long, and yesterday also !!

      This solution finally works.

      Bit strange though that you need reflection to accomplisch something simple as storing some values.

    • May 30, 2009 at 12:18 pm
      Permalink

      Hi Can I achieve the same stuff outside this Field class.

      I have a fieldAdded event and I’m trying getcustomproperty there…..and it sometimes work and sometimes it returns null value.

      Please mail back to me at
      manu_khullar@infoys.com if you have a solution.

      Thanks
      Manu

    • May 30, 2009 at 12:53 pm
      Permalink

      Hi Can I achieve the same stuff outside this Field class.

      I have a fieldAdded event and I’m trying getcustomproperty there…..and it sometimes work and sometimes it returns null value.

      Please mail back to me at

      manu_khullar@infosys.com if you have a solution.

      Thanks

      Manu

    • June 3, 2009 at 8:25 am
      Permalink

      Thanks.. works like a charm! Have seen a few solutions around for this issue – nothing as clean/simple as this.

    • November 11, 2009 at 9:00 am
      Permalink

      Thanks for good words Jimmy! :)

    • November 27, 2009 at 5:51 pm
      Permalink

      I had SetCustomPropery working with a little help from ReadUnreaColumn on Codeplex.

      Then my SSD failed while backing up the source!!!

      I re-wrote it and just couldn’t get Set/GetCustomProperty to work — your work here has got me out of a big hole – many thanks.

      What I’ve said in my earlier post remains true but there’s another nasty thing going on here.

    • March 13, 2010 at 6:09 pm
      Permalink

      Thanks for feedback, Daniel!

      You are almost correct: yes, in thoery property schema should help you. In practice there are versions when this does not hold true. As some of your customers may face this problem you know the solution what to do. :)

    • April 29, 2010 at 8:27 am
      Permalink

      Thank you Gunnar, without wanting to sound strange, I could kiss ya! I’ve spent two days trying to sort that out. Using Reflection is a bit annoying, but it’s a tidier solution that what we had been trying!

      Oh, and for Daniel above – my XML already contained my properties, but I still had the issue this solves.

    • February 5, 2015 at 6:48 am
      Permalink

      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.

    • February 7, 2015 at 1:45 am
      Permalink

      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.

    • Pingback:Update Custom property of Custom Field Type | DL-UAT

    Leave a Reply

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