How to extract a type from a field?

In the side code of SharePoint Server, you can write something like:

field.fieldvalueType 

which sometimes gives you a type (DateTime or something else). Annoyingly, sometimes it just returns Null (like an ID field).

In CSOM you do not have this field. However, there is a TypeAsString that provides SharePoint types, such as:

  • Computer
  • Integer
  • Note

What I would like to do is grab this huge table from MSDN :

And retrieve “Int32” when I know that I am dealing with the “Integer” field, and extract “System.String” from the SharePoint note.

This view works, but it’s the mother of all hacks:

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

After creating, you can use:

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

Now, what is the right way to do this? I just hope that the answer will not be a ten-digit expression about changing the operator.

+6
source share
4 answers

Because the SPField.FieldValueType property does not exist in the SharePoint CSOM API, the following extension method demonstrates how to execute it:

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

Using

 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); } } 
+3
source

Turning to @Vadim's answer, here is a version that does not create a new dictionary every time the extension method is called;

 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]; } } } 
+1
source

Typically, you need to perform the mapping that you describe, not myTempItemCreated[fieldImTryingToGetTypeOf.Title].GetType().FullName Method.

The reason is because myTempItemCreated[fieldImTryingToGetTypeOf.Title].GetType().FullName will fail in a script where the field value (for example, Title) is null for this particular ListItem object. (Although Title is not null, other fields can and are null). And, obviously, null does not give you an object that you can call the GetType () method on (you will obviously get a NullReferenceException).

So, for a general solution to the problem, you really need to display the string returned from TypeAsString in the list box, calling from the list box / list box, and not from the list item.

0
source

You can get the field type using the following snippet:

 item.Fields["Title"].FieldValueType.FullName 
-1
source

All Articles