X

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!
Categories: SharePoint

View Comments (12)

  • 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.

  • 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

  • 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

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

  • 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.

  • 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. :)

  • 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.

  • 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.

  • 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.

Related Post