I have a document library with 5 content types. Each content type has the same lookup on another list. I need a caml query that checks if a document with the same content type and lookup value exists in a given folder, excluding the item that has just been added.
Here is my logic (I use this logic in an ItemUpdated Event for the document library):
var ctId = listItem.ContentTypeId;
var customer = listItem[Fields.customersID] as SPFieldLookupValueCollection;
var project = new SPFieldLookupValue(listItem[Fields.miscellaneousProjectRecordID] as String);
var id = listItem.ID;
var projectsubfolder = list.RootFolder.SubFolders[project.LookupValue];
var query = new SPQuery
{
Folder = projectsubfolder,
Query = "<Query>" +
"<Where>" +
"<And>" +
"<And>" +
"<Neq>" +
"<FieldRef Name=\"ID\" />" +
"<Value Type=\"Counter\">" + id + "</Value>" +
"</Neq>" +
"<Eq>" +
"<FieldRef Name=\"Customers\" LookupId=\"TRUE\" />" +
"<Value Type=\"LookupMulti\">" + customer[0].LookupId + "</Value>" +
"</Eq>" +
"</And>" +
"<Eq>" +
"<FieldRef Name=\"ContentTypeId\" />" +
"<Value Type=\"ContentTypeId\">" + listItem.ContentTypeId + "</Value>" +
"</Eq>" +
"</And>" +
"</Where>" +
"</Query>"
};
var items = list.GetItems(query);
return items;
However, this also returns the item which has been added. In other words, the <Neq>
part doesn't seem to work. Am I doing something wrong here?
EDIT: Strange, when I use a CAML tool (SP CAML Query Helper Online), the same query works. I even copy pasted query.Query
while debugging and executed them one after the other, one in the debugger, the other in the CAML tool to make sure everything was the same. The CAML tool was returning the items I expected, but my code did not.