日历 CAML 查询,获取当月事件
Calendar CAML Query, get current month event
请问如何从日历中获取当前月份的事件?
这里有我的源代码。
ClientContext clientContext = new ClientContext("https://intra.aspac.XXX.com/sites/sg");
Guid guid = new Guid("1F62FC88-XXXX-XXXX-XXXX-091D3023A99F");
List list = clientContext.Web.Lists.GetById(guid);
CamlQuery query = new CamlQuery();
query.ViewXml = "<View/>";
ListItemCollection items = list.GetItems(query);
clientContext.Load(list);
clientContext.Load(items);
clientContext.ExecuteQuery();
foreach (ListItem item in items)
{
//Console.WriteLine(item.Id + " - " + item["Name"]);
Label1.Text = " - " + item["Title"] + item["Description"] + item["EventDate"] + "/"+item.Id;
}
您将面临在 Firefox 中呈现列表 RSS 提要的问题
为了解决此问题,您可以对 Sharepoint 使用以下解决方法 on-premise:创建 ashx 处理程序,将其放入 /Layouts 子文件夹,处理程序的代码将向 OTB /_layouts/ 发送内部 http 请求15/listfeed.aspx?List={listId} url,然后通过 Regex 和 return 删除附件标签到响应的最终结果(即为 OTB RSS 提要实施一种代理):
string url = string.Format("{0}?List={1}", SPUrlUtility.CombineUrl(web.Url, "_layouts/15/listfeed.aspx"), list.ID);
var request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultNetworkCredentials;
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
}
result = Regex.Replace(result, @"<enclosure.+?/>", string.Empty);
}
您可以像这样使用 CAML 查询获取当月事件:
using System;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Utilities;
namespace ConsoleApplication7
{
class Class1
{
static void Main(string[] args)
{
DateTime firstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
string firstDayValue = SPUtility.CreateISO8601DateTimeFromSystemDateTime(firstDay);
string firstDayValueplus1Month = SPUtility.CreateISO8601DateTimeFromSystemDateTime(firstDay.AddMonths(1));
ClientContext clientContext = new ClientContext("http://sp/sites/dev");
Guid guid = new Guid("d62d9bae-8dce-4aa6-aedb-73e82cd1415b");
List list = clientContext.Web.Lists.GetById(guid);
CamlQuery query = new CamlQuery();
query.ViewXml = "<View><Query><Where><And><Geq><FieldRef Name='EventDate' /><Value Type='DateTime'>" + firstDayValue +
"</Value></Geq><Leq><FieldRef Name='EndDate' /><Value Type='DateTime'>" + firstDayValueplus1Month +
"</Value></Leq></And></Where>"+"</Query></View>";
ListItemCollection items = list.GetItems(query);
clientContext.Load(list);
clientContext.Load(items);
clientContext.ExecuteQuery();
foreach (ListItem item in items)
{
}
}
}
}
请问如何从日历中获取当前月份的事件?
这里有我的源代码。
ClientContext clientContext = new ClientContext("https://intra.aspac.XXX.com/sites/sg");
Guid guid = new Guid("1F62FC88-XXXX-XXXX-XXXX-091D3023A99F");
List list = clientContext.Web.Lists.GetById(guid);
CamlQuery query = new CamlQuery();
query.ViewXml = "<View/>";
ListItemCollection items = list.GetItems(query);
clientContext.Load(list);
clientContext.Load(items);
clientContext.ExecuteQuery();
foreach (ListItem item in items)
{
//Console.WriteLine(item.Id + " - " + item["Name"]);
Label1.Text = " - " + item["Title"] + item["Description"] + item["EventDate"] + "/"+item.Id;
}
您将面临在 Firefox 中呈现列表 RSS 提要的问题
为了解决此问题,您可以对 Sharepoint 使用以下解决方法 on-premise:创建 ashx 处理程序,将其放入 /Layouts 子文件夹,处理程序的代码将向 OTB /_layouts/ 发送内部 http 请求15/listfeed.aspx?List={listId} url,然后通过 Regex 和 return 删除附件标签到响应的最终结果(即为 OTB RSS 提要实施一种代理):
string url = string.Format("{0}?List={1}", SPUrlUtility.CombineUrl(web.Url, "_layouts/15/listfeed.aspx"), list.ID);
var request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultNetworkCredentials;
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
}
result = Regex.Replace(result, @"<enclosure.+?/>", string.Empty);
}
您可以像这样使用 CAML 查询获取当月事件:
using System;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Utilities;
namespace ConsoleApplication7
{
class Class1
{
static void Main(string[] args)
{
DateTime firstDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
string firstDayValue = SPUtility.CreateISO8601DateTimeFromSystemDateTime(firstDay);
string firstDayValueplus1Month = SPUtility.CreateISO8601DateTimeFromSystemDateTime(firstDay.AddMonths(1));
ClientContext clientContext = new ClientContext("http://sp/sites/dev");
Guid guid = new Guid("d62d9bae-8dce-4aa6-aedb-73e82cd1415b");
List list = clientContext.Web.Lists.GetById(guid);
CamlQuery query = new CamlQuery();
query.ViewXml = "<View><Query><Where><And><Geq><FieldRef Name='EventDate' /><Value Type='DateTime'>" + firstDayValue +
"</Value></Geq><Leq><FieldRef Name='EndDate' /><Value Type='DateTime'>" + firstDayValueplus1Month +
"</Value></Leq></And></Where>"+"</Query></View>";
ListItemCollection items = list.GetItems(query);
clientContext.Load(list);
clientContext.Load(items);
clientContext.ExecuteQuery();
foreach (ListItem item in items)
{
}
}
}
}