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();
}
}