如何从字段中提取类型?

时间:2015-09-11 11:27:09

标签: c# sharepoint-2013 csom

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

field.fieldvalueType

有时会为您提供类型(DateTime或其他)。令人讨厌的是,有时它只返回Null(例如,ID字段)。

在CSOM中,您没有该字段。但是,TypeAsString提供了SharePoint类型,例如:

  • 已计算
  • 整数
  • 注意

我想做的就是抓住这个huge table from MSDN

当我知道我正在处理“整数”字段时,提取“Int32”,并从SharePoint的笔记中提取“System.String”。

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

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

context.Load(myTempItem);
context.ExecuteQuery();

创建后,您可以使用:

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

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

4 个答案:

答案 0 :(得分:3)

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

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

用法

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

答案 1 :(得分:1)

扩展@Vadim的答案,这是一个每次调用扩展方法时都不构造新字典的版本;

namespace SharePoint.Client.Extensions
{
    public static class FieldExtensions
    {
        private static Dictionary<FieldType, Type> _fieldTypes = new Dictionary<FieldType, Type>()
        {
            { FieldType.Guid, typeof(Guid) },
            { FieldType.Attachments, typeof(bool)},
            {FieldType.Boolean, typeof(bool)},
            {FieldType.Choice, typeof(string)},
            {FieldType.CrossProjectLink, typeof(bool)},
            {FieldType.DateTime, typeof(DateTime)},
            {FieldType.Lookup, typeof(FieldLookupValue)},
            {FieldType.ModStat, typeof(int)},
            {FieldType.MultiChoice, typeof(string[])},
            {FieldType.Number, typeof(double)},
            {FieldType.Recurrence, typeof(bool)},
            {FieldType.Text, typeof(string)},
            {FieldType.URL, typeof(FieldUrlValue)},
            {FieldType.User, typeof(FieldUserValue)},
            {FieldType.WorkflowStatus, typeof(int)},
            {FieldType.ContentTypeId, typeof(ContentTypeId)},
            {FieldType.Note, typeof(string)},
            {FieldType.Counter, typeof(int)},
            {FieldType.Computed, typeof(string)},
            {FieldType.Integer, typeof(int)},
            {FieldType.File, typeof(string)}
        };

        public static Type GetFieldValueType(this Field field)
        {
            if (!_fieldTypes.ContainsKey(field.FieldTypeKind))
                throw new NotSupportedException(string.Format("Unknown field type: {0}", field.FieldTypeKind));
            return _fieldTypes[field.FieldTypeKind];
        }
    }
}

答案 2 :(得分:0)

通常,您需要执行您描述的映射,而不是myTempItemCreated[fieldImTryingToGetTypeOf.Title].GetType().FullName 方法

原因是,myTempItemCreated[fieldImTryingToGetTypeOf.Title].GetType().FullName 将在该字段值(例如Title)对于该特定ListItem对象为null的情况下失败。 (虽然Title通常不为null,但其他字段可以为null)。显然null不会为您提供一个可以调用GetType()方法的对象(显然会出现NullReferenceException)。

因此,对于问题的一般解决方案,确实必须映射从列表字段的TypeAsString返回的String,从列表对象/列表字段调用,而不是从列表项调用。

答案 3 :(得分:-1)

您可以使用以下代码段获取字段类型:

'String?' is not identical to 'AnyObject'