Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have two contact lists in my site i.e. Contacts 1 and Contacts 2.

I am creating "item added" event receiver on Contacts 1 that is whenever new entry is made to the Contacts 1 automatically it should be dropped to the Contacts 2.

My logic for copying the entry to the Contacts 2 list is:

public override void ItemAdded(SPItemEventProperties properties)
{
    base.ItemAdded(properties);
    if (properties.List.Title.Equals("Contacts 1"))
    {
        using (SPSite site = new SPSite(properties.WebUrl))
        {

           SPWeb web = site.RootWeb;


            SPList list = web.Lists["Contacts 2"];
            SPListItem item = list.Items.Add();

            foreach (SPField field in properties.List.Fields)
            {


                item[field.Title] = properties.ListItem[field.Title].ToString();

            }

            item.Update();
        }
    }
}

I am still not getting the item copied in the list 'Contacts 2'

What should I do to tackle this error.? Please help.

share|improve this question
1  
Your code will break if the old value is NULL, do a nullcheck on the ListItem[field.Title] before casting it to a string –  Robert Lindgren Jul 25 '13 at 12:48
    
What is the error you are getting? Perhaps add "web.AllowUnsafeUpdates = true;" and then setting it back at the end... Are the lists in the same spweb? If so then you can use properties.Web instead of getting the SPSite. –  BlueBird Jul 25 '13 at 12:54
    
@BlueBird AllowUnsafeUpdates only exists in a specific SPSite or SPWeb instance, so no need to set i back :) –  Robert Lindgren Jul 25 '13 at 12:57
    
I put the condition ` if(Convert.ToString(properties.ListItem[field.Title])!=null)` but still not getting the record in "Contacts 2" @RobertLindgren –  users1100 Jul 25 '13 at 12:58
    
do if(properties.ListItem[field.Title] != null) –  Robert Lindgren Jul 25 '13 at 12:59
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.