简体   繁体   English

如何在 SharePoint 2010 中设置 SPField 的内部名称?

[英]How to set the internal name for a SPField in 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".在方法的第一行之后,该字段填充了大量信息,但没有 internalName(NULL) 和包含“internalFieldName”的 Title。

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).之后标题更改为“ShownFieldName”(当然)。 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但我的目标是有一个内部名称“internalFieldName”,它有一个显示名称“ShownFieldName”,所以“ShownFieldName”显示在列表中,但我可以通过内部名称访问该项目

as field.InternalName is readOnly: How can I solve that issue?因为 field.InternalName 是只读的:我该如何解决这个问题?

This line creates an SPField object that could have the internal name you need ( displayName passed to the constructor equals fieldInternalName ):此行创建一个SPField对象, SPField对象可能具有您需要的内部名称( displayName传递给构造函数等于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:这是解决方案:

  1. Add a field to the list passing internalFieldName as both fieldInternalName and fieldDisplayName .向列表中添加一个字段,将internalFieldName作为fieldInternalNamefieldDisplayName
  2. Call the SPList.Update() method.调用SPList.Update()方法。
  3. Get the field reference from the list and change its Title property to ShownFieldName .从列表中获取字段引用并将其Title属性更改为ShownFieldName
  4. Call the SPField.Update() method.调用SPField.Update()方法。

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.自 SharePoint 2007 以来,他们在 SharePoint 中有一个AddFieldAsXml方法。如果这样做,您可以更好地控制添加字段的方式。 See an example in the link above.请参阅上面链接中的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM