locked
Parent list and Child list with EventHandler RRS feed

  • Question

  • Hi,

    I'm creating Two Lists using Feature.(Parent List and Child List),

    Then i have added Items in Parent List, and Child list,

    Child list are refering Parent list Title field,

    When i deleting Parent list item if there is any item in child list referring parent list,

    i want to show error message.

    Please help me ASAP thanks in advance.

    I'M using SharePoint 2007 and .NET 3.5

    ---

    Thankyou,

    Bala

     

    Saturday, October 29, 2011 7:59 AM

Answers

All replies

  • Create an event receiver ItemDeleting on the Parent List and there check if the child list is referring that item or not. If it referring then cancel the deletion by using
    public override void ItemDeleting(SPItemEventProperties properties)
           {
              SPListItem item = properties.ListItem; 
     
              //Your logic to check if exists in child list
    
              if(exists in child list)
             {
                  properties.ErrorMessage = "Cannot delete";
                  properties.Status = SPEventReceiverStatus.CancelWithError;
                 properties.Cancel = true;
              }
    }
    
    


    Have a look at the following link for reference as to how to create an event receiver http://koenvosters.wordpress.com/2009/07/31/howto-create-an-event-handler-for-sharepointmoss-2007/

     

    Saturday, October 29, 2011 8:53 AM
  • Hi,

    Thanks for your replay,

    Can you please explain briefly, It's good with example.

    Thanks,

    Bala

    Wednesday, November 2, 2011 10:24 AM
  • public override void ItemDeleting(SPItemEventProperties properties)
           {
             //this will give you the parent list item that you are deleting
              SPListItem item = properties.ListItem; 
     
              //Your logic to check if exists in child list
             SPList childList = web.Lists["Child List Name"];
             SPQuery query = new SPQuery();
             query.Query = "Query Exp";
    
             SPListItemCollection items = childList.GetItems(query);
    
              if(items.Count >0)
             {
                  properties.ErrorMessage = "Cannot delete";
                  properties.Status = SPEventReceiverStatus.CancelWithError;
                 properties.Cancel = true;
              }
    }
    
    


    Here when you try to delete some item from the Parent list , then an event will fire i.e. ItemDeleting. So here you will see if your child list contains any item that is referring to the item that is getting deleted in the parent list.

    SPQuery will contain some criteria based on which you can see if that item is referred in child list, then if you get a count that is greater than 0 you know that the reference is there and then you can simply cancel the deletion event and display the error message about the dependency.

    Hope it helps !

    Wednesday, November 2, 2011 11:12 AM