I'm trying to delete items from the myDestinationList if they do not exist in the mySourceList. I'm trying to get this method done in C#. I have it set up this way because the two list are on different servers. Any suggestions?
Also would I be able to assign the Author/Editor from the source list item to the Author/Editor fields in the destination list?
Update: List has over 2k items, the client may update the list once a day or once a week, this may vary.
static void Main(string[] args) {
ClientContext context = new ClientContext(site2);
List mySourceList = context.Web.Lists.GetByTitle(ListName);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View></View>";
ListItemCollection mySourceItemColl = mySourceList.GetItems(camlQuery);
context.Load(mySourceItemColl);
context.ExecuteQuery();
SPSite myDestinationSite = new SPSite(site2);
SPWeb myDestinationWeb = myDestinationSite.OpenWeb();
SPList myDestinationList = myDestinationWeb.Lists[ListName2];
SPQuery myDestinationListQuery = new SPQuery();
myDestinationListQuery.Query = "" +
"" +
"" +
"" +
"";
SPListItemCollection myDestinationItemColl = myDestinationList.GetItems(myDestinationListQuery);
foreach(ListItem mySourceListItem in mySourceItemColl) {
foreach() {
//delete items
item.Delete();
}
foreach(SPListItem myDestinationListItem in myDestinationItemColl) {
//Update items here
}
}
}
Event Receiver
public override void ItemAdded(SPItemEventProperties properties) {
using(ClientContext context = new ClientContext(site1)) {
List list = context.Web.Lists.GetByTitle(sourceList);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View></View>";
ListItemCollection collListItem = list.GetItems(camlQuery);
context.Load(collListItem);
context.ExecuteQuery();
using(ClientContext target = new ClientContext(site1)) {
List list2 = target.Web.Lists.GetByTitle(destinationList);
foreach(ListItem oListItem in collListItem) {
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem AddListItem = list2.AddItem(itemCreateInfo);
AddListItem["Title"] = oListItem["Title"];
AddListItem.Update();
target.ExecuteQuery();
}
}
}