none
How to Update multiple items in other list using event handler? RRS feed

  • Question

  • Hi All,

    If i update a item in a list, then i should update multiple items in another list need to be update. How to achive using event receivers?

    Sunday, September 21, 2014 6:59 AM

Answers

  • Hi Sam,

    According to your description, my understanding is that you want to update multiple items in another list when updated a list item.

    In the event receiver, you can update the multiple item using Client Object Model.

    Here is a code snippet for your reference:

     public override void ItemUpdated(SPItemEventProperties properties)
            {
                string siteUrl = "http://sp2013sps/sites/test/";
                ClientContext clientContext = new ClientContext(siteUrl);
                List oList = clientContext.Web.Lists.GetByTitle("another list name");
                ListItem oListItem = oList.GetItemById(1);
                oListItem["Title"] = "Hello World Updated!";
                oListItem.Update();
                clientContext.ExecuteQuery();
            }
    

    Best regards,<o:p></o:p>

    Zhengyu Guo


    Zhengyu Guo
    TechNet Community Support

    Monday, September 22, 2014 2:17 AM
  • Hi Sam,

    The event receiver will be associated with the Source list, and gets executed whenever the item is updated.

    And now in the ItemUpdated Event, you will be having your logic to update the items in the other list, from the Event Receiver properties you can get the Source Site, List, Item and the field being updated. 

    Now, you can get the destination List with the Site object and the destination list relative url, and then get the ListItem or ListItemCollection from the Destination List, and update the fields whichever you need to.

    Code snippet:

    SPSite site = SPItemEventProperties.Web

    SPList destList = site.GetList('relative url')

    SPListItemCollection itemColln = destList.Items

    for(<loop item collection>)

    {  

    'update the required field of the item and update'

    }

    Thanks

    Senthu

    -----------------------------------------------------------------------------------------------------------

    Please mark as 'Answer' if it resolves your query or vote as 'Helpful' if it helps

    Thursday, September 25, 2014 6:31 AM
  • If you are using SharePoint 2013, it's better to implement this logic using SP 2013 Remote Event Receiver.

    http://msdn.microsoft.com/en-us/library/office/dn456315(v=office.15).aspx#RER_Will2020EventHandlerWillWorkOn2013

    http://msdn.microsoft.com/en-us/library/office/jj220043(v=office.15).aspx


    Please mark the replies as answers if they help or unmark if not.

    Thursday, September 25, 2014 7:48 AM

All replies

  • Hi Sam,

    According to your description, my understanding is that you want to update multiple items in another list when updated a list item.

    In the event receiver, you can update the multiple item using Client Object Model.

    Here is a code snippet for your reference:

     public override void ItemUpdated(SPItemEventProperties properties)
            {
                string siteUrl = "http://sp2013sps/sites/test/";
                ClientContext clientContext = new ClientContext(siteUrl);
                List oList = clientContext.Web.Lists.GetByTitle("another list name");
                ListItem oListItem = oList.GetItemById(1);
                oListItem["Title"] = "Hello World Updated!";
                oListItem.Update();
                clientContext.ExecuteQuery();
            }
    

    Best regards,<o:p></o:p>

    Zhengyu Guo


    Zhengyu Guo
    TechNet Community Support

    Monday, September 22, 2014 2:17 AM
  • Hi Sam,

    The event receiver will be associated with the Source list, and gets executed whenever the item is updated.

    And now in the ItemUpdated Event, you will be having your logic to update the items in the other list, from the Event Receiver properties you can get the Source Site, List, Item and the field being updated. 

    Now, you can get the destination List with the Site object and the destination list relative url, and then get the ListItem or ListItemCollection from the Destination List, and update the fields whichever you need to.

    Code snippet:

    SPSite site = SPItemEventProperties.Web

    SPList destList = site.GetList('relative url')

    SPListItemCollection itemColln = destList.Items

    for(<loop item collection>)

    {  

    'update the required field of the item and update'

    }

    Thanks

    Senthu

    -----------------------------------------------------------------------------------------------------------

    Please mark as 'Answer' if it resolves your query or vote as 'Helpful' if it helps

    Thursday, September 25, 2014 6:31 AM
  • If you are using SharePoint 2013, it's better to implement this logic using SP 2013 Remote Event Receiver.

    http://msdn.microsoft.com/en-us/library/office/dn456315(v=office.15).aspx#RER_Will2020EventHandlerWillWorkOn2013

    http://msdn.microsoft.com/en-us/library/office/jj220043(v=office.15).aspx


    Please mark the replies as answers if they help or unmark if not.

    Thursday, September 25, 2014 7:48 AM