如何从字段中提取类型?

Dav*_*vid 7 c# sharepoint-2013 csom

在SharePoint Server端代码中,您可以编写如下内容:

field.fieldvalueType
Run Code Online (Sandbox Code Playgroud)

有时会给你类型(DateTime,或其他).令人讨厌的是,有时,它只返回Null(例如,ID字段).

在CSOM中,您没有该字段.但是,有以下TypeAsString类型的SharePoint类型:

  • 计算
  • 整数
  • 注意

我想做的是从MSDN中获取这个巨大的表格:

当我知道我正在处理"整数"字段时,提取"Int32",并从SharePoint的注释中提取"System.String".

这种方法有效,但它是所有黑客的母亲:

var myTempItem = list.AddItem(new ListItemCreationInformation());
myTempItem.Update();
context.ExecuteQuery();

context.Load(myTempItem);
context.ExecuteQuery();
Run Code Online (Sandbox Code Playgroud)

创建后,您可以使用:

myTempItemCreated[fieldImTryingToGetTypeOf.Title].GetType().FullName - > Gives-> System.Int32

现在,做到这一点的正确方法是什么?我只是希望答案不是十英尺长的开关案例声明.

Vad*_*hev 3

由于 SharePoint CSOM API 中没有SPField.FieldValueType 属性对应项,因此以下扩展方法演示了如何执行它:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.Client;
using Field = Microsoft.SharePoint.Client.Field;

namespace SharePoint.Client.Extensions
{
    public static class FieldExtensions
    {


        public static Type GetFieldValueType(this Field field)
        {
            var table = new Dictionary<FieldType, Type>();
            table[FieldType.Guid] = typeof(Guid);
            table[FieldType.Attachments] = typeof(bool);
            table[FieldType.Boolean] = typeof(bool);
            table[FieldType.Choice] = typeof (string);
            table[FieldType.CrossProjectLink] = typeof(bool);
            table[FieldType.DateTime] = typeof(DateTime);
            table[FieldType.Lookup] = typeof(FieldLookupValue);
            table[FieldType.ModStat] = typeof(int);
            table[FieldType.MultiChoice] = typeof(string[]);
            table[FieldType.Number] = typeof(double);
            table[FieldType.Recurrence] = typeof(bool);
            table[FieldType.Text] = typeof(string);
            table[FieldType.URL] = typeof(FieldUrlValue);
            table[FieldType.URL] = typeof(FieldUrlValue);
            table[FieldType.User] = typeof(FieldUserValue);
            table[FieldType.WorkflowStatus] = typeof(int);
            table[FieldType.ContentTypeId] = typeof(ContentTypeId);
            table[FieldType.Note] = typeof(string);
            table[FieldType.Counter] = typeof(int);
            table[FieldType.Computed] = typeof(string);
            table[FieldType.Integer] = typeof(int);
            table[FieldType.File] = typeof(string);

            if (!table.ContainsKey(field.FieldTypeKind))
                throw new NotSupportedException(string.Format("Unknown field type: {0}", field.FieldTypeKind));
            return table[field.FieldTypeKind];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法

var list = ctx.Web.Lists.GetByTitle(listTitle);
var fields = list.Fields;
ctx.Load(fields);
ctx.ExecuteQuery();

foreach (var field in fields)
{
    if (field.FieldTypeKind != FieldType.Invalid)
    {
         var fieldValueType = field.GetFieldValueType();
         Console.WriteLine("{0} : {1}", field.InternalName, fieldValueType);    
    }        
}
Run Code Online (Sandbox Code Playgroud)