Programatically changing field order in Sharepoint 2007 list

后端 未结 3 1224
北恋
北恋 2021-01-27 03:07

I\'m adding in two new fields into an already existing Sharepoint list programmatically through a feature. The fields are being added successfully but I have been unable to adju

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-27 03:39

    I used the code from your answer, except I programmatically examined the content types and fields for the list I wanted to re-order.

    //Step 1 (optional): List out the content types and fields for your list to see what is in the list

     SPList list = web.Lists[strListName];
    
     string strRet="";
     foreach (SPContentType spct in list.ContentTypes)
                    {
                        strRet += "Content Type: " + spct.Name + ", Fields: 
    "; foreach (SPField field in spct.Fields) { if (strFieldInfo != "") { strFieldInfo += ", "; } strFieldInfo += "\"" + field.StaticName + "\""; } strRet += strFieldInfo + "
    -----
    "; } //Output the results lblOutput.Text = strRet;

    Now, you'll have an idea of how many content types your list has and what fields are in the list.

    By default, if content type management is not enabled, you'll have one content type that has all the fields.

    Sample output from the above code:

    Content Type: Event, Fields:

    "ContentType", "Title", "Location", "EventDate", "EndDate", "Description", "fAllDayEvent", "fRecurrence", "WorkspaceLink", "EventType", "UID", "RecurrenceID", "EventCanceled", "Duration", "RecurrenceData", "TimeZone", "XMLTZone", "MasterSeriesItemID", "Workspace", "Course", "CourseLocation"

    Next Step 2 is to change the order of the content type. You can cut and paste from the output from step 1, re-order it, and add "{" and "};" around it to create the string array for the ordering you want.

     if (list.ContentTypes.Count > 0)
                    {
    
                        SPContentType ct = list.ContentTypes[0]; //Specify the content type here, if you have more than one content type in your list.
    
                        string[] fieldnames = { "ContentType", "Title", "Course", "CourseLocation",  "EventDate", "EndDate", "Description", "fAllDayEvent", "fRecurrence", "WorkspaceLink", "EventType", "UID", "RecurrenceID", "EventCanceled", "Duration", "RecurrenceData", "TimeZone", "XMLTZone", "MasterSeriesItemID", "Workspace", "Location"};
                        ct.FieldLinks.Reorder(fieldnames);
                        web.AllowUnsafeUpdates = true;
                        ct.Update(true);
                        web.AllowUnsafeUpdates = false;
                    }
    

提交回复
热议问题