重新排序SharePoint 2010中的列表字段

时间:2011-09-20 18:33:24

标签: .net sharepoint sharepoint-2010 sharepoint-2007 sharepoint-api

我正在尝试使用下面的函数重新排序列表字段。但由于某种原因,在执行此功能后,当我检查List Settings中的字段顺序时,它似乎没有被更改。

我得到以下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>

我在这里缺少什么?

/// <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);
    }
}

1 个答案:

答案 0 :(得分:2)

根据其他例子我可以找到:

我在您的代码中看到的唯一区别是,在您ProcessBatchData来电之前,您没有:

spWeb.AllowUnsafeUpdates = true;

如果添加不起作用,我建议从ProcessBatchData捕获字符串输出,以找出究竟出了什么问题。