AB. AB. - 1 month ago 10
C# Question

Check which field type a column belongs to programmatically in SharePoint

How can we check what is the field type of a column through code? For example, I know "Country" is one column in SharePoint and I am accessing it, but I don't know its type. If there is any way I can check it programmatically and then perform an action, for example if it is a Lookup Field, then if I want its value, i need to do...
lookupvalue country ...or if it's a Text Field, I can simply get its value as string.

Any idea how to get the field type?

Thanks.

Answer

Well i don't know if that is what you need.

but you can get the column type using this method:

        SPSite site = new SPSite("your site");
        SPWeb web = site.OpenWeb("your web");
        SPField field = web.Fields["field Name"];
        SPFieldType fieldType = field.Type;
        switch (fieldType)
        {
            case SPFieldType.AllDayEvent:
                break;
            case SPFieldType.Attachments:
                break;
            case SPFieldType.Boolean:
                break;
            case SPFieldType.Calculated:
                break;
            case SPFieldType.Choice:
                break;
            default:
                break;...
        }
Comments