Search code examples
sharepointmappingfieldtype

Microsoft.SharePoint.Client Online FieldType Mapping


I am attempting to push values into a SharePoint Online List. A couple questions:

  1. If I create this code using SharePoint online structures, will that also work for companies that have their own on-premise servers? Or do I have to create a whole new code base to support that also? Seems there are some large difference between SPFieldType and FieldType, or is the code automatically making that change?
  2. In mapping FieldType, I am looking for some reference or code that explains the proper mapping for the various field types. I have seen these for the Sister product SharePoint 2013, such as :

https://social.technet.microsoft.com/wiki/contents/articles/21801.sharepoint-a-complete-guide-to-getting-and-setting-fields-using-c.aspx

But the differences between 2013 SPFieldType and the Microsoft.SharePoint.Client.FieldType seem rather large. Is there something corresponding that describes what each field type requires for the values to be successful in FieldType?

Here is an example my existing code, and what I am still missing:

       public string ToString(Microsoft.SharePoint.Client.FieldType ColumnType)
    {
        switch (ColumnType)
        {
            case Microsoft.SharePoint.Client.FieldType.Boolean:
                // 0 or 1, or "true" or "false"???
                break;
            case Microsoft.SharePoint.Client.FieldType.Calculated:
                // No idea!
                break;
            case Microsoft.SharePoint.Client.FieldType.Choice:
                // No idea!
                break;
            case Microsoft.SharePoint.Client.FieldType.Computed:
                // No idea!
                break;
            case Microsoft.SharePoint.Client.FieldType.Currency:
                // No idea!
                break;
            case Microsoft.SharePoint.Client.FieldType.DateTime:
                return GetDateTimeValue().ToString("yyyy-MM-dd");

            case Microsoft.SharePoint.Client.FieldType.File:
                // No idea!
                break;
            case Microsoft.SharePoint.Client.FieldType.GridChoice:
                // No idea!
                break;
            case Microsoft.SharePoint.Client.FieldType.Guid:
                // No idea!
                break;
            case Microsoft.SharePoint.Client.FieldType.Integer:
                return GetIntValue().ToString(); 

            case Microsoft.SharePoint.Client.FieldType.Lookup:
                // No idea!
                break;
            case Microsoft.SharePoint.Client.FieldType.MultiChoice:
                // No idea!
                break;
            case Microsoft.SharePoint.Client.FieldType.Note:
                return GetStringValue();
            case Microsoft.SharePoint.Client.FieldType.Number:
                return GetDoubleValue().ToString();
            case Microsoft.SharePoint.Client.FieldType.Text:
                return GetStringValue();
            case Microsoft.SharePoint.Client.FieldType.User:
                // No idea!
                break;
            case Microsoft.SharePoint.Client.FieldType.URL:
                // No idea!
                break;
            // No Idea what to do for these
            case Microsoft.SharePoint.Client.FieldType.WorkflowEventType:
            case Microsoft.SharePoint.Client.FieldType.WorkflowStatus:
            case Microsoft.SharePoint.Client.FieldType.AllDayEvent:
            case Microsoft.SharePoint.Client.FieldType.Attachments:
            case Microsoft.SharePoint.Client.FieldType.ThreadIndex:
            case Microsoft.SharePoint.Client.FieldType.Threading:
            case Microsoft.SharePoint.Client.FieldType.PageSeparator:
            case Microsoft.SharePoint.Client.FieldType.Recurrence:
            case Microsoft.SharePoint.Client.FieldType.Invalid:
            case Microsoft.SharePoint.Client.FieldType.ContentTypeId:
            case Microsoft.SharePoint.Client.FieldType.Counter:
            case Microsoft.SharePoint.Client.FieldType.CrossProjectLink:
            case Microsoft.SharePoint.Client.FieldType.MaxItems:
            case Microsoft.SharePoint.Client.FieldType.ModStat:
            case Microsoft.SharePoint.Client.FieldType.Error:
                break;
            default:
                return GetStringValue();
        }
        return GetStringValue();
    }

The missing methods just convert values from my data into the appropriate strings.


Solution

  • Server Object Model and .NET Client Object Model are different. If you want to push values into a SharePoint Online List, I suggest you use Pnp- PowerShell to achieve it.

    Add-PnPListItem

    About the Field type, please check the code from GetField.

    If you don't want to use Pnp-PowerShell, you need convert the Server Object Model code to Client Object Model code.