使用REST API从SharePoint日历中检索事件

时间:2017-07-18 19:57:14

标签: rest sharepoint calendar

我有其余的端点用于在工作时从我的内部网络访问sharepoint日历。它看起来像这样: https://teamsites。{COMPANY}的.com /位点/ {SITE_URI} / _ AP​​I /网络/列表/ getbytitle({TITLE-OF-CALENDAR})/项目

当我对上述网址发出授权的GET请求时,它会给我一个日历中4个事件的列表,但日期是随机的。为了检索当月的所有事件,我需要指定什么端点?

另外,我猜我每次请求只获得4个事件的原因是因为我没有处理的一些分页事件......如果你知道如何在一个请求中检索所有事件,那个将是一个奖金。

提前致谢!

1 个答案:

答案 0 :(得分:4)

可以通过以下CAML查询检索当月的事件:

<Where>
   <DateRangesOverlap>
      <FieldRef Name='EventDate' />
      <FieldRef Name='EndDate' />
      <Value Type='DateTime'>
          <Month />
      </Value>
   </DateRangesOverlap>
</Where>

就此而言,可以使用GetItems method,如下所示:

var query = `
<Where>
   <DateRangesOverlap>
      <FieldRef Name='EventDate' />
      <FieldRef Name='EndDate' />
      <Value Type='DateTime'>
          <Month />
      </Value>
   </DateRangesOverlap>
</Where>`


getListItems(_spPageContextInfo.webAbsoluteUrl,'TeamCalendar',query)
.done(function(data){
     var items = data.d.results;
     for(var i = 0; i < items.length;i++) {
         console.log(items[i].Title);
     }    
})
.fail(function(error){
    console.log(JSON.stringify(error));
});

,其中

function getListItems(webUrl,listTitle, queryText) 
{
    var viewXml = '<View><Query>' + queryText + '</Query></View>';
    var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems"; 
    var queryPayload = {  
               'query' : {
                      '__metadata': { 'type': 'SP.CamlQuery' }, 
                      'ViewXml' : viewXml  
               }
    };
    return $.ajax({
           url: url,
           method: "POST",
           data: JSON.stringify(queryPayload),
           headers: {
              "X-RequestDigest": $("#__REQUESTDIGEST").val(),
              "Accept": "application/json; odata=verbose",
              "content-type": "application/json; odata=verbose"
           }
     });
}

但是这种方法有一个限制,REST和CSOM / JSOM API都不支持 支持针对重复事件进行扩展(仅表示单个事件项目将返回重复活动)。请参阅这些请求以获取更多详细信息:

对于这种情况,遗留的SharePoint Web服务正在拯救,特别是Lists Web Service

以下示例演示了如何检索当前月份的事件并展开定期事件(此处使用了SPServices library):

$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "TeamCal",
    CAMLViewFields: "<ViewFields>" +
            "<FieldRef Name='Title' />" +
            "<FieldRef Name='EventDate' />" +
            "<FieldRef Name='EndDate' />" +
            "<FieldRef Name='Location' />" +
            "<FieldRef Name='Description' />" +
            "<FieldRef Name='fRecurrence' />" +
            "<FieldRef Name='RecurrenceData' />" +
            "<FieldRef Name='fAllDayEvent' />" +
        "</ViewFields>",
    CAMLQuery: "<Query>" +
            "<Where>" +
                "<DateRangesOverlap>" +
                    "<FieldRef Name='EventDate' />" +
                    "<FieldRef Name='EndDate' />" +
                    "<FieldRef Name='RecurrenceID' />" +
                    "<Value Type='DateTime'>" +
                        "<Month />" +
                    "</Value>" +
                "</DateRangesOverlap>" +
            "</Where>" +
            "<OrderBy>" +
                "<FieldRef Name='EventDate' />" +
            "</OrderBy>" +
        "</Query>",
    CAMLQueryOptions: "<QueryOptions>" +
            "<RecurrencePatternXMLVersion>v3</RecurrencePatternXMLVersion>" +
            "<ExpandRecurrence>TRUE</ExpandRecurrence>" +
        "</QueryOptions>",
    completefunc: function (xData, Status) {
        $(xData.responseXML).SPFilterNode("z:row").each(function() {

            var $node = $(this);
            var eventTitle = $node.attr("ows_Title");
            console.log(eventTitle);

        });
    }
})