简体   繁体   中英

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

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 (ie 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!

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


 }

For each of the SPField s 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM