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

'Declaration
<SerializableAttribute> _
Public Class SPFieldLookupValueCollection _
	Inherits List(Of SPFieldLookupValue) _
	Implements ISerializable
'Usage
Dim instance As SPFieldLookupValueCollection

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.

Imports System
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()

        Using site As New SPSite("http://localhost")
            Using web As SPWeb = site.OpenWeb()

                Dim list As SPList = web.Lists.TryGetList("Tasks")

                If list IsNot Nothing Then
                    For Each item As SPListItem In list.Items
                        ' Get the predecessors.
                        Dim rawvalue As String = item(SPBuiltInFieldId.Predecessors).ToString()

                        ' Print information about the task.
                        Dim values As New SPFieldLookupValueCollection(rawvalue)
                        Console.WriteLine(vbLf & "Task {0}: {1}", item.ID, item.Title)
                        Console.WriteLine(vbTab & "Predecessors: {0}", values.Count)

                        ' Print the predecessors.
                        For Each value As SPFieldLookupValue In values
                            Console.WriteLine(vbTab & "{0} (Task {1})", value.LookupValue, value.LookupId)
                        Next
                    Next
                End If

            End Using
        End Using

        Console.Write(vbCrLf & "Press ENTER to continue....")
        Console.Read()
    End Sub

End Module

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.

Imports System
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()
        Using site As New SPSite("http://localhost")
            Using web As SPWeb = site.OpenWeb()

                Dim list As SPList = Nothing
                For Each webList As SPList In web.Lists
                    If webList.BaseType = SPBaseType.Issue Then
                        list = webList
                        Exit For
                    End If
                Next

                If list IsNot Nothing Then
                    Dim items As SPListItemCollection = list.Items
                    Dim relatedItems As New SPFieldLookupValueCollection()

                    Dim firstItem As SPListItem = items.Add()
                    firstItem(SPBuiltInFieldId.Title) = "The first issue"
                    firstItem.Update()

                    Dim firstLookupValue As New SPFieldLookupValue(firstItem.ID, firstItem.Title)
                    relatedItems.Add(firstLookupValue)

                    Dim secondItem As SPListItem = 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()
                End If

            End Using
        End Using
    End Sub
End Module


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show: