0

i am trying to add items from one list to another list and able to add the items but how should i modify my code so if item exist in destination list update the item else create new entry ?

I have modified my code and trying below query but not getting expected results is this right way ?

below is my code for add item ?

Done more updates in code which i am trying ..

static void Main(string[] args)
        {
            string userName = "";
            Console.WriteLine("Enter Your Password Please  ------- ");
            SecureString password = GetPasswordOfYourSite();
            ClientContext contextSource = new ClientContext("SourceSite");
            contextSource.Credentials = new SharePointOnlineCredentials(userName, password);

            List sourceList = contextSource.Web.Lists.GetByTitle("ListA");
            
            CamlQuery query = new CamlQuery();
            CamlQuery cq = CamlQuery.CreateAllItemsQuery(200);
            ListItemCollection source = sourceList.GetItems(cq);
            contextSource.Load(source);
            contextSource.ExecuteQuery();
            using (var clientContext = new
            ClientContext("DestinationSite"))
            {

                List lst = clientContext.Web.Lists.GetByTitle("ListB");
                ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                CamlQuery query2 = new CamlQuery();
                foreach (ListItem SourcelistItem in source)
                 {
                    query2.ViewXml= "<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + SourcelistItem["Title"] + "</Value></Eq></Where></Query></View>";
                    ListItemCollection lic = lst.GetItems(query2);
                    clientContext.Load(lic);
                    clientContext.ExecuteQuery();

                      try
                        {
                            if (lic.Count <= 0)
                            {

                                var employee = contextSource.Web.EnsureUser(((FieldUserValue)SourcelistItem["Account"]).LookupValue);
                                var supervisor = contextSource.Web.EnsureUser(((FieldUserValue)SourcelistItem["MGR"]).LookupValue);

                            contextSource.Load(supervisor, user => user.Id);

                            contextSource.Load(employee, user => user.Id);
                            contextSource.ExecuteQuery();
                                ListItem DestnewItem = lst.AddItem(itemCreateInfo);
                                DestnewItem["Title"] = SourcelistItem["Title"];
                                DestnewItem["Account"] = new FieldUserValue { LookupId = employee.Id };

                                DestnewItem["MGR"] = new FieldUserValue { LookupId = supervisor.Id };
                                DestnewItem["Country"] = SourcelistItem["Country"];
                               
                                DestnewItem.Update();
                            }
                            else
                            {
                               // UpdateList();

                            }
                        }

                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());

                        }

                    }
                contextSource.ExecuteQuery();

                }
            

        }
        
6
  • What field or combination of fields uniquely identifies an item? Once you establish that you can look for an item that matches that criteria via a CAML query and update if it exists, create new if it doesn't.
    – Rob D'Oria
    Aug 1 at 17:06
  • So Title is the field from source which should check in destination list Title is like key
    – Ashok
    Aug 1 at 17:29
  • The Title column isn't necessarily unique unless it's set to be unique. I believe "Enforce unique values" is set to "No" by default. If your source and destination have "Enforce unique values" to "Yes" then you can use that field to check for an existing item in the destination.
    – Rob D'Oria
    Aug 1 at 20:32
  • Enfore unique value is set to Yes, but can you let me know how should I write caml query and check item in destination before adding in my code?
    – Ashok
    Aug 2 at 2:12
  • I'm having a hard time trying to follow what you're doing in the code you provided. Are you trying to get all the items from List A and then add them to List B if they aren't already there?
    – Rob D'Oria
    Aug 2 at 15:57

0

Your Answer

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

Browse other questions tagged or ask your own question.