2

i am using CSOM 2013 and need the required data type of a ListItem-field.
Who can help?

4
  • Did you mean content type of item? Nov 26, 2013 at 10:42
  • No, the data type of a field in a list.
    – user21023
    Nov 26, 2013 at 11:34
  • So, the SPFieldType of the field? Nov 26, 2013 at 11:54
  • Please elaborate on your question in order for us to help answer your query.
    – Hugh Wood
    Nov 26, 2013 at 12:42

2 Answers 2

1

The client library does not know the real type of the returned object "field" and SharePoint.Field is the only possible type. If you know the real type, you can use the ClientContext.CastTo method to cast the object.

For Example

ClientContext context = new ClientContext("http://SiteUrl"); 
SP.List list = context.Web.Lists.GetByTitle("Announcements"); 
SP.Field field = list.Fields.AddFieldAsXml("<Field DisplayName='MyField2' Type='Number' />", 
                                           true, 
                                           AddFieldOptions.DefaultValue); 
SP.FieldNumber fldNumber = context.CastTo<FieldNumber>(field); 
fldNumber.MaximumValue = 100; 
fldNumber.MinimumValue = 35; 
fldNumber.Update(); 

context.ExecuteQuery();

Many examples using CSOM can be found on MSDN

1
  • OK, thanks for the answer. I'm not sure if we mean the same. I think this example is better for me: SPContext.Current.Web.Lists[Id].Fields[Id].TypeAsString
    – user21023
    Nov 26, 2013 at 11:31
1

You must get all fields using list.get_fields() then foreach field from collection get field type using var fType = oField.get_fieldTypeKind();

var clientContext = new SP.ClientContext.get_current();
        var web = clientContext.get_web();
        var list = web.get_lists().getByTitle(listName);
        this.listFields = list.get_fields();
        clientContext.load(this.listFields);
        clientContext.executeQueryAsync(Function.createDelegate(this, 
            this.onListFieldsQuerySucceeded), Function.createDelegate(this, 
            this.onListFieldsQueryFailed));

        function onListFieldsQuerySucceeded() {
            var fieldEnumerator = listFields.getEnumerator();
            while (fieldEnumerator.moveNext()) {
                var oField = fieldEnumerator.get_current();
                var fType = oField.get_fieldTypeKind();
                if(fType === SP.FieldType.choice) {
                   .....
                }
            }
        } 

Link to source

1
  • Please add some text to your answer, indicating what the code does and how to use it :) Nov 26, 2013 at 12:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.