SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)
       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())


share|improve this question
up vote 2 down vote accepted

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).

share|improve this answer
And like whot should look CAML Query to delete ? – Grzegorz Z Nov 28 '12 at 19:52
you cannot delete items with CAML query, but you can retrieve items for deletion. If you would like to delete items in one time you should use batch deletion. You can see this question for more details. – Alexander Nov 28 '12 at 20:15
You should use batch deletion if you have really a lot of items ) – Alexander Nov 28 '12 at 20:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.