Caml查询获取最近30天的数据

时间:2016-03-09 09:00:02

标签: sharepoint-2013 caml

我想从共享点列表中获取最近30天的项目。

到目前为止我的努力,

                        SPQuery dataColQuery = new SPQuery();

                        dataColQuery.Query = "<Where>"+
                                                "<And>"+
                                                    "<Eq>"+
                                                        "<FieldRef Name='isAct' /><Value Type='Choice'>Yes</Value>"+
                                                    "</Eq>"+
                                                    "<Geq>"+
                                                        "<FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'><Today Offset='-30' /></Value>"+
                                                    "</Geq>"+
                                                "</And>"+
                                              "</Where>"+
                        "<OrderBy><FieldRef Name='Created' Ascending='True' /></OrderBy><GroupBy Collapse='True'><FieldRef Name='Created' /></GroupBy>";

                        dataColQuery.ViewFields = "<FieldRef Name='Created' /><FieldRef Name='tckStat' />";
                        dataColQuery.ViewFieldsOnly = true;
                        tktData = tktList.GetItems(dataColQuery).GetDataTable();

但是每次查询运行时我都会得到null值。 我确信数据存在于我希望获取数据的范围内。

请帮忙。 感谢

2 个答案:

答案 0 :(得分:0)

我选择了另一种方法后就开始工作了。请遵循以下代码。

DateTime lstNDaysDate = DateTime.UtcNow.AddDays(-30);
DateTime curntDt = DateTime.Now;

if (oWeb.CurrentUser.RegionalSettings != null)
                        {
                            nedt = Convert.ToDateTime(oWeb.CurrentUser.RegionalSettings.TimeZone.UTCToLocalTime(lstNDaysDate)).Date.ToString("yyyy-MM-dd");
                            curDt = Convert.ToDateTime(oWeb.CurrentUser.RegionalSettings.TimeZone.UTCToLocalTime(curntDt)).Date.ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            nedt = Convert.ToDateTime(oWeb.RegionalSettings.TimeZone.UTCToLocalTime(lstNDaysDate)).Date.ToString("yyyy-MM-dd");
                            curDt = Convert.ToDateTime(oWeb.RegionalSettings.TimeZone.UTCToLocalTime(curntDt)).Date.ToString("yyyy-MM-dd");
                        }

SPQuery dataColQuery = new SPQuery();
dataColQuery.Query = "<Where><And><Eq><FieldRef Name='isAct' /><Value Type='Choice'>Yes</Value></Eq><And><Geq><FieldRef Name='Created' /><Value Type='DateTime' >" + nedt + "</Value></Geq><Leq><FieldRef Name='Created' /><Value   Type='DateTime' >" + curDt + "</Value></Leq></And></And></Where>";

它就像一个魅力。

答案 1 :(得分:0)

将“ Offset”更改为“ OffsetDays”

例如: