0

I have written below line which create item in destination list but would like to check if item exist update the column else create new item my Title field is primary key for source & destination

foreach (ListItem SourcelistItem in lic)
                {
                    // We have all the list item data. For example, Title.
                    var employee = contextDestination.Web.EnsureUser(((FieldUserValue)SourcelistItem["Account"]).LookupValue);
                    var supervisor = contextDestination.Web.EnsureUser(((FieldUserValue)SourcelistItem["MGR"]).LookupValue);
                    contextDestination.Load(supervisor, user => user.Id);
                    
                    contextDestination.Load(employee, user => user.Id);
                    contextDestination.ExecuteQuery();
                    ListItem DestnewItem = destList.AddItem(itemCreateInfo);
                    DestnewItem["Title"] = SourcelistItem["Title"];
                    DestnewItem["Account"] = new FieldUserValue { LookupId = employee.Id };
                    
                    DestnewItem["MGR"] = new FieldUserValue { LookupId = supervisor.Id };
                    

                    DestnewItem.Update();

                }
0

1 Answer 1

0

You can check whether item is exist in destination list using CAML Query before adding item.

You can use below code.

    foreach (ListItem SourcelistItem in lic)
        {
            // We have all the list item data. For example, Title.
            var employee = contextDestination.Web.EnsureUser(((FieldUserValue)SourcelistItem["Account"]).LookupValue);
            var supervisor = contextDestination.Web.EnsureUser(((FieldUserValue)SourcelistItem["MGR"]).LookupValue);
            contextDestination.Load(supervisor, user => user.Id);

            contextDestination.Load(employee, user => user.Id);
            contextDestination.ExecuteQuery();

            ListItem DestnewItem = null;
            ListItemCollection existingItems = destList.GetItems(new CamlQuery() { ViewXml = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + Convert.ToString(SourcelistItem["Title"]) + "</Value></Eq></Where></Query></View>" });
            contextDestination.Load(existingItems);
            contextDestination.ExecuteQuery();

            if(existingItems != null && existingItems.Count > 0)
            {
                DestnewItem = existingItems[0];
            }
            else
            {
                DestnewItem = destList.AddItem(itemCreateInfo);
            }                                
            DestnewItem["Account"] = new FieldUserValue { LookupId = employee.Id };

            DestnewItem["MGR"] = new FieldUserValue { LookupId = supervisor.Id };


            DestnewItem.Update();

        }

Hope it will helpful to you and please mark this as answer if it works for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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