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?
This line creates an SPField object that could have the internal name you need (displayName passed to the constructor equals fieldInternalName):
SPField field = new SPField(list.ParentWeb.Fields, fieldType.ToString(), fieldInternalName);
but before anything has been saved to the database you change it in this line:
field.Title = fieldDisplayName;
Here's the solution:
fieldInternalName and fieldDisplayName.SPList.Update() method.Title property to ShownFieldName.SPField.Update() method.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With