Thursday, September 2, 2010

Matching lookup column values using CAML query

To fetch items from a list based on a lookup column value, I tried the following query first.


< Query >
  < Where >
    < Eq >
        < FieldRef Name="Roadmap_x0020_Address" / >
        < Value Type="Lookup" > xyz < /Value >
   < /Eq >
 < /Where >
< /Query >


But, have not got the desired result because this query matches on the title of a lookup column, there there are cases where we can have a duplicate title. As we know that the lookup value structure is 12;#xyz, Instead of fetching by name we should look by ID


To fetch information by ID, we need use this switch LookupId=”TRUE”





< Query >

  < Where >
    < Eq >
        < FieldRef Name="Roadmap_x0020_Address" LookupId=”TRUE” / >
        < Value Type="Lookup" >12 < /Value >
   < /Eq >
 < /Where >
< /Query >

Another thing to remember is, the "FieldRef Name" we are using should be the internal name of a SharePoint column.