¿Cómo establecer el nombre interno de un SPField en SharePoint 2010?

I add fields to a list programmaticly. The following line adds a new field´to an existing List

Lists.AddFieldToList(warrantyList, SPFieldType.Text, "internalFieldName", "ShownFieldName", "Comment", false, false, false);                    

This is the method that is called:

public static bool AddFieldToList(SPList list, SPFieldType fieldType, string fieldInternalName, string fieldDisplayName, string fieldDescription, bool unique, bool required, bool indexed)
    {
        SPField field = new SPField(list.ParentWeb.Fields, fieldType.ToString(), fieldInternalName);             
        bool success = AddFieldToList(list, field, fieldDisplayName, fieldDescription, unique, required, indexed);
        return success;
    }

after the first line of the method the field is filled with a lot of information, but no internalName(NULL) and a Title that contains "internalFieldName".

In the second line the following method is called:

  public static bool AddFieldToList(SPList list, SPField field, string fieldDisplayName, string fieldDescription, bool unique, bool required, bool indexed)
    {
        if (field != null &&
            (!list.Fields.Contains(field.Id)))
        {

            field.ReadOnlyField = false;
            field.Title = fieldDisplayName;
            field.Description = fieldDescription;
            field.EnforceUniqueValues = unique;
            field.Indexed = indexed;
            field.Required = required;
            list.Fields.Add(field);
            return true;
        }

        return false;
    }

After that the title is changed to "ShownFieldName" (of course). But my goal is to have an internalname "internalFieldName" which has a Displayname "ShownFieldName", so "ShownFieldName" is shown in the List but I can access the item by the internal name

as field.InternalName is readOnly: How can I solve that issue?

preguntado el 08 de noviembre de 11 a las 15:11

2 Respuestas

This line creates an SPField object that could have the internal name you need (displayName pasado a la constructor iguales fieldInternalName):

SPField field = new SPField(list.ParentWeb.Fields, fieldType.ToString(), fieldInternalName);

pero before anything has been saved to the database you change it in this line:

field.Title = fieldDisplayName;

Esta es la solucion:

  1. Add a field to the list passing internalFieldName como ambos fieldInternalName y fieldDisplayName.
  2. Llama a el SPList.Update() método.
  3. Get the field reference from the list and change its Title propiedad a ShownFieldName.
  4. Llama a el SPField.Update() método.

respondido 09 nov., 11:00

They have a method AddFieldAsXml in SharePoint since SharePoint 2007. If you do that, you get more control over how you are adding the fields. See an example in the link above.

respondido 09 nov., 11:08

I know that method but prefer programming with "real" objects rather than building strings. - Ole Albers

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.