tags:

views:

110

answers:

2

Hi,

How we can check what is the field type of a column through code . For e.g 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 do the action like may be if its lookup then If i want its value i need to do... lookupvalue country ...or if its a text i can simply get its value in string

Any idea how to know this...

Thanks,

+1  A: 

You can use the following snippet to get the information about the field type

                SPContext.Current.Web.Lists["X"].Fields["Country"].Type
                SPContext.Current.Web.Lists["X"].Fields["Country"].TypeAsString
                Enum SPFieldType //Should help you to compare the type with the built in types
Kusek
+2  A: 

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;...
        }
Gaby