2

下面的代码运行完美,但是我需要新字段位于第 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;
}
4

1 回答 1

1

使用ct.FieldLinks.Reorder(stringArrayOfInternalFieldNames)MSDN SPFieldLinkCollection.Reorder

于 2012-07-31T14:40:02.477 回答