Event Receiver - Item is being deleted event doubts and learning

Dear all,

I am learning Event Receiver for quite sometime and I am having some knowledge in c# and its methods etc.

I have a doubt in using  - item is being deleted event receiver

Scenario lists:-

List A :- RegistrationList

RegID        -   10-105

First Name - Ganesh

Last Name - K

E-Mail Address Ganesh.K@ifmr.co.in

Phone Number - 89097890

SessID Readonly field

List B:- SessionList

SessID SharePoint 2010 Advanced for Developers-10

Courses Title  - SharePoint 2010 Advanced for Developers

Trainer - x

Training Venue - LA

Register Info - 3

Total Seats Info - 5

Open Seats Info - 2

Start Date - 3/23/2015

End Date 3/27/2015

Scenario:-

List A :- RegistrationList

List B:- SessionList

If RegistrationList  - List Item is being deleted I want to deduct 1 from sessionlist in Register Info field. I want the string rID - Registrations ID - first two characters should match the last two characters of Sess ID. In this case it is 10

I am just thinking of this code will work - error in GetItemById:-

if (properties.ListTitle == "Registrations")

           {

               //SPWeb currentWeb = properties.Web;

               //SPList rList = currentWeb.Lists["Registrations"];

               //SPListItem rItem = rList.GetItemById

               string rID = properties.ListItem["RegID"].ToString();

              rID = rID.Remove(rID.Length - 3);

              SPWeb currentWeb = properties.Web;

              SPList sList = currentWeb.Lists["Session List"];

              SPListItem sItem = sList.GetItemById(rID);

             

           }

Let me know.

Warm Regards,

Sathya

March 19th, 2015 11:36am

Firstly SPList.GetItemById expects an integer as paramater not sting. In above code rID is string. Second, you want rID to be compared with last two characters of SessID. So, you need to actualy read the items of the Seesion List and then read the value contained in SessID field and then compare rID with the field value. Something like this:

string rID = rID.Remove(rID.Length - 3);
SPWeb currentWeb = properties.Web;
SPList sList = currentWeb.Lists["Session List"];
SPListItemCollection items = sList.Items;
foreach(SPListItem item in items)
{
string sessID = Convert.ToString(item["SessID"]);
//Now compare rID value and get the ID of the item. Finally perform the deuction

}

Free Windows Admin Tool Kit Click here and download it now
March 19th, 2015 12:07pm

Dear Nadeem,

I just saw the original code to complete this.

if(properties.ListTitle == "RegistrationList")

           {

              

//SPWeb currentWeb = properties.Web;


              

//SPList rList = currentWeb.Lists["Registrations"];


              

//SPListItem rItem = rList.GetItemById


             

stringrID = properties.ListItem["RegID"].ToString();//80-43


             

inthyphenIndex = rID.IndexOf("-");

             

stringsessId = rID.Substring(0,hyphenIndex); //80


             

SPWebcurrentWeb = properties.Web;

             

SPListsessionList = currentWeb.Lists["SessionList"];

             

SPListItemcurrentSession = sessionList.GetItemById(Convert.ToInt32(sessId));

              currentSession[

"Register Info"] =Convert.ToInt32(currentSession["Register Info"].ToString()) -1;

               currentSession.Update();

             

//rID = rID.Remove(rID.Length - 4);


             

//SPWeb currentWeb = properties.Web;


             

//SPList sList = currentWeb.Lists["Session List"];


             

//SPListItemCollection items = sList.Items;


             

//foreach (SPListItem item in items)


             

//{


             

//    string sessID = Convert.ToString(item["SessID"]);


             

//    sessID = sessID.


             

//    if (rID == sessID)


             

//    {


                     

                  }

          

base.ItemDeleting(properties);

             

             

             

          

          

          

       }

I thought to share this for others benefit.

Cheers

Sathya

  • Marked as answer by sathyaav 18 hours 47 minutes ago
March 21st, 2015 8:45am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics