Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
 
 
DZone MVB

Links

Social

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.
Posted: Feb 03 2009, 10:59 PM by DigiMortal | with 16 comment(s)
Filed under:

Comments

TechnoMANYAK said:

oooo thanks. i seacrhing to these codes at google

# February 5, 2009 9:26 AM

Links (2/5/2009) « Steve Pietrek - Everything SharePoint said:

Pingback from  Links (2/5/2009) « Steve Pietrek - Everything SharePoint

# February 5, 2009 8:11 PM

vVeen said:

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.

# February 11, 2009 8:30 AM

Manu said:

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 8:18 AM

Manu said:

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

# May 30, 2009 8:53 AM

Keith said:

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

# June 3, 2009 4:25 AM

Confluence: SharePoint Development Wiki said:

There's plenty of advice out there and I figured it made sense to put it all centrally somewhere to save others the hassle! Rendering Customizing the Rendering of a Custom SPField

# July 20, 2009 5:00 AM

Steve Dawkins said:

What's happening is that SharePoint reinstantiates your field class after your implementation of IFieldEditor OnSaveChange has been called. Resulting in the OnAdded event having a clean instance of the class to save properties from! (hence that are empty)

If this wasn't happening Get and Set would work, they're just appearing not to work.

You'll find another workaround for this in the code for the codeplex solution at www.codeplex.com/readunreadcolumn

# November 8, 2009 3:10 PM

Jimmy said:

Hi DigiMortal.

I would like to thank you for your work on this article. I have been struggling with the problems caused by Get/SetCustomProperty on SPField. It really puzzled me that I received no erros, and that it seemed that the values were indeed saved/stored. But the values did not get stored permanently.

I successfully implemented your solution, and it now works just as expected. I really appriciate that you took the time to write this article. You saved my day - I now look forward to the further development on my project, and my girl friend is happy, because I now have time for her too. In short: You made this world a better place ;)

Thank you once again! :)

Kind regards

Jimmy

# November 11, 2009 3:27 AM

DigiMortal said:

Thanks for good words Jimmy! :)

# November 11, 2009 4:00 AM

Steve Dawkins said:

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.

# November 27, 2009 12:51 PM

Daniel Smon said:

You don't need to use reflection to save this information.

The reason that GetCustomProperty and SetCustomProperty don't work for you is probably because you haven't defined the property in your fldtypes definition for the Field Type.

GetCustomProperty and SetCustomProperty only work for properties already defined:

   <PropertySchema>

       <Fields>

         <Field Name="YourPropertyName"

                DisplayName="YourPropertyName"

                Type="Text"

                Hidden="TRUE">

         </Field>

       </Fields>

   </PropertySchema>

See social.msdn.microsoft.com/.../fb1cb936-3abb-48c2-8d19-49007688dc34 for another example.

Regards,

Daniel.

# March 12, 2010 12:18 AM

DigiMortal said:

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

# March 13, 2010 1:09 PM

Andy Burns said:

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.

# April 29, 2010 4:27 AM

Inconvenient GetCustomProperty and SetCustomProperty » Andy Burns’ SharePoint Blog said:

Pingback from  Inconvenient GetCustomProperty and SetCustomProperty &raquo; Andy Burns&#8217; SharePoint Blog

# April 29, 2010 4:45 AM

Steve Dawkins said:

Back on this again and determined to understand the issues:

1. Get and SetCustomProperty do work but you have to declare the properties in your _fldtypes... xml using <PropertySchema>.

2. The persistence issue fully explained in the Storing Field Settings section of this article:

msdn.microsoft.com/.../cc889345(v=office.12).aspx#CreatingWSS3CustomFields_StoringFieldSetting

The article clearly explains how and where any metadata should be stored and more importantly gives a SharePoint farm safe method of working; enjoy otherwise it will drive you nuts.

# April 3, 2011 3:40 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)