Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Saving Custom Field Type properties using SPField.SetCustomProperty within the IFieldEditor.OnSaveChange event does not seem to work, for reasons explained elsewhere.

I've seen all kinds of bodges workarounds for this problem (including using Thread Local Storage or nasty Javascript hacks), but I can't believe this is as Microsoft intended.

Is there a way to get complex column settings from the Add/Edit column page to the column creation events (SPField.OnAdded, SPField.OnUpdated) without resorting to these methods? Or is this just a massive oversight?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

One approach might be Gunnar Peipman's - use reflection to bypass the SetCustomProperty field entirely. It has worked well for me a few times.You may need to serialize/deserialize data in these prope

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

baseType = typeof(MyCustomField);
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(MyCustomField);
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();
}
share|improve this answer
    
Now that's suitably outlandish to earn an upvote at least. Love Gunnar's take on the subject: "... parts of SharePoint need brutality and violence to work like expected" –  Stuart Pegg Apr 13 '11 at 13:12
    
Agreed, that's what I thought - but it works, and it's tidier than storing the properties in Thread Local Storage. That can work too - but is God-awful to maintain. –  Andy Burns Apr 13 '11 at 13:22
add comment

I have been using SetCustomProperty in a recent project. The way that it was implemented was to create a new Field Type and inherit from SPField. A custom field type xml field type was then created and stored in 12HIVE\TEMPLATES\XML\fldtypes_[projectname].xml and a property schema was described in this xml file for the field type:-

    <PropertySchema>
      <Fields>
        <Field Name="MultiImageMaxNoImages"
               DisplayName="Maximum Number of Images"
               MaxLength="1"
               Type="Number">
          <Default>4</Default>
        </Field>
        <Field Name="MultiImageMaxSize"
               DisplayName="Maximum Size of Image (Kb)"
               MaxLength="5"
               Type="Number">
          <Default>1024</Default>
        </Field>
      </Fields>
    </PropertySchema>

I then created a couple of public properties in the SPField class which would use the following to save field properties.

    /// <summary>
    /// The maximum number of images that can be stored in the field.
    /// </summary>
    public int MaxImages
    {
        get
        {
            int maxImages = 4;
            Int32.TryParse(GetCustomProperty(Providers.MultiImageFieldProvider.Properties.MaxNumberOfImages).ToString(),out maxImages);
            return maxImages;
        }

        set
        {
            int maxImages = value;
            SetCustomProperty(Providers.MultiImageFieldProvider.Properties.MaxNumberOfImages,maxImages);
        }
    }

The Providers.MultiImageFieldProvider is just a static helper class:

    /// <summary>
    /// Provider for common SharePoint fields and functions.
    /// </summary>
    public static class MultiImageFieldProvider
    {
        /// <summary>
        /// Properties class contains internal names of extra custom properties 
        /// </summary>
        public static class Properties
        {
            // Custom Properties
            public const string MaxNumberOfImages = "MultiImageMaxNoImages";
            public const string MaxSize = "MultiImageMaxSize";
        }
    }
share|improve this answer
    
Thanks for your answer, but unfortunately this doesn't fit the requirements I mentioned. The straightforward data types presented and stored by SharePoint itself (defined in the PropertySchema) require no additional work to support; the SetCustomProperty call in the example being largely redundant in this context. The challenge is in storing anything more than these simple data types. –  Stuart Pegg Apr 11 '11 at 7:31
    
OK, sorry I see your issue. Could you explain what is that you are trying to achieve, maybe there is a different way to achieve the result. –  Simon Doy Apr 12 '11 at 6:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.