Monday, September 26, 2011

SharePoint 2010 Setting Lookup Relationship Behavior (RelationshipDeleteBehavior)

If we want to enforce referential integrity in the relationship between two lists then we need to make use of RelationshipDeleteBehavior property of lookup field. 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.
A lookup field that enforces a deletion constraint must be indexed. That means we first need to set Indexed property of Lookup field to True.

See programmatically example below:

using(SPSite site = new SPSite("http://anmol-pc"))
using(SPWeb web = site.OpenWeb())

SPList list = web.Lists.TryGetList("MyChildList");
SPFieldLookup lookupField = (SPFieldLookup)list.Fields.GetFieldByInternalName("fieldname");
lookupField.Indexed = true;
lookupField.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Cascade;

lookupField.Required = true;
lookupField.LinkToItem = true;
lookupField.EnforceUniqueValues = false;


1 comment:

  1. Having the code is useful enough. It provides you alternative for function.

    small business consulting


Popular Posts