Search code examples
c#sharepoint-2013caml

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

Solution

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