This topic has not yet been rated - Rate this topic

SPFieldLookupValueCollection Class

Contains the values for an SPFieldLookup object that can contain multiple values.

System.Object
  System.Collections.Generic.List<SPFieldLookupValue>
    Microsoft.SharePoint.SPFieldLookupValueCollection

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
[SerializableAttribute]
public class SPFieldLookupValueCollection : List<SPFieldLookupValue>, 
	ISerializable

The following example shows how to read the value of a multi-value lookup field. The example is a console application that iterates over the items on the Task list. The code looks at the Predecessors field in each item and prints the title of the item, the number of predecessors, and the title and task number of each predecessor.

using System;
using Microsoft.SharePoint;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists.TryGetList("Tasks");
                    if (list != null)
                    {
                        foreach (SPListItem item in list.Items)
                        {
                            // Get the predecessors.
                            string rawvalue = item[SPBuiltInFieldId.Predecessors].ToString();

                            // Print information about the task.
                            SPFieldLookupValueCollection values = new SPFieldLookupValueCollection(rawvalue);
                            Console.WriteLine("\nTask {0}: {1}", item.ID, item.Title);
                            Console.WriteLine("\tPredecessors: {0}", values.Count);

                            // Print the predecessors.
                            foreach (SPFieldLookupValue value in values)
                                Console.WriteLine("\t{0} (Task {1})", value.LookupValue, value.LookupId);
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue....");
            Console.Read();
        }
    }
}

The following example shows how to write the value of a multi-value lookup field. The example is a console application that looks for an Issues list in the site collection's root website. If an Issue list is found, the application adds two new items to the list and sets the value of the Related Items field in each item.

using System;
using Microsoft.SharePoint;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SPList list = null;
                    foreach (SPList webList in web.Lists)
                    {
                        if (webList.BaseType == SPBaseType.Issue)
                        {
                            list = webList;
                            break;
                        }
                    }
                    if (list != null)
                    {
                        SPListItemCollection items = list.Items;
                        SPFieldLookupValueCollection relatedItems = new SPFieldLookupValueCollection();

                        SPListItem firstItem = items.Add();
                        firstItem[SPBuiltInFieldId.Title] = "The first issue";
                        firstItem.Update();

                        SPFieldLookupValue firstLookupValue = new SPFieldLookupValue(firstItem.ID, firstItem.Title);
                        relatedItems.Add(firstLookupValue);

                        SPListItem secondItem = items.Add();
                        secondItem[SPBuiltInFieldId.Title] = "The second issue";
                        secondItem[SPBuiltInFieldId.RelatedIssues] = relatedItems.ToString();
                        secondItem.Update();

                        relatedItems.Remove(firstLookupValue);
                        relatedItems.Add(new SPFieldLookupValue(secondItem.ID, secondItem.Title));

                        firstItem[SPBuiltInFieldId.RelatedIssues] = relatedItems.ToString();
                        firstItem.Update();
                    }
                }
            }
        }
    }
}
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
First code sample is incorrect
Following expression is incorrect and just does not work:

SPFieldLookupValueCollection values = new SPFieldLookupValueCollection(rawvalue);

Instead of that use unboxing like this:

SPFieldLookupValueCollection values = (SPFieldLookupValueCollection)rawvalue;

For more information see description of SPFieldLookup class (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldlookup.aspx): "A lookup field can have multiple values if the AllowMultipleValues property returns true. In this case, the field value is an object of type SPFieldLookupValueCollection that is boxed as Object. You can get all the values by first casting the raw field value to SPFieldLookupValueCollection and then enumerating the SPFieldLookupValue objects in the collection."