Take the 2-minute tour ×

i have got two Lists {"Wpisy", "Szczegoly"} And if I delete record in Wpisy I want to delete all corelate records by days in "Szczegoly", and worker schoud be the same, and Kontrakt schould be the same. In Szczegóły are the list of elements wchich are betwen start date and stop date in Wpisy. enter image description here

So when I deleting one item from Wpisy, I want to delete all elements in the Szczegóły which are corelate with the Wpisy. Here is my Code. I thin i could lose one loop, which are iterate to the list wpisy. Please help where schould I put this loop.

    public override void ItemAttachmentDeleting(SPItemEventProperties properties)
       if (properties.List.Title == "Wpisy")
           SPList mainList = properties.Web.Lists["Wpisy"];
           SPList lstOtherList = properties.Web.Lists["Szczegoly"];
           // Stworzenie dwóch zmiennych typu data, jednej opisującej dzień Startowy, drugiej opisującej dzień końcowy
           DateTime dzienStart = new DateTime();
           DateTime dzienStop = new DateTime();
           // Podstawienie odpowiednich pól z propetisów pod wcześniej stworzone zmienne
           dzienStart = Convert.ToDateTime(properties.AfterProperties["DzienStart"]).ToUniversalTime();
           dzienStop = Convert.ToDateTime(properties.AfterProperties["DzienStop"]).ToUniversalTime();
           while (dzienStart <= dzienStop)
               for (int j = 1; j <= lstOtherList.ItemCount; j++)
                   if (Convert.ToDateTime(lstOtherList.Items[j]["Dzien"].ToString()).ToUniversalTime() == dzienStart && lstOtherList.Items[j]["Pracownik"].ToString() == mainList.Items[i]["Pracownik1"].ToString() && lstOtherList.Items[j]["Kontrakt"].ToString() == mainList.Items[i]["Kontrakt1"].ToString())

               dzienStart = dzienStart.AddDays(1.0);
       } // Koniec Ifa dotyczącego Listy "Wpisy"
   } // Koniec ItemAttachmentDeleting

I don't know that I'm doing good to call by the element: lstOtherList.Items[j]["Pracownik"].ToString()

share|improve this question
add comment

2 Answers

  1. Don't use Item Attachment handlers. Those are for acting when attachments are manipulated, but you seem to be trying to act when the actual item is deleted. It looks like you should be using ItemDeleted.

  2. Since you are manipulating items in other lists and acting on potentially many items, you should consider using ItemDeleted (the asynchronous version) instead of ItemDeleting (synchronous) as you are now.

  3. You have just tested if your event list is "Wpisy", do not re-open it again through Web.Lists.

  4. Always open lists through their URL instead of their display names. Use SPWeb.GetList( ).

  5. No need to initialize your Start/Stop DateTimes to blank values if you are right away assigning their wanted value on the next line.

  6. Using a for loop on lists is a big no-no, both for performance reasons, and in your case you cannot assume that mainList.Items[i] and OtherList.Items[j] have any kind of correlation. Actually you are not incrementing i so I think you just put this loop to illustrate what you were looking for?

Instead of iterating through all items in both lists, you should use a CAML query on your Details list, using parameters from your deleted item that you are receiving in the event handler:

Where Contract is properties.Item["Kontrakt"]
AND Employee is properties.Item["Pracownik"]
AND Day is properties.Item["Dzien"]

*(Note that this isn't actual CAML, just to illustrate. You'll have to modify it. Specifically, you probably need to use a date range instead of checking directly for Dzien, e.g. > Dzien - 1 AND < Dzien + 1).*

Once you have the collection of all items that you want to delete, you should use CAML batch delete. For example, look at this blog post. You can just insert your query instead of getting all items in the list as he does in his example code.

This two step process will provide better constant performance as the number of items in your two list grow.

share|improve this answer
How learn CAML. Are You have got any Tutorial? I got a U2U CAML Query Buldier. I see there is a lot of option. Have You see any tutorial for U2U ? –  Grzegorz Z Oct 24 '12 at 11:54
add comment

If I understand you correctly. You can create relation between lists and use cascade delete. You dont need any additional code for that.

Here you have example how to setup relationa lists in SP2010:


share|improve this answer
add comment

Your Answer


