【问题标题】:How do I extract the type from a field?如何从字段中提取类型?
【发布时间】:2015-12-07 22:48:25
【问题描述】:

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

field.fieldvalueType

这有时会给你类型(日期时间,或其他)。烦人的是,有时它只返回 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 -> 给予-> System.Int32

现在,正确的方法是什么?我只是希望答案不是十英尺长的 switch case 语句。

【问题讨论】:

    标签: c# sharepoint-2013 csom


    【解决方案1】:

    可以使用如下sn-p获取字段的类型:

    item.Fields["Title"].FieldValueType.FullName
    

    【讨论】:

    • 抱歉,我对需要什么感到困惑?你想得到字段的值吗? i,e: 只需调用一个方法来获取值,它会根据它的类型返回值?
    • 对不起,问题很清楚。你的回答是字面意思问题的第二行(这显然很令人沮丧)。我需要在不创建列表项的情况下获取字段的类型。阅读问题以解释为什么您的 sn-p 不起作用。
    【解决方案2】:

    一般来说,你需要做你描述的映射,而不是myTempItemCreated[fieldImTryingToGetTypeOf.Title].GetType().FullName 方法。

    原因是,myTempItemCreated[fieldImTryingToGetTypeOf.Title].GetType().FullName 在特定 ListItem 对象的字段值(例如 Title)为 null 的情况下将失败。 (虽然 Title 通常不为 null,但其他字段可以为 null)。显然 null 并没有给你一个可以调用 GetType() 方法的对象(你显然会得到一个 NullReferenceException)。

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

    【讨论】:

      【解决方案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);    
          }        
      }
      

      【讨论】:

      【解决方案4】:

      扩展@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];
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-26
        • 2022-01-08
        • 1970-01-01
        • 2020-11-27
        • 1970-01-01
        • 2020-01-04
        • 2021-02-21
        • 2020-12-15
        相关资源
        最近更新 更多