How to: Programmatically Set the Delete Behavior on a Lookup Field

Overview

Microsoft® SharePoint® Server 2010 lets you enforce a delete behavior between related lists. You can cascade deletions, so that if a user deletes an item from one list, child items in a related list are also deleted. You can also restrict deletions, so that users are prevented from deleting items in one list that are referenced by items in another list. This how-to topic shows you how to programmatically define the delete behavior between related lists.

For a practical example of programmatically defining the delete behavior between related lists, see Reference Implementation: SharePoint List Data Models.

Note

Note: This how-to topic assumes that you have created a project in Microsoft Visual Studio® 2010 by using the Empty SharePoint Project template and that the project is a Farm Solution. This topic also assumes that you have already created a feature that deploys a list instance with a lookup.

Summary of Steps

This procedure creates a feature receiver that programmatically sets the delete behavior on the lookup field. This how-to topic includes the following steps:

  • Step 1: Create a Feature Receiver. In this step, you create the feature receiver on the feature that deploys a list instance containing a lookup column.
  • Step 2: Configure the Receiver to Restrict Deletions. In this example, you set the delete behavior to restrict deletions, which prevents users from deleting items from related lists that are referenced by the lookup column. You could use the same approach to set the delete behavior to cascade deletions to the related list.

Step 1: Create a Feature Receiver

To create a feature receiver that programmatically sets the delete behavior on a lookup field

  1. In the Solution Explorer window, right-click the feature that deploys the list instance containing the lookup column, and then click Add Event Receiver.

  2. Open the corresponding <FeatureName>.EventReceiver.cs file that is generated.

  3. Add the following method to the class.

    private string GetListUrl(string webRelativeUrl, string listUrl)
    {
         if (webRelativeUrl[webRelativeUrl.Length - 1] != '/') return
              (webRelativeUrl + '/' + listUrl);
         else return (webRelativeUrl + listUrl);
    }
    

    Note

    Note: This method accepts two arguments. The webRelativeUrl argument is the SPWeb.ServerRelativeUrl property. The listUrl argument is the relative URL to the list.
    Essentially this method ensures that the relative URL to the list is correct, regardless of whether the SPWeb object represents a root site or a sub-site.
    This method is called by other methods in the event receiver.

Step 2: Configure the Receiver to Restrict Deletions

  1. Add the following method to the class that you created in step 1.

    private void RestrictDeleteOnLookupField(SPWeb web, string listUrl, Guid fieldGuid)
    {
        SPList list = web.GetList(GetListUrl(web.ServerRelativeUrl, listUrl));
        SPField field = list.Fields[fieldGuid];
        SPFieldLookup fieldLookup = (SPFieldLookup)field;
        fieldLookup.Indexed = true;
        fieldLookup.RelationshipDeleteBehavior =    
            SPRelationshipDeleteBehavior.Restrict;
        fieldLookup.Update();
    }
    

    Note

    Note: This method accepts three arguments. The Web argument is the SPWeb where the list resides. The listUrl argument is the relative URL to the list that contains the lookup column. The fieldGuid argument represents the ID of the lookup field to set the delete behavior on.
    This method sets the delete behavior for a lookup field to SPRelationshipDeleteBehavior.Restrict. This value turns on the restricted delete behavior for the lookup column and the lists it relates to. You could also set the delete behavior to SPRelationshipDeleteBehavior.Cascade.
    This method is called by other methods in the event receiver.

  2. Remove the comment from the FeatureActivated method in the class.

  3. Add the following code to the FeatureActivated method.

    try
    {
        SPSite thisSite = properties.Feature.Parent as SPSite;
        SPWeb rootWeb = thisSite.RootWeb;               
        RestrictDeleteOnLookupField(rootWeb, 
                                    <ListUrl>,
                                    <LookupFieldGUID>);                
    }
    catch (Exception e) 
    {
        System.Diagnostics.Trace.WriteLine(e.ToString()); 
    }
    

    Note

    Note: <ListUrl> is a placeholder for the relative URL to the list that contains the lookup column. <LookupFieldGUID> is the ID for the lookup field on which to set the delete behavior.