Skip to main content

Cloud

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Matthew Morse

More from this Author

Follow Us