Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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);
        }
share|improve this question

2 Answers 2

up vote 0 down vote accepted

There might be more elegant ways of doing this, but the following works for me:

    public static void MoveFieldInColumnOrder(SPList list, SPContentTypeId ctId, string internalName, int insertAt)
    {
        SPContentTypeId ct = list.ContentTypes.BestMatch(ctId);
        SPContentType cType = list.ContentTypes[ct];

        List<string> listOfFieldNames = new List<string>();

        foreach (SPFieldLink fl in cType.FieldLinks)
        {
            listOfFieldNames.Add(fl.Name);
        }

        listOfFieldNames.Remove(internalName);
        listOfFieldNames.Insert(insertAt, internalName);

        cType.FieldLinks.Reorder(listOfFieldNames.ToArray());

        cType.Update(false, true);
    }
share|improve this answer
    
please check my update1:, I changed a little bit your code because the content type is in the site collection root level, but the list its in a subsite. I need to modify the content type in the root level, the code doesnt throw any exception, but when I try to add an item to the list, it shows at the end. not in thr 25th position –  L.V. Sharepoint Architect Jul 30 '12 at 12:17

You can use SPFieldLinkCollection.Reorder just pass it the internal names of the first 5 fields

share|improve this answer
    
that was an example only, the true is this content type has like 80 fields, and I want to find an easier way to type 80 fields for the reorder method –  L.V. Sharepoint Architect Jul 30 '12 at 12:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.