0

How can I delete a elements in secound list bounded with secand list if I delete elements in first list.

I write a code, but i don't know how to delete a bounded item, please help:

 public override void ItemAttachmentDeleting(SPItemEventProperties properties)
   {
       base.ItemAttachmentDeleting(properties);
       if (properties.List.Title == "Wpisy")
       {
           SPSite oSiteCollection = new SPSite("http://wasp03/");
           SPWeb witryna = oSiteCollection.RootWeb;
           SPListItemCollection firstList = witryna.Lists["Wpisy"].Items;
           SPListItemCollection secoundList = witryna.Lists["Szczegoly"].Items;
           foreach (SPListItem ofirstList in firstList)
           {
               foreach (SPListItem osecoundList in secoundList)
               {
                   if (ofirstList.ID.ToString() == osecoundList["ID_z_Wpisy"].ToString())
                   {

                   }

               }
           }
       }
   }
Grzegorz Z
  • 1,709
  • 7
  • 42
  • 73

1 Answers1

2

Fist of all, you should dispose oSiteCollection object. Second, if you will have a lot of items the foreach operation will be very slow, you should use CAML query to retrive items. Third, I think that you can use SPLookupField to set references between lists and use RelationShipsDeleteBehavior (also you can use this approach without any line of code, you can add this field to the list through list settings option).

Alexander
  • 8,139
  • 2
  • 27
  • 42