Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

                   }

               }
           }
       }
   }
share|improve this question

1 Answer 1

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
2  
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
1  
You should use batch deletion if you have really a lot of items ) –  Alexander Nov 28 '12 at 20:58

Your Answer

 
discard

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.