Getting SPListItems from a multi-value Lookup Column

I have put this example together because I wanted to demo how to get multiple SPListItems from a lookup column rather than just the one OR just the lookup value/text. Anyway, it’s a handy piece of code to have on hand because lookup columns are always a little tricky to deal with but they are also super useful. For example, I used the code below in a web part that filters displayed list items based on a complex filter defined in another list. So rather than having one column which defined a filter i.e. ‘User=Me’ I had a whole list of columns which each defined a specific filter: type, condition for applictaion, property and value.  

 public bool displayItem(SPListItem listItem)
        {
            bool displayItem = true;
            try
            {
                if (!String.IsNullOrEmpty(listItem[“PreferenceFilter”].ToString()))
                {
                    if (listItem.Fields[“PreferenceFilter”] is SPFieldLookup)
                    {
                        SPFieldLookup lookupField = (SPFieldLookup)listItem.Fields[“PreferenceFilter”];
                        Guid webID = lookupField.LookupWebId;
                        Guid listID = new Guid(lookupField.LookupList);
                        string[] splitStr = { “;”, “#” };// 2;#Filter1;#3;#Filter2              

  splitStr = listItem[“PreferenceFilter”].ToString().Split(splitStr, StringSplitOptions.RemoveEmptyEntries);
                        for (int i = 0; i < splitStr.Length; i = i + 2)
                        {
                            int itemID = Int32.Parse(splitStr[i]);
                            SPListItem lookupItem = SPContext.Current.Site.OpenWeb(webID).Lists[listID].GetItemById(itemID);
                            string filterType = lookupItem[“FilterType”].ToString();
                            string propName = lookupItem[“PropertyTitle”].ToString();
                            string propValue = lookupItem[“PropertyValue”].ToString();
                            if (FilterType… && PropertyTitle….)
                            {

                                 displayItem = false;
                              }

                                etc

Advertisements

4 thoughts on “Getting SPListItems from a multi-value Lookup Column

  1. Why complicate?

    SPFieldLookupValueCollection values = new SPFieldLookupValueCollection(item[“fieldname”].ToString());
    foreach (SPFieldLookupValue value in values)
    {
    ListItem item = new ListItem();
    item.Value = value.LookupId.ToString();
    item.Text = value.LookupValue;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s