Check which field type a column belongs to programmatically in SharePoint
Ho开发者_Go百科w 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.
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;...
}
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
精彩评论