SPFieldLookup.RelationshipDeleteBehavior Property
Gets or sets the delete behavior of the lookup field.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
Syntax
'Declaration
<ClientCallableAttribute> _
Public Property RelationshipDeleteBehavior As SPRelationshipDeleteBehavior
Get
Set
'Usage
Dim instance As SPFieldLookup
Dim value As SPRelationshipDeleteBehavior
value = instance.RelationshipDeleteBehavior
instance.RelationshipDeleteBehavior = value
[ClientCallableAttribute]
public SPRelationshipDeleteBehavior RelationshipDeleteBehavior { get; set; }
Property Value
Type: Microsoft.SharePoint.SPRelationshipDeleteBehavior
The delete behavior. Possible values include Cascade, Restrict and None.
Exceptions
Exception | Condition |
---|---|
SPException | You cannot set a value other than None if the lookup field represents a site column. |
Remarks
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.
Important
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 lookup field allows multiple values.
Before setting the RelationshipDeleteBehavior property, be sure that the AllowMultipleValues property returns false.
-
The lookup field points to a list in another Web site.
Check the value of the lookup field's LookupWebId property.
-
The number of items in the list exceeds the maximum set for large lists.
Compare the value returned by the list's ItemCount property with the value returned by the Web application's MaxItemsPerThrottledOperation property.
The current user must have SPBasePermissions.ManageLists permission on the target list when this property is set to Restrict.
Examples
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.
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 lookupList = site.Lists.TryGetList("Customers");
SPList relatedList = site.Lists.TryGetList("Pending Orders");
if (lookupList != null && relatedList != null)
{
string strPrimaryCol = relatedList.Fields.AddLookup("Customer ID", lookupList.ID, true);
SPFieldLookup primaryCol = (SPFieldLookup)relatedList.Fields.GetFieldByInternalName(strPrimaryCol);
primaryCol.LookupField = lookupList.Fields["ID"].InternalName;
primaryCol.Indexed = true;
primaryCol.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict;
primaryCol.Update();
}
}
}
Console.Write("\nPress ENTER to continue...");
Console.ReadLine();
}
}
}
Imports System
Imports Microsoft.SharePoint
Module ConsoleApp
Sub Main()
Using siteCollection As New SPSite("https://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