C# 以编程方式对Sharepoint内容类型字段重新排序

C# 以编程方式对Sharepoint内容类型字段重新排序,c#,sharepoint-2010,C#,Sharepoint 2010,如何通过C#&Sharepoint 2010客户端对象模型或Sharepoint 2010 Web Services设置创建和编辑表单中内容类型字段的顺序? 谢谢。以下示例演示如何使用CSOM对内容类型中的字段重新排序 var ctId = "0x01020047F5B07616CECD46AADA9B5B65CAFB75"; //Event ct var listTitle = "Calendar"; using (var ctx = new ClientContext(webUri)) {


如何通过C#&Sharepoint 2010客户端对象模型或Sharepoint 2010 Web Services设置创建和编辑表单中内容类型字段的顺序?

谢谢。

以下示例演示如何使用CSOM对内容类型中的字段重新排序

var ctId = "0x01020047F5B07616CECD46AADA9B5B65CAFB75";  //Event ct
var listTitle = "Calendar";
using (var ctx = new ClientContext(webUri))
{

     var list = ctx.Web.Lists.GetByTitle(listTitle);
     var contentType = list.ContentTypes.GetById(ctId);


     //retrieve fields
     ctx.Load(contentType,ct => ct.FieldLinks);
     ctx.ExecuteQuery();
     var fieldNames = contentType.FieldLinks.ToList().Select(ct => ct.Name).ToArray();

     fieldNames.ShiftElement(1, 2);   //Ex.:Render Title after Location in Calendar

     //reorder
     contentType.FieldLinks.Reorder(fieldNames);
     contentType.Update(false);
     ctx.ExecuteQuery();
} 
在哪里

公共静态类ArrayExtensions
{
//从http://stackoverflow.com/a/7242944/1375553
公共静态void-ShiftElement(此T[]数组,int-oldIndex,int-newIndex)
{
//TODO:参数验证
if(oldIndex==newIndex)
{
return;//无操作
}
T tmp=数组[oldIndex];
如果(新索引<旧索引)
{
//需要将阵列的一部分向上移动以腾出空间
复制(数组,新索引,数组,新索引+1,旧索引-新索引);
}
其他的
{
//需要将阵列的一部分“向下”移动以填充间隙
复制(数组,oldIndex+1,数组,oldIndex,newIndex-oldIndex);
}
数组[newIndex]=tmp;
}
}
结果

以前

之后


Vadim,谢谢您的回复!我已经使用过这种方法(FieldLinkCollection.Reorder(string[]),但类似SharePoint.Client.ServerException的方法“Reorder”不存在。此方法存在于Sharepoint 2010 API中还是仅存在于Sharepoint 2013 API中?
public static class ArrayExtensions
{
    //from http://stackoverflow.com/a/7242944/1375553
    public static void ShiftElement<T>(this T[] array, int oldIndex, int newIndex)
    {
        // TODO: Argument validation
        if (oldIndex == newIndex)
        {
            return; // No-op
        }
        T tmp = array[oldIndex];
        if (newIndex < oldIndex)
        {
            // Need to move part of the array "up" to make room
            Array.Copy(array, newIndex, array, newIndex + 1, oldIndex - newIndex);
        }
        else
        {
            // Need to move part of the array "down" to fill the gap
            Array.Copy(array, oldIndex + 1, array, oldIndex, newIndex - oldIndex);
        }
        array[newIndex] = tmp;
    }
}