Skip to main content

Posts

Showing posts from June, 2014

CAML query on lookup columns

Let's imagine two SharePoint lists: -  Products -  Categories In this scenario, the  Products  list contains a lookup column called  Category  pointing on the  Categories  list. Now, I want to write a CAML query to get all the products list items related to the  Hardware  category. The first thing coming into my mind is the following piece of code: private SPListItemCollection GetProductsByCategory( SPFieldLookupValue Category) { SPList listProducts = this ._web.Lists[ "Products" ]; SPQuery camlQuery = new SPQuery (); camlQuery.Query = string .Format( "<Where><Eq><FieldRef Name=\"Category\" /><Value Type=\"Lookup\">{0}</Value></Eq></Where>" , Category.LookupValue); return listProducts.GetItems(camlQuery); } The problem with this query appears if some categories have the same name ( Hardware ). It's due to the fact that the query doesn't use the