Determine Whether a Column is a MultiValued Lookup Column in SharePoint


The following code snippet demonstrates how to check a column to determine if it is a MultiValued Lookup column.


How to check a column to determine if it is a MultiValued Lookup column.

protected
DropDownListddlParent
ArrayListobjLookupColumnCollection = newArrayList();
SPListobjList = SPContext.Current.Web.Lists[objSelectedList];
SPViewFieldCollectionobjSPViewFieldCollection = objList.DefaultView.ViewFields;
stringstrTest = objList.Views.SchemaXml;
SPFieldCollectionobjFieldCollection = objList.Fields;
foreach (SPFieldobjFieldinobjFieldCollection)
                {
if (IsValidField(objField))
                    {
if (objField.Type == SPFieldType.Lookup)
                        {
SPFieldLookuplookUpColumn = (SPFieldLookup)objField;
if(!lookUpColumn.AllowMultipleValues)
                            {
objLookupColumnCollection.Add(objField);
                            }
                        }
                    }
                }
privateboolIsValidField(SPField field)
        {
return (field.Id == SPBuiltInFieldId.FileLeafRef || field.Hidden == false&& (field.ReadOnlyField == false || field.Id ==
SPBuiltInFieldId.ID)
&& (
field.Type == SPFieldType.Counter
                        || field.Type == SPFieldType.Boolean
                        || field.Type == SPFieldType.Integer
                        || field.Type == SPFieldType.Currency
                        || field.Type == SPFieldType.DateTime
                        || field.Type == SPFieldType.Number
                        || field.Type == SPFieldType.Text
                        || field.Type == SPFieldType.URL
                        || field.Type == SPFieldType.User
                        || field.Type == SPFieldType.Choice
                        || field.Type == SPFieldType.MultiChoice
                        || field.Type == SPFieldType.Lookup
                        || field.Type == SPFieldType.File
                        || (field.Type == SPFieldType.Calculated && ((SPFieldCalculated)field).OutputType ==
SPFieldType.Text))
                        );
        }

Kindly Bind objLookupColumnCollection (ArrayList Collection) to any DropDownList to see output.
As example given below (ddlParent is defined above):

ddlParent.DataSource = objLookupColumnCollection;
ddlParent.DataBind();