3

I have created a sharepoint list named "test" in that I have a column URL as multiline text.While inserting data into that column need to update the same value in another list. I tried by adding event receiver

public override void ItemAdded(SPItemEventProperties properties)  
{  
    base.ItemAdded(properties);  
    using (SPWeb web = properties.OpenWeb())  
    {  
        try  
        {  
            SPListItem currentItem = properties.ListItem;  
           /* some code to get values */
            currentItem.Update();  
        }  
        catch (Exception ex)  
        {  
            throw ex;  
        }  
    }  

It's not working, Please suggest some idea achieve this.

  • Do you tried to change the Elements.xml and replace<!--<Receivers ListTemplateId="101">--> to your List URL <Receivers ListUrl="Lists/test"> – Mohamed El-Qassas MVP Sep 28 '17 at 9:26
1

First, in case you need to run your event receive on a specific List, so you should modify Elements.xml to use ListUrl instead of ListTemplateId as the following

<Receivers ListTemplateId="101">

to

<Receivers ListUrl="Lists/test">

Where the Lists/test is the List URL


Second, try to use the below code to

public override void ItemAdded(SPItemEventProperties properties)  
{ 
   base.ItemAdded(properties);  
    using (SPWeb web = properties.OpenWeb())  
    {  
        try  
        {  
            // get the current List item            
            SPListItem currentItem = properties.ListItem; 
            // Second list
            web.AllowUnsafeUpdates = true;
            SPList List2 = web.Lists.TryGetList("destlist");
            SPListItem newItem = List2.Items.GetItemById(ID);
            newItem["FieldName"] = currentItem["FieldName"];
            newItem.Update();  
        }  
        catch (Exception ex)  
        {  
            throw ex;  
        }  
    } 
}

Note: in case you don't know the corresponsind item ID, you can get the List item via CAML at SPQuery, check the details at another thread at How to check if list item already exists? C# Sharepoint 2013

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.