How to get the latest item created by a specific user by CAML query

janejanejane picture janejanejane · Nov 9, 2010 · Viewed 16.1k times · Source

I am trying to get the most recent item created by a specific user by CAML query but it seems to return all the data created by everyone.

Help please.

Here's my code:

string lifestyleQuery = @"<Where><Eq><FieldRef Name='Author' /><Value Type='Text'>" + _id + @"</Value></Eq></Where>";

Answer

Rich Bennema picture Rich Bennema · Nov 9, 2010

Try this:

SPQuery query = new SPQuery();
query.Query = @"<Where><Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='Integer'>" + _id + @"</Value></Eq></Where><OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy>";
query.RowLimit = 1;