Compartilhar via


SPFieldLookupValueCollection class

Contém os valores de um objeto de SPFieldLookup que pode conter vários valores.

Inheritance hierarchy

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

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaração
<SerializableAttribute> _
Public Class SPFieldLookupValueCollection _
    Inherits List(Of SPFieldLookupValue) _
    Implements ISerializable
'Uso
Dim instance As SPFieldLookupValueCollection
[SerializableAttribute]
public class SPFieldLookupValueCollection : List<SPFieldLookupValue>, 
    ISerializable

Examples

O exemplo a seguir mostra como ler o valor de um campo de pesquisa de valores múltiplos. O exemplo é um aplicativo de console que itera sobre os itens na lista de tarefas. O código analisa o campo Predecessoras em cada item e imprime o título do item, o número de predecessores e o título e o número de tarefas de cada predecessor.

using System;
using Microsoft.SharePoint;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://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();
        }
    }
}
Imports System
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()

        Using site As New SPSite("https://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

O exemplo a seguir mostra como gravar o valor de um campo de pesquisa de valores múltiplos. O exemplo é um aplicativo de console que procura por uma lista de problemas no site de raiz do conjunto de sites. Se for encontrada uma lista de questões, o aplicativo adiciona dois novos itens à lista e define o valor do campo itens relacionados em cada item.

using System;
using Microsoft.SharePoint;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://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();
                    }
                }
            }
        }
    }
}
Imports System
Imports Microsoft.SharePoint

Module ConsoleApp

    Sub Main()
        Using site As New SPSite("https://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

Thread safety

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

Ver também

Referência

SPFieldLookupValueCollection members

Microsoft.SharePoint namespace