Adis Jugo

The Southern Side – SharePoint thoughts and bytes

Using CAML to query Sharepoint lists over Lookup fields

If you have ever tried to query a SharePoint List, over a LookupList value with CAML, you know that the "supposed" CAML way to do it is over the "Title" field of the LookupList values.

For example, if we want to query the sharepoint list, which has a Lookup field "ProjectID", and we want to extract all list items with the requested ProjectID, the CAML query would look like:

<Query>
<Where>
<Eq>
<FieldRef Name="ProjectID" />
<Value Type="Lookup">My Test Project 1</Value>
</Eq>
</Where>
</Query>

Of course, it is not very practical, and basicaly it is useless when developing SharePoint based applications: what if Project’s name changes? Or maybe we do not store the Project’s name in the "Title" field, but in some other text field?

The only solution would be by writing a CAML which would query the same list, but over ID of the Lookup list. Luckilly, there is a way to do that:

<Query>
<Where>
<Eq>
<FieldRef Name="ProjectID" LookupId="TRUE" />
<Value Type="Text">3</Value>
</Eq>
</Where>
</Query>

Now, this query will return the same results like the previous query, only that we query LookupList data over it’s ID, which is much more convinient. And, ListItem is really easy to retrieve from the SharePoint Object Model.

Wed, December 12 2007 » Development

Share 'Using CAML to query Sharepoint lists over Lookup fields' on Facebook Share 'Using CAML to query Sharepoint lists over Lookup fields' on LinkedIn Share 'Using CAML to query Sharepoint lists over Lookup fields' on Twitter Share 'Using CAML to query Sharepoint lists over Lookup fields' on XING

One Response

  1. Siva Sakthivel,INDIA,Software Engineer in CAP DIGISOFT SOLNS November 29 2008 @ 15:45

    I m having a issue..Me have 3 Custom Lists(1-Books Custom List 2-Members Custom Lists 3-Transaction Custom List)In the Trans Custom List, I ve Lookuped the Book ID from Books Custom List,When i try to select the BookID(LOOKUP VALUE)I need to get the selected Row Items From the Book Custom List and need to display in d Transaction Issue list (Add Item),whether the book has been issued or still in the library…like that..

    Whether its possible….Need a Solution Pls….

Leave a Reply