SPRelatedField class
Provides information about a lookup field on a child list that gets information from a parent list.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
In Microsoft SharePoint Foundation two lists become related when a user creates a lookup field on one list that takes its value from a field on another list. You can think of the list that provides data as the parent and the list that looks up data as the child. To discover which fields in child lists depend on information in a parent list, call the GetRelatedFields() method on an SPList object that represents the parent list.
The GetRelatedFields method returns a collection of SPRelatedField objects with the following properties:
LookupList. Gets the SPList object that represents the parent list.
WebId. Gets the ID of the Web site where the child list is located.
RelationshipDeleteBehavior. Gets the data integrity constraint defined for the list relationship.
The last property, RelationshipDeleteBehavior, represents an important aspect of list relationships. A user who creates a lookup field, either through the user interface or through the object model, can set constraints on how deletions from one list affect deletions from the other. For example, the user can specify that deletions from the parent list cascade to the child; that is, when an item is deleted from the parent list, all related items on the child list are also deleted. The user can also specify that deletions from the parent list are restricted; an item cannot be deleted from the parent list if related items exist on the child list. For more information, see the SPRelationshipDeleteBehavior enumeration.
Note |
|---|
In order to restrict deletions, the user must have SPBasePermissions.ManageLists permission on the parent list. |
In SharePoint Foundation a user can also create a multiple-column lookup. In this case, the child list has a primary column that defines the relationship with the parent list, and it has one or more secondary columns that read values from fields on the parent list but depend on the primary column for the list relationship. SPRelatedField objects contain information about only the primary column. To get information about secondary columns, call the GetDependentLookupInternalNames method of the SPFieldLookup object that represents the primary column.
The following example is a console application that reports information about fields in the Customers list that provide data to lookup fields in other lists. For each related field that represents a primary column, the application calls the GetDependentLookupInternalNames method to get a list of fields that represent secondary columns.
The console application prints output that resembles the following example.
====================================================================== Target List: Customers (Team Site) | Related List: Complete Orders (Team Site) Primary Column -------------------- Target Field: ID | Related Field: Customer ID Deletion behavior: None Secondary Columns -------------------- Target Field: Last Name | Related Field: Customer ====================================================================== Target List: Customers (Team Site) | Related List: Addresses (Team Site) Primary Column -------------------- Target Field: ID | Related Field: Customer ID Deletion behavior: Cascade Secondary Columns -------------------- Target Field: Last Name | Related Field: Last Name Target Field: First Name | Related Field: First Name ====================================================================== Target List: Customers (Team Site) | Related List: Pending Orders (Team Site) Primary Column -------------------- Target Field: ID | Related Field: Customer ID Deletion behavior: Restrict Secondary Columns -------------------- Target Field: First Name | Related Field: First Name Target Field: Last Name | Related Field: Last Name Target Field: Phone | Related Field: Phone ====================================================================== Press ENTER to continue...
Note