SPFieldCollection.AddDependentLookup - Méthode

Ajoute un champ de recherche secondaire qui dépend d’un champ de recherche principal pour sa relation avec la liste dont il récupère ses informations.

Espace de noms :  Microsoft.SharePoint
Assembly :  Microsoft.SharePoint (dans Microsoft.SharePoint.dll)

Syntaxe

'Déclaration
Public Function AddDependentLookup ( _
    displayName As String, _
    primaryLookupFieldId As Guid _
) As String
'Utilisation
Dim instance As SPFieldCollection
Dim displayName As String
Dim primaryLookupFieldId As Guid
Dim returnValue As String

returnValue = instance.AddDependentLookup(displayName, _
    primaryLookupFieldId)
public string AddDependentLookup(
    string displayName,
    Guid primaryLookupFieldId
)

Paramètres

  • displayName
    Type : System.String

    Le nom d'affichage pour le champ de recherche secondaire.

  • primaryLookupFieldId
    Type : System.Guid

    La valeur de la propriété Id de l'objet SPFieldLookup qui représente le champ primaire.

Valeur renvoyée

Type : System.String
Le nom du champ interne.

Exceptions

Exception Condition
ArgumentNullException

La valeur passée dans le paramètre displayName est une chaîne vide ou null.

ArgumentNullException

La valeur passée dans le paramètre primaryLookupFieldId est Guid.Empty ou la valeur null.

SPException

Le champ identifié par la valeur transmise dans le paramètre primaryLookupFieldId n'existe pas, n'est pas de type SPFieldLookupou son IsDependentLookup propriété renvoie true.

Remarques

Dans une recherche de colonnes multiples, le champ principal est l'objet SPFieldLookup qui établit la relation avec la liste qui est la source de la valeur du champ liste de choix. Un ou plusieurs champs secondaires dépend puis le champ principal de leur relation avec la liste source. La liste source est conscient du champ primaire ; Autrement dit, vous pouvez découvrir le champ primaire en examinant les objets dans la collection retournée par la méthode de GetRelatedFields() de la liste source. Le champ primaire, à son tour, est informé de tous les champs qui en dépendent ; Vous pouvez découvrir des champs dépendants en appelant la méthode de GetDependentLookupInternalNames() du champ primaire.

Pour créer une recherche de plusieurs colonnes, commencez par créer le champ primaire en appelant la méthode AddLookup(String, Guid, Boolean) . Vous pouvez ensuite utiliser la méthode AddDependentLookup pour créer un ou plusieurs champs secondaires qui dépendent du champ primaire.

Exemples

L'exemple suivant est une application console qui obtient la collection des champs associés à la liste des commandes en attente et ajoute qu'un champ de recherche nommé code client qui pointe vers le champ ID dans la liste des clients. Le code crée ensuite un champ secondaire dépend du champ code client de sa relation à la liste des clients.

using System;
using Microsoft.SharePoint;

namespace RelatedLists
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite siteCollection = new SPSite("https://localhost"))
            {
                using (SPWeb site = siteCollection.OpenWeb())
                {
                    SPList sourceList = site.Lists["Customers"];
                    SPList dependentList = site.Lists["Pending Orders"];

                    // Create the primary column.
                    string strPrimaryCol = dependentList.Fields.AddLookup("Customer ID", sourceList.ID, true);
                    SPFieldLookup primaryCol = (SPFieldLookup)dependentList.Fields.GetFieldByInternalName(strPrimaryCol);
                    primaryCol.LookupField = sourceList.Fields["ID"].InternalName;
                    primaryCol.Indexed = true;
                    primaryCol.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict;
                    primaryCol.Update();

                    // Create the secondary column.
                    string strSecondaryCol = dependentList.Fields.AddDependentLookup("Last Name", primaryCol.Id);
                    SPFieldLookup secondaryCol = (SPFieldLookup)dependentList.Fields.GetFieldByInternalName(strSecondaryCol);
                    secondaryCol.LookupField = sourceList.Fields["Last Name"].InternalName;
                    secondaryCol.Update();

                    // Make the primary column the first one on New and Edit forms.
                    SPContentType contentType = dependentList.ContentTypes[0];
                    SPFieldLinkCollection fieldRefs = contentType.FieldLinks;
                    fieldRefs.Reorder(new[] { primaryCol.InternalName });
                    contentType.Update();
                    

                    // Add the columns to the default view.
                    SPView view = dependentList.DefaultView;
                    if (view != null)
                    {
                        SPViewFieldCollection viewFields = view.ViewFields;
                        if (viewFields != null)
                        {
                            viewFields.Add(primaryCol);
                            viewFields.Add(secondaryCol);
                            viewFields.MoveFieldTo(primaryCol.InternalName, 0);
                            viewFields.MoveFieldTo(secondaryCol.InternalName, 1);
                            view.Update();
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }
    }
}
Imports System
Imports Microsoft.SharePoint

Namespace RelatedLists
    Class Program
        Shared Sub Main(ByVal args As String())
            Using siteCollection As New SPSite("https://localhost")
                Using site As SPWeb = siteCollection.OpenWeb()
                    Dim sourceList As SPList = site.Lists("Customers")
                    Dim dependentList As SPList = site.Lists("Pending Orders")

                    ' Create the primary column.
                    Dim strPrimaryCol As String = dependentList.Fields.AddLookup("Customer ID", sourceList.ID, True)
                    Dim primaryCol As SPFieldLookup = DirectCast(dependentList.Fields.GetFieldByInternalName(strPrimaryCol), SPFieldLookup)
                    primaryCol.LookupField = sourceList.Fields("ID").InternalName
                    primaryCol.Indexed = True
                    primaryCol.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict
                    primaryCol.Update()

                    ' Create the secondary column.
                    Dim strSecondaryCol As String = dependentList.Fields.AddDependentLookup("Last Name", primaryCol.Id)
                    Dim secondaryCol As SPFieldLookup = DirectCast(dependentList.Fields.GetFieldByInternalName(strSecondaryCol), SPFieldLookup)
                    secondaryCol.LookupField = sourceList.Fields("Last Name").InternalName
                    secondaryCol.Update()

                    'Make the primary column the first one on New and Edit forms.
                    Dim contentType As SPContentType = dependentList.ContentTypes(0)
                    Dim fieldRefs As SPFieldLinkCollection = contentType.FieldLinks
                    fieldRefs.Reorder(New String() {primaryCol.InternalName})
                    contentType.Update()

                    ' Add the columns to the default view.
                    Dim view As SPView = dependentList.DefaultView
                    If view <> Nothing Then
                        Dim viewFields As SPViewFieldCollection = view.ViewFields
                        If viewFields <> Nothing Then
                            viewFields.Add(primaryCol)
                            viewFields.Add(secondaryCol)
                            viewFields.MoveFieldTo(primaryCol.InternalName, 0)
                            viewFields.MoveFieldTo(secondaryCol.InternalName, 1)
                            view.Update()
                        End If
                    End If
                End Using
            End Using
            Console.Write(vbLf & "Press ENTER to continue...")
            Console.ReadLine()
        End Sub
    End Class
End Namespace

Voir aussi

Référence

SPFieldCollection classe

SPFieldCollection - Membres

Microsoft.SharePoint - Espace de noms

AddLookup(String, Guid, Boolean)

IsDependentLookup

GetDependentLookupInternalNames()

GetRelatedFields()