Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11508

Using event receiver to update item in other List when the itemUpdating even or itemUpdated even occurred?

$
0
0
Help me!!! I'm a newbie Sharepoint. and I have an Event Receiver the itemAdded on a custom List (source) creates a Group Calendar entry in another List (target). Now, i want to add an itemUpdated event or itemUpdating event so that when the the source custom List is updated the change is filtered through to the calendar target List.
I am using c# in Visual Studio to develop the Event Receiver. I was code like that but it not work:

public override void ItemUpdating(SPItemEventProperties properties)
       {
           properties.Web.AllowUnsafeUpdates = true;
           SPList myCalendarList = properties.Web.GetList("Lists/My Calendar") as SPList; 
           SPListItem sourceItem = properties.ListItem;

           string query = string.Format("<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>{0}</Value></Eq></Where>", sourceItem["Title"].ToString());
           SPQuery spQuery = new SPQuery();
           spQuery.Query = query;
           SPListItemCollection foundItems = myCalendarList.GetItems(spQuery);

           for (int i = 0; i < foundItems.Count;i++ )
           {
               try
               {
                   foundItems[i]["Title"] = properties.AfterProperties["Title"];
                   foundItems[i]["Start Time"] = properties.AfterProperties["Start Date"];
                   foundItems[i]["End Time"] = properties.AfterProperties["Due Date"];
                   foundItems[i]["Description"] = properties.AfterProperties["Description"];
                   foundItems[i]["Attendees"] = properties.AfterProperties["Assigned to"];
                   foundItems[i].SystemUpdate();
               }
               catch (Exception ex)
               {
               }
           }
           myCalendarList.Update();
           properties.Web.AllowUnsafeUpdates = false;
    }

Can anyone please advise the best way to do this and how I create the link between the two Lists to ensure I can update from source to target?
Thank you.

Viewing all articles
Browse latest Browse all 11508

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>