2

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.

1 Answer 1

0

Ok so it turns out that in server object model, the root element of a query is not <Query> but <Where>. In other words, when I remove my <Query> tags, the exact same query works. I just found this info on some guy's blog (forgot the url), no trace of it on MSDN or other documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.