PointBridge Weblogs > Blogs > Matt’s Blog > Posts > Querying WSS Lookup Columns with CAML
I spent way too long yesterday trying to get a CAML query that would allow me to query on a lookup field and figured I’d share.
<rant>
First, one thing I find very annoying about SharePoint is that if you have an invalid query, the call to the GetItems() method returns all rows. What?? How ’bout a friendly exception that I have an invalid query??
</rant>
So here was the solution: the CAML query should be formatted with a type attribute of Lookup in the Value tag and the text of that element should be the same value that you’d see in the SharePoint UI.
For example:
<Where>
<Eq>
<FieldRef Name="GiftsForMom" />
<Value Type="Lookup">Flowers</Value>
</Eq>
</Where>
Looking at it now, this is reasonably straightforward. However, it didn’t click in my development-damaged brain, as the value you see in a lookup field when you inspect it in the debugger is something like 4;#Gift Types.
http://thorprojects.com/blog/archive/2007/02/23/643.aspx
When constructing CAML queries you must use internal names by this wonderful exception:
Exception caught Microsoft.SharePoint.SPException: One or more field types are not installed properly. Go to the list settings page to delete these fields. —>
System.Runtime.InteropServices.COMException (0x81020014): One or more field types are not installed properly. Go to the list settings page to delete these fields.