Question

According to my requirement, I've 2 Lists within the same site List A and List B. Below are the columns in List A: User [PeoplePicker], City [dropdown (2 choices)], Company [dropdown (5 choices)], Salary [dropdown]

Below are the columns in List B : User [PeoplePicker], City [dropdown (2 choices)], Company [dropdown (5 choices)]

there are already many items in List A and for an User no two items in List A are similar (criteria we don't need to worry about) so an User can have a total of 10 combinations. Now I want to trigger an Event Receiver using ItemAdded() in List B so that based on the values of those 3 columns (User, City, and Company) I can update the value of Salary in List A. Can anyone please help me to achieve this requirement?

Example: List A: User: Rolly Anand City: Delhi Company: Microsoft Salary: 2 Lakh

adding Item to List B: User: Rolly Anand City: Delhi Company: Microsoft

after adding the item to List B that Salary should change in List A: User: Rolly Anand City: Delhi Company: Microsoft Salary: 3 Lakh

P.S: I think it is better to do it by Caml Query but don't know the exact way how to use it and Update that particular List Item.

Was it helpful?

Solution

Sample test demo for you reference:

public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            SPListItem item = properties.ListItem;
            using (SPWeb web = properties.OpenWeb())
            {
                SPQuery query = new SPQuery();
   string username = new SPFieldUserValue(web, item["User"].ToString()).User.Name;                
                query.Query = @"<Where>
      <And>
         <And>
            <Eq>
               <FieldRef Name='User' />
               <Value Type='User'>"+username+"</Value></Eq><Eq><FieldRef Name='City' /><Value Type='Choice'>" + item["City"] + "</Value></Eq></And> <Eq><FieldRef Name='Company' /><Value Type='Choice'>" + item["Company"] + "</Value> </Eq> </And></Where>";
                SPList list = web.Lists.TryGetList("listA");
                SPListItemCollection items = list.GetItems(query);
                if(items != null)
                {
                    foreach (SPListItem listitem in items)
                    {
                        listitem["Salary"] = "3 Lakh";
                        listitem.Update();
                    }
                }




            }

        }
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top