How do I read LookUp fields with multiple values in SharePoint?

Matt Cashatt picture Matt Cashatt · Dec 6, 2011 · Viewed 17.8k times · Source

Hi and thanks for looking!

Background

I have inherited an old .NET project based on SharePoint 2007 and have designed and external core library which merely accesses SP data, therefore making SP just a backend. Yes, I know it would be better to migrate to SQL, but the client doesn't agree.

The previous developers used a simple read method to read the data in a SP list:

  SPList list = CurrentRootWeb.Lists["SomeListName"];

And then they access list properties through a dictionary of sorts (i.e. for each item in list, get item["SomeValue"]).

I am not skilled in SharePoint, so I don't know if this is the most efficient manner to go about accessing it's data.

Problem

How do I read LookUp fields with multiple values in SharePoint?

Every property they request seems to want a string in return. So item[SomeString] is okay, but item[SomeList] makes everything barf! I would have thought that a multi-value lookup list column comes in as a serialized or delimited string holding the selected values (example: "red;blue;green"). What am I missing?

Thanks!

Answer

justforkix09 picture justforkix09 · Dec 7, 2011

If you are specifically interested in LookUp fields with multiple values not just MultiChoice feilds, then following code should help:

item.Fields["LookFieldName"].Type == SPFieldType.Lookup;
SPFieldLookup LookUpField = item.Fields["LookFieldName"] as SPFieldLookup;
if (LookUpField.AllowMultipleValues)
{
    SPFieldLookupValueCollection valueCollection = item[Field.Id] as SPFieldLookupValueCollection;

    string[] arrLookupValues = (from SPFieldLookupValue val in valueCollection select val.LookupValue).ToArray<string>();


 }