locked
Item Deleting Event Handler RRS feed

  • Question

  • Hai All,

    I need to create an event handler for Item Deleting. I have two SPList one is main list and another is the temporary list. These two list contains same fields. I just create an event handler for Item Adding. when i add any item in the main list my event handler is triggred and same data is added to my temporary list.

    But now i need to do an Item Deleting Event handler. When the user delete any item from the main list i need to remove the same item which are in the temporary list. For which i need to create an event handler for deleting the paricular item from my temporay list.

    Can anybody get me suggetions, also if posssible get me some code samples.

    Thanx in advance.

     
    Tuesday, January 13, 2009 1:48 PM

Answers

  • This should help;

    http://www.alexbruett.net/?p=10
    Blog; http://www.the-north.com/sharepoint --- Posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, January 13, 2009 2:18 PM
  • Ok If I correctly understand, u need delete same item from temp list after/during deleting item from main list.
    I think that in you case u need delete item from temp list on ItemDeleted Event because during deleting item from main list may be occur some error and etc. and in any case u will delete item from temp list...

    in the second place, when u'll create EventHadler u need deploy one via Feature and in the Feature.xml create paramter with temp list name
    <Properties>
        <Property Key="tmpListName" Value="TempList" />
    </Properties>
    when Feature activate save it parameter :
    ...
    list.ParentWeb.Properties.Add("ParamName", properties.Feature.Properties["PreviewLibName"].Value)
    ...

    and then use parameter in the Item EventHandler:

    SPDocLib.ParentWeb.Properties["ParamName"]

    Tuesday, January 13, 2009 2:32 PM

All replies

  • This should help;

    http://www.alexbruett.net/?p=10
    Blog; http://www.the-north.com/sharepoint --- Posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, January 13, 2009 2:18 PM
  • Ok If I correctly understand, u need delete same item from temp list after/during deleting item from main list.
    I think that in you case u need delete item from temp list on ItemDeleted Event because during deleting item from main list may be occur some error and etc. and in any case u will delete item from temp list...

    in the second place, when u'll create EventHadler u need deploy one via Feature and in the Feature.xml create paramter with temp list name
    <Properties>
        <Property Key="tmpListName" Value="TempList" />
    </Properties>
    when Feature activate save it parameter :
    ...
    list.ParentWeb.Properties.Add("ParamName", properties.Feature.Properties["PreviewLibName"].Value)
    ...

    and then use parameter in the Item EventHandler:

    SPDocLib.ParentWeb.Properties["ParamName"]

    Tuesday, January 13, 2009 2:32 PM
  • here is a nice article on ItemDeleting Event hanlder.
    http://www.wrox.com/WileyCDA/Section/Programming-Event-Handling-in-Windows-SharePoint-Services.id-306329.html

    here is some code which you may write to delete item from tempList while you are deleting item from Main list. Please note here i am assuming that IDs between two lists are same. since you are adding at the same time to the temp list so ID column will have the same value. so you need to locate item in temp list while you are inside event handler for the main list. this event handler must be register with Main list. how to register event handler can be found in above link. however here is the detail code for you.

    =============
    Using Microsoft.SharePoint;

    Namespace EventHandlerToDelete
    {
        class MainHandlerToDeleteFromTempList: SPItemEventReceiver
        {
            public override void ItemDeleting(SPItemEventProperties properties)
            {
                //Find the item from TempList
                SPWeb web=properties.web;
                SPList SPtemplist=web.Lists["tempList"];
                string sIDtoDelete=properties.listitem["ID"].ToString();

                SPListItemCollection listItems = SPtemplist.Items;
                int itemCount = listItems.Count;

                for (int iCounter=0; iCounter<itemCount; iCounter++)
                {
                        SPListItem item = listItems[iCounter];

                    if (sIDtoDelete==item["ID"].ToString())
                    {
                           listItems.Delete(iCounter);
                    }
                }

        }
    }
    • Edited by Moonis Tahir Tuesday, January 13, 2009 3:27 PM updated
    • Proposed as answer by Moonis Tahir Tuesday, January 13, 2009 7:47 PM
    Tuesday, January 13, 2009 3:27 PM
  • Hai Moonis,

    Thanx for your suggetion. I will try like this and update you.
    Wednesday, January 14, 2009 4:32 AM