将字段添加到特定位置的内容类型

时间:2012-07-30 11:46:05

标签: c# .net sharepoint

下面的代码工作正常,但是我需要新字段位于第5位,而不是表单中的结尾。

知道怎么做?

private void UpdateDossierContentType(SPWeb parentWeb)
{
    SPList productList = parentWeb.Site.RootWeb.Lists["Product"];
    SPFieldLookup lookup = CreateLookupField("ProductNameLookup", "CustomGroup",
            false, false, parentWeb.Site.RootWeb,
            parentWeb.Site.RootWeb.Lists["Product"],
           productList.Fields["Product Name"].InternalName);
   LinkFieldToContentType(parentWeb.Site.RootWeb, "Dossier", (SPField)lookup);

    //SPContentType dossierCT = productList.ContentTypes["Dossier"];
    //dossierCT.DeleteFieldRefFromContentType(parentWeb.Fields["Product"]);
    //dossierCT.Update();
}

public static void LinkFieldToContentType(SPWeb web, string contentType, SPField field)
{
    SPContentType ct = web.ContentTypes[contentType];
    ct.FieldLinks.Add(new SPFieldLink(field));
    ct.Update(true);
}

public static SPFieldLookup CreateLookupField(string fieldName, string group, bool required, bool allowMultipleValues, SPWeb w, SPList lookupList, string lookupField)
{
    w.Fields.AddLookup(fieldName, lookupList.ID, lookupList.ParentWeb.Site.RootWeb.ID, required);
    SPFieldLookup lookup = (SPFieldLookup)w.Fields[fieldName];
    lookup.AllowMultipleValues = allowMultipleValues;
    lookup.LookupField = lookupField;
    lookup.Group = group;
    lookup.Update(true);
    return lookup;
}

1 个答案:

答案 0 :(得分:1)

使用ct.FieldLinks.Reorder(stringArrayOfInternalFieldNames),请参阅MSDN SPFieldLinkCollection.Reorder