Moon
Moon

Reputation: 35265

Reordering list fields in SharePoint 2010

I am trying to reorder list fields using the function below. But for some reason, after executing this function, when I go check the field order in the List Settings, it doesn't seem to have been changed.

I get the following as the result of the ProcessBatchData:

<Result ID="0,REORDERFIELDS" Code="-2130575323">
    <ErrorText>
        Fields have been added or removed since you began this editing session. Please refresh your view and try again
    </ErrorText>
</Result>

What am I missing here?

/// <summary>
/// Reorders the share point list fields.
/// </summary>
/// <param name="spWeb">The sp web.</param>
/// <param name="spList">The sp list.</param>
/// <param name="orderedFields">The ordered fields.</param>
private static void ReorderSharePointListFields(SPWeb spWeb, SPList spList, IEnumerable<string> orderedFields)
{
    var stringBuilder = new StringBuilder();
    using (var xmlTextWriter = new XmlTextWriter(new StringWriter(stringBuilder)))
    {
        xmlTextWriter.Formatting = Formatting.Indented;

        xmlTextWriter.WriteStartElement("Fields");

        foreach (string orderedField in orderedFields)
        {
            xmlTextWriter.WriteStartElement("Field");
            xmlTextWriter.WriteAttributeString("Name", orderedField);
            xmlTextWriter.WriteEndElement();
        }

        xmlTextWriter.WriteEndElement();
        xmlTextWriter.Flush();

        const string rpcMethod =
            @"<?xml version=""1.0"" encoding=""UTF-8""?>  
                            <Method ID=""0,REORDERFIELDS"">  
                            <SetList Scope=""Request"">{0}</SetList>  
                            <SetVar Name=""Cmd"">REORDERFIELDS</SetVar>  
                            <SetVar Name=""ReorderedFields"">{1}</SetVar>  
                            <SetVar Name=""owshiddenversion"">{2}</SetVar>  
                            </Method>";

        SPList list = spWeb.Lists[spList.ID];

        string rpcCall = string.Format(rpcMethod, list.ID, SPHttpUtility.HtmlEncode(stringBuilder.ToString()),
                                       list.Version);
        spWeb.ProcessBatchData(rpcCall);
    }
}

Upvotes: 0

Views: 964

Answers (1)

Rich Bennema
Rich Bennema

Reputation: 10335

Based on other examples I can find:

The only difference I saw with your code was that, before your ProcessBatchData call, you did not have:

spWeb.AllowUnsafeUpdates = true;

If adding that does not help, I would suggest capturing the string output from ProcessBatchData to find out what exactly went wrong.

Upvotes: 2

Related Questions