Querying WSS Lookup Columns with CAML

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s