0

我正在尝试访问 SharePoint 列表并返回我制作的自定义 Web 部件的日历日期。它工作正常,然后我决定只检索选定的日期而不是整个日历,所以我想添加一个 where 子句。

我已尝试将 'yyyy-MM-dd'、'yyyy-MM-ddThh:mm:ssZ' 和 'yyyy-MM-dd hh:mm:ssZ' 作为字符串格式 我也尝试过 MM/dd/yyyy作为日期格式。

我正在使用 jQuery,并且日历中确实有列表项。我假设我的日期格式不正确。

        var date = $(this).attr('date');            

        var sharepointDate = Date.parse(date).toString('yyyy-mm-ddT00:00:01Z');
        var soapEnv =
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
            <soapenv:Body> \
            <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
            <listName>CorporateCalendar</listName> \
            <viewFields> \
            <ViewFields> \
            <FieldRef Name='Title' /> \
            </ViewFields> \
            </viewFields> \
            <query><Query><Where><Geq><FieldRef Name='EventDate' /><Value Type='DateTime'>" + sharepointDate + "</Value></Geq></Where></Query></query> \
            <rowLimit>500</rowLimit> \
            </GetListItems> \
            </soapenv:Body> \
            </soapenv:Envelope>";

如果我去掉 where 子句,我会收到日历中的所有项目。如果查询在那里,我不会收到任何结果。

提前致谢

工作代码:

    var sharepointDate = Date.parse(date).toString('yyyy-MM-dd');
        var soapEnv =
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
            <soapenv:Body> \
            <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
            <listName>CorporateCalendar</listName> \
            <viewFields> \
            <ViewFields> \
            <FieldRef Name='Title' /> \
            </ViewFields> \
            </viewFields> \
            <query><Query><Where><Eq><FieldRef Name='EventDate' /><Value Type='DateTime' IncludeTimeValue='False'>" + sharepointDate + "</Value></Eq></Where></Query></query>\
            <rowLimit>1500</rowLimit> \
            </GetListItems> \
            </soapenv:Body> \
            </soapenv:Envelope>";
4

2 回答 2

0
    var sharepointDate = Date.parse(date).toString('yyyy-MM-dd');
        var soapEnv =
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
            <soapenv:Body> \
            <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
            <listName>CorporateCalendar</listName> \
            <viewFields> \
            <ViewFields> \
            <FieldRef Name='Title' /> \
            </ViewFields> \
            </viewFields> \
            <query><Query><Where><Eq><FieldRef Name='EventDate' /><Value Type='DateTime' IncludeTimeValue='False'>" + sharepointDate + "</Value></Eq></Where></Query></query>\
            <rowLimit>1500</rowLimit> \
            </GetListItems> \
            </soapenv:Body> \
            </soapenv:Envelope>";
于 2012-06-11T15:11:33.383 回答
0

问题不在于您的 CAML 查询,而在于解析日期。不幸的是,JavaScript 中的 Date.toString 方法不像 C# 那样支持格式化。toString 方法不接受任何参数,因此您必须自己将其解析为有效的 ISO 格式。

我从问题How do I output an ISO-8601 formatted string in Javascript? 中获取了 ISODateString 方法?. 我们可以使用此方法获取 sharepointDate 的有效值,代码应如下所示:

function ISODateString(d){
 function pad(n){return n<10 ? '0'+n : n}
 return d.getUTCFullYear()+'-'
      + pad(d.getUTCMonth()+1)+'-'
      + pad(d.getUTCDate())+'T'
      + pad(d.getUTCHours())+':'
      + pad(d.getUTCMinutes())+':'
      + pad(d.getUTCSeconds())+'Z'}
var sharepointDate = ISODateString(new Date(Date.parse(date)));

现在 sharepointDate 将采用 'yyyy-mm-ddT00:00:01Z' 格式。由于这是 CAML 查询中预期的格式,您现在可以获取按日期过滤的项目。

于 2012-06-10T10:12:40.153 回答