简体   繁体   中英

Calendar CAML Query, get current month event

May I ask about how can I get the current month event from Calendar?

Here with my Source code.

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;
}

You can get current month event using CAML Query like this:

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)
            {

            }
            
        }
    }
}

You will facing the Problem with rendering list RSS feed in Firefox

In order to fix this issue you may use the following workaround for Sharepoint on-premise: create ashx handler, put it to /Layouts subfolder, code of the handler will send internal http request to OTB /_layouts/15/listfeed.aspx?List={listId} url, then remove enclosure tag via Regex and return final result to the response (ie implement kind of proxy for OTB RSS feed):

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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