2

I have a SharePoint 2007 (MOSS) list with 2 "Person or Group" columns which I would like to compare, essentially:

SELECT * FROM List WHERE (Analyst = Developer)

In my code (C#) I've built this as:

SPQuery itemQuery = new SPQuery();
itemQuery.Query = "<Where><Eq><FieldRef Name='Analyst' /><FieldRef Name='Developer' /></Eq></Where>";

SPListItemCollection queryResults = list.GetItems(itemQuery);

But this is throwing an exception ("Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION)").

I've seen some information regarding the Type attribute of a Value element when comparing against a "Person or Group" column, but nothing about comparing two of these columns against each other.

I've also tried adding "LookupId='TRUE'" to each FieldRef, with no change.

3 Answers 3

3

It is not possible to compare two fields like that using CAML. You have to use a literal value. This means, that you'll likely have two queries:

  1. Retrieve the Analyst/Developer's user ID
  2. Retrieve items where Analyst and Developer are the same

Example of #2:

<Where>
   <And>
      <Eq><FieldRef Name="Analyst" LookupId="TRUE"/><Value Type="Integer">42</Value></Eq>
      <Eq><FieldRef Name="Developer" LookupId="TRUE"/><Value Type="Integer">42</Value></Eq>
   </And>
</Where>

FYI, you can also use <UserID/> for the "Current User" instead of user ID (in this example, 42).

1
  • Thanks for the reply. Unfortunately the right answer to my question seems to be "it is not possible". As I've mentioned in another comment, I've gotten around the problem in my case by at run-time replacing the Value (42 in your example) with the result of another CAML query (using C# and lots of string functions)
    – DaveD
    Jul 29, 2011 at 13:58
1

The logic you are looking for, I believe, is this:

<Where>
   <Eq>
      <FieldRef Name='Analyst'/>
      <Value Type="Text"><FieldRef Name='Developer'/></Value>
   </Eq>
</Where>

I tested and this is not possible so the two options, as I see it, are:

  1. Get all list items , then iterate through with JQuery with a find and compare for the two fields being equal.

  2. Create a calculated column that sets a true or false value if the two columns are equal in the list and then do your select based on that column. This is probably the most expedient and also most effective from a performance perspective.

1
  • Thanks for the suggestion and the follow-up! My specific requirements involve being able to specify which fields are being compared at run-time, so I've ended up with an approach which takes an invalid CAML statement, parses it to replace some markup (the invalid part) with the result of a query, and then runs the resulting query. It's ugly :( But at least it works
    – DaveD
    Jul 29, 2011 at 13:55
0

Try this:

<Where> 
  <Eq> 
    <FieldRef Name="Analyst" /> 
    <Value Type="Text">Developer</Value> 
  </Eq> 
</Where>

I have found a list that has two matching values to compare and I got a CAML query to work with the comparison; however, it is a boolean value so I am not sure if this is what you are looking for. It does compare two fields but I think the literal is still getting in the way. This list has about 25 entries in it and this is the only one that matched so I thought this would be a good test.

Here is the code:

private DataTable ExecuteQuery(SPList list) 
{ 
   SPQuery qry = new SPQuery(); 
 qry.Query = "<Where><And><Contains><FieldRef Name='Show' /><Value       Type='Boolean'>1</Value></Contains><Contains><FieldRef Name='Highlight' /><Value     Type='Boolean'>1</Value></Contains></And></Where>"; 
qry.ViewFields = "<FieldRef Name='Show' /><FieldRef Name='Highlight' />"; 
qry.IncludeMandatoryColumns = true; 
return list.GetItems(qry).GetDataTable();

Sorry if this is not what you are looking for. Good Luck!!

2
  • I don't want to compare a field to a literal value, but the values of two fields. One field name "Analyst" and one named "Developer".
    – DaveD
    Jul 28, 2011 at 19:35
  • Thanks for the updated suggestion, but isn't your second CAML equivlant to "SELECT * FROM list WHERE ((Show CONTAINS 1) AND (Highlight CONTAINS 1))" ? (taking liberties with "CONTAINS"). If so, that doesn't really help, since I don't care what the values of Show and Highlight are, only that they are the same.
    – DaveD
    Jul 29, 2011 at 12:28

Your Answer

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

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