AB. AB. - 9 months ago 64
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?


Answer Source

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:
            case SPFieldType.Attachments:
            case SPFieldType.Boolean:
            case SPFieldType.Calculated:
            case SPFieldType.Choice: