I have created an event receiver that runs when item is added. The code copies list item from list A
to list B
;
It is working wonderful but I have a problem, if list A
has 10 items, all of those 10 items are being copied to list B
. Now if I add an item to List A
(which means that now list A
has 11 items) in List B
are going to be copied again all 11 items (which means that now list B has 21 items)
How Can I check if in List B
already exists items that are in List A
Here is my code:
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
using (SPSite site = new SPSite("http://MyWeb/sites/mysite/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList sourceList = web.Lists.TryGetList("A");
foreach (SPListItem sourceItem in sourceList.Items)
{
SPList desList = web.Lists.TryGetList("B");
if (desList != null)
{
SPListItem desItem = desList.Items.Add();
foreach (SPField field in sourceItem.Fields)
{
if (!field.ReadOnlyField && !field.Hidden && field.InternalName != "Attachments")
{
if (desItem.Fields.ContainsField(field.InternalName))
{
desItem[field.InternalName] = sourceItem[field.InternalName];
}
}
}
desItem.Update();
}
}
}
}
}
List A
has unique ID inList B
there is a filed calledlistA:ID
now I have to checkif (listA:ID != with the Unique ID in list A)