THe code below is working perfect, however I need the new field to be in the 5th position, and not at the end in the forms.
Any idea how?
private void UpdateDossierContentType(SPWeb parentWeb)
{
#region Adds the product name lookup to the content type Dossier
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);
#endregion
#region Removes the old product name managed metadata field from the content type.
//SPContentType dossierCT = productList.ContentTypes["Dossier"];
//dossierCT.DeleteFieldRefFromContentType(parentWeb.Fields["Product"]);
//dossierCT.Update();
#endregion
}
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;
}
Update 1:
MoveFieldInColumnOrder(parentWeb, "Dossier", productList.Fields["Product Name"].InternalName, 25);
public static void MoveFieldInColumnOrder(SPWeb web, string contentTypeName, string internalName, int insertAt)
{
SPContentType ct = web.Site.RootWeb.ContentTypes[contentTypeName];
List<string> listOfFieldNames = new List<string>();
foreach (SPFieldLink fl in ct.FieldLinks)
{
listOfFieldNames.Add(fl.Name);
}
listOfFieldNames.Remove(internalName);
listOfFieldNames.Insert(insertAt, internalName);
ct.FieldLinks.Reorder(listOfFieldNames.ToArray());
ct.Update(true, true);
}