Share via


CAML query with Person or People and Groups column in Sharepoint 2007 (using in Event Handlers)

I was facing an issue with my simple CAML query where I was trying to filter the Field inside Sharepoint List and supplying value from the properties.AfterProperties of List Item event handler. This field was of Person or to say People and Groups kind of field. Internally inside sharepoint this value is stored as USERID and not as the display name. So I was using a simple CAML builder to build my query and it looked like:

oQuery.Query = "<Where><And><Eq><FieldRef Name='Team_x0020_Member' />" +

                        "<Value Type='User'>" + properties.Afterproperties[“Team_x0020_Member”].ToString() + "</Value>" + "</Eq>" +

                        "<Eq><FieldRef Name='Year' /> <Value Type='Choice'>" + properties.AfterProperties["Year"].ToString() +

                        "</Value> </Eq></And></Where>";

And this never worked for me, then one of my colleagues Madhur Ahuja helped me crack this it was a simple Lookup attribute that was missing, so basically the query should have been:

oQuery.Query = "<Where><And><Eq><FieldRef Name='Team_x0020_Member' LookupId= 'TRUE' />" +

                        "<Value Type='User'>" + properties.Afterproperties[“Team_x0020_Member”].ToString() + "</Value>" + "</Eq>" +

                        "<Eq><FieldRef Name='Year' /> <Value Type='Choice'>" + properties.AfterProperties["Year"].ToString() +

                        "</Value> </Eq></And></Where>";  

And this never worked for me, so bottom line if you use People and Groups kind of column in Filters always make sure that you use LookupId ='TRUE' attribute in Filters.

Comments

  • Anonymous
    May 26, 2008
    Can u please help me with a CAML query to retrieve all sharepoint groups in a site collection