简体   繁体   English

如何通过CAML查询获取特定用户创建的最新项目

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

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. 我试图通过CAML查询获取特定用户创建的最新项目,但它似乎返回所有人创建的所有数据。

Help please. 请帮助。

Here's my code: 这是我的代码:

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

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;

Use this to get the current date in a CAML acceptable format: 使用此选项以CAML可接受的格式获取当前日期:

DateTime dt = DateTime.Now;
string currentDate = String.Format("{0:yyyy-MM-ddThh-mm-ssZ}", dt);

This query will give you the items created by a user from newest to oldest. 此查询将为您提供用户从最新到最旧创建的项目。 I'm not sure how you would return only one node. 我不确定你将如何只返回一个节点。

string lifestyleQuery = @"<Query><OrderBy><FieldRef Name='Date'></OrderBy><Where><And><Eq><FieldRef Name='Author' /><Value Type='Text'>" + _id + @"</Value></Eq><Lt><FieldRef Name='Date' /><Value Type='DateTime'>" + currentDate + @"</Value></Lt></Where></Query>";

You might have to mess around with the query a bit to get correct column names (etc.), but I think this might be what you're asking? 你可能不得不乱用查询来获得正确的列名(等),但我想这可能是你要问的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM