SPQuery with lookup columns returns no data

An SPQuery for lookup columns should be easy. Start the famous U2U Caml Editor, configure your query, and query the list.

image

This query will get a result if the lookup column stores something like “1;#1”. But if its value is e.g. “1;#Title”, the query will not return an item.

So what can we do about it?

Add a LookupId=’TRUE’ to your query, so it will look like this:

<Where>
	<Eq>
		<FieldRef Name='LookupField' LookupId='TRUE'/>
		<Value Type='Lookup'>2</Value>
	</Eq>
</Where>

Remember to omit the Query tags from the U2U Caml Editor when pasting the query into your Visual Studio source code!