I have two identical SharePoint lists. I’m using C# to loop through one and adding to the other using the SP object model.
Problem is I don’t want to add to the other, if there is already a list entry with 3 specific fields that match the from list.
How can do this is in C#? using CAML?
Say my lists are called from and to and the fields are called a,b and c.
Here, I don’t want to add a entry to “to”, if there is already an entry in to with a,b and c matching my currently from entry.
SPList fList = web.GetList("/sites/xxxx/Lists/from");
SPList tList = web.GetList("/sites/xxxx/Lab/Lists/to");
foreach (SPListItem fListItem in fList.Items)
{
// my caml code here i suspect?
//SPquery.query = "CAML";
//SPList<yourlist>.GetItems(SPQuery object)
SPListItem tListItem = tList.Items.Add();
foreach (SPField field in fList.Fields)
{
try
I would start by using SPMetal (LINQ To SharePoint if possible).
The other way is like you mentioned and use SPQuery.
I am not at my normal PC so I can’t test either of these codes but should give you an idea of how to start, it is unfortunate that SharePoint doesn’t allow composite unique keys.
It would be possible to enforce the composite unique key with an event receiver on that list if you want to approach the problem from a different angle.