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

时间:2015-04-27 13:31:18

标签: c# sharepoint-2010


如何通过C#和&amp ;;在“创建和编辑”表单中设置内容类型字段的顺序 Sharepoint 2010客户端对象模型 Sharepoint 2010 Web服务
谢谢。

1 个答案:

答案 0 :(得分:2)

以下示例演示如何使用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();
} 

,其中

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

<强>结果

enter image description here

enter image description here