C# – How to update one list from another using ItemUpdated event receiver in SharePoint

csharepointsharepoint-2010visual studio 2010

I am quite new to SharePoint and have been working on this thing for a couple of days. I was able to somehow use ItemAdded event receiver but I want if I update List 1 then using ItemUpdated event receiver List 2 should also get updated.
I am struggling with the coding part. How to link items in one list with another and then how to update.

Best Answer

Here is an example "ItemAdded" event -- your itemupdating event can work similar to this. In this example, any time a new item is added to my "Tasks" list, the event receiver adds a similar item to my "Skills and Abilities" list:

        // ITEM ADDED
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);

            try
            {
                String curListName = properties.ListTitle;
                if (_applicableLists.Contains(curListName))
                {

                    if (properties.ListItem.ContentType.Name == "Discussion")
                    {

                        if (curListName == "Tasks")
                        {
                            SPList saList = properties.Web.Lists["Skills & Abilities"];
                            SPListItem saItem = SPUtility.CreateNewDiscussion(saList, properties.ListItem["Task Text"].ToString());
                            SPFieldLookupValue taskLookup = new SPFieldLookupValue();
                            taskLookup = new SPFieldLookupValue(properties.ListItem.ID, (string)properties.AfterProperties["Task ID"]);
                            saItem["Task Lookup"] = taskLookup;
                            saItem["Title"] = properties.ListItem["Task Text"].ToString();
                            saItem["Body"] = "Please leave a comment by clicking the Reply link on the right of this message.";
                            saItem[_inStatus] = "New";
                            // perform the update with event firing disabled
                            EventFiringEnabled = false;
                            saItem.Update();
                            EventFiringEnabled = true;

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // handle exception...
            }
        }

I also urge you to check out the sharepoint.stackexchange.com site for a focus on SharePoint. Good luck!

Related Topic