Rizwan
Rizwan

Reputation: 63

Sharepoint 2010 Event Receiver Item Adding and Updating in another List

i have two list, i wanted to update the second list while adding a new item in first one. i am using the following code but nothing happens.

     public override void ItemAdding(SPItemEventProperties properties)
      {
        base.ItemAdding(properties);
        string MenuList = properties.ListTitle;

        if (MenuList == "Menu")
        {
            string mySiteUrl = "http://rizwan-pc";
            using (SPSite mysiteCollection = new SPSite(mySiteUrl))
            {
                using (SPWeb mySite = mysiteCollection.RootWeb)
                {
                    string title = properties.ListItem["Title"].ToString();

                    SPList Menu_lookUp = mySite.Lists["Menu LookUp"];
                    SPListItem newListItem = Menu_lookUp.Items.Add();
                    newListItem["Applicable"] = title;
                    newListItem.Update();
                }
            }
        }
    }

i wanted to copy the new item in the second list which i currently added in the first list. any help would be great, Thanks in advance.

Upvotes: 0

Views: 18975

Answers (1)

Jignesh Rajput
Jignesh Rajput

Reputation: 3558

if you want to title of the listitem than you can try this :

its good habit to use properties in your code for get site or web :

 using(SPWeb objweb = properties.openweb())
 {
  string title = convert.ToString(properties.AfterProperties["Title"]);
  objweb.AllowUnsafeUpdates = true;
  SPList Menu_lookUp = mySite.Lists["Menu LookUp"];
  SPListItem newListItem = Menu_lookUp.Items.Add();
  newListItem["Applicable"] = title;
  newListItem.Update();
  objweb.AllowUnsafeUpdates = false;
 }

one things is that you should use itemadded instead of itemadding. its easy to get listitem by current id.

  SPList objlist = objweb.Lists[properties.ListTitle];
  SPListItem liCaseID =   objlist.Items.GetItemById(properties.ListItemId);
  //your excute your code.

Upvotes: 6

Related Questions