Pergunta

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?

Foi útil?

Solução

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();
}

Outras dicas

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";
        }
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top