Gets or sets the delete behavior of the lookup field.
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
'Declaration Public Property RelationshipDeleteBehavior As SPRelationshipDeleteBehavior Get Set 'Usage Dim instance As SPFieldLookup Dim value As SPRelationshipDeleteBehavior value = instance.RelationshipDeleteBehavior instance.RelationshipDeleteBehavior = value
Property valueType: Microsoft.SharePoint.SPRelationshipDeleteBehavior
The delete behavior. Possible values include Cascade, Restrict and None.
You cannot set a value other than None if the lookup field represents a site column.
The RelationshipDeleteBehavior property enables you enforce referential integrity in the relationship between two lists when one list depends on another list. If a lookup field in the dependent list has the RelationshipDeleteBehavior property set to Cascade, then deleting an item from the source list causes all related dependent list items to be deleted as well. For example, suppose you have a Customers list that is related to an Addresses list by a lookup field in the Addresses list. You might want deleting an item from Customers to cause all related items from Addresses to be deleted as well. You could accomplish that by setting the RelationshipDeleteBehavior property of the lookup field in the Addresses list to Cascade.
Setting the RelationshipDeleteBehavior property to Restrict prevents an item in the source list from being deleted if any items in the dependent list look up to it. For example, suppose your Customers list is the source for a lookup field in the Pending Orders list. You might not want an item from the Customers list to be deleted if the customer has pending orders. In that case, you could set the RelationshipDeleteBehavior property for the lookup field in Pending Orders to Restrict.
A lookup field that enforces a deletion constraint must be indexed. Before setting the RelationshipDeleteBehavior property to Cascade or Restrict, first set the Indexed property to true.
The user must have ManageLists permission on the source list in order to specify either Cascade or Restrict. For more information, see the SPBasePermissions enumeration.
In addition, you cannot specify a deletion constraint if:
The current user must have SPBasePermissions.ManageLists permission on the target list when this property is set to Restrict.
The following example is a console application that creates a link between two lists, Customers and Pending Orders. The lists are linked by using the ID field in the Customers list as the source for a new Customer ID field in the Pending Orders list. In order to prevent a Customers list item from being deleted if it has any Pending Orders list items looking up to it, the RelationshipDeleteBehavior property of the new Customer ID field is set to Restrict.
Imports System Imports Microsoft.SharePoint Module ConsoleApp Sub Main() Using siteCollection As New SPSite("http://localhost") Using site As SPWeb = siteCollection.OpenWeb() Dim lookupList As SPList = site.Lists.TryGetList("Customers") Dim relatedList As SPList = site.Lists.TryGetList("Pending Orders") If lookupList IsNot Nothing AndAlso relatedList IsNot Nothing Then Dim strPrimaryCol As String = relatedList.Fields.AddLookup("Customer ID", lookupList.ID, True) Dim primaryCol As SPFieldLookup = _ DirectCast(relatedList.Fields.GetFieldByInternalName(strPrimaryCol), SPFieldLookup) primaryCol.LookupField = lookupList.Fields("ID").InternalName primaryCol.Indexed = True primaryCol.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict primaryCol.Update() End If End Using End Using Console.Write(vbLf & "Press ENTER to continue...") Console.ReadLine() End Sub End Module