Question

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!

Was it helpful?

Solution

For each of the SPFields in the list's fields, you need to test the field's Type.

If the type is SPFieldType.MultiChoice, then you cast the SPField to SPFieldChoice and access the Choices collection, which is a StringCollection.

OTHER TIPS

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>();


 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top