Un esempio di come interrogare una lista di tipo calendario di SharePoint 2010 in C#

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      using (SPSite site = new SPSite("http://sharepoint2010.sgart.local"))
      {
        using (SPWeb web = site.OpenWeb())
        {
          SPList list = web.GetList(web.ServerRelativeUrl.TrimEnd('/') + "/Lists/Calendar");
          SPListItemCollection items = list.Items;
          foreach (SPListItem item in items)
          {
            Console.WriteLine("ID: " + item.ID.ToString());
            Console.WriteLine("ContentType: " + item[SPBuiltInFieldId.ContentType].ToString());
            Console.WriteLine("Title: " + item.Title);
            Console.WriteLine("StartTime: " + item[SPBuiltInFieldId.StartDate].ToString());
            Console.WriteLine("EndTime: " + item[SPBuiltInFieldId.EndDate].ToString());
            Console.WriteLine("AllDayEvent: " + item[SPBuiltInFieldId.fAllDayEvent].ToString());
            Console.WriteLine("Recurrence: " + item[SPBuiltInFieldId.fRecurrence].ToString());
            Console.WriteLine("RecurrenceData: " + (item[SPBuiltInFieldId.RecurrenceData] == null ? "" : item[SPBuiltInFieldId.RecurrenceData].ToString()));
            Console.WriteLine("EventType: " + item[SPBuiltInFieldId.EventType].ToString());
            Console.WriteLine("UID: " + (item[SPBuiltInFieldId.UID] == null ? "" : item[SPBuiltInFieldId.UID].ToString()));
            Console.WriteLine("RecurrenceID: " + (item[SPBuiltInFieldId.RecurrenceID] == null ? "" : item[SPBuiltInFieldId.RecurrenceID].ToString()));
            Console.WriteLine("EventCanceled: " + (item[SPBuiltInFieldId.EventCanceled] == null ? "" : item[SPBuiltInFieldId.EventCanceled].ToString()));
            Console.WriteLine("Duration: " + item[SPBuiltInFieldId.Duration].ToString());

            Console.WriteLine("Location: " + (item[SPBuiltInFieldId.Location] == null ? "" : item[SPBuiltInFieldId.Location].ToString()));
            Console.WriteLine("------------------------------------");
          }

        }
      }
      Console.ReadKey();
    }
  }
}
da un risultato simile al seguente

Text

ID: 5
ContentType: Event
Title: acadsfdsf
StartTime: 9/14/2012 1:00:00 PM
EndTime: 9/14/2012 2:00:00 PM
AllDayEvent: False
Recurrence: False
RecurrenceData:
EventType: 0
UID:
RecurrenceID:
EventCanceled:
Duration: 3600
Location:
------------------------------------
ID: 6
ContentType: Event
Title: 1 - Evento continuo
StartTime: 6/3/2013 10:00:00 PM
EndTime: 9/3/2013 10:00:00 AM
AllDayEvent: False
Recurrence: False
RecurrenceData:
EventType: 0
UID:
RecurrenceID:
EventCanceled:
Duration: 7905600
Location:
------------------------------------
ID: 8
ContentType: Event
Title: 3 - Evento singolo
StartTime: 7/10/2013 2:00:00 PM
EndTime: 7/10/2013 6:00:00 PM
AllDayEvent: False
Recurrence: False
RecurrenceData:
EventType: 0
UID:
RecurrenceID:
EventCanceled:
Duration: 14400
Location: Milano
------------------------------------
ID: 9
ContentType: Event
Title: 5 - All day event
StartTime: 7/19/2013 12:00:00 AM
EndTime: 7/19/2013 11:59:00 PM
AllDayEvent: True
Recurrence: False
RecurrenceData:
EventType: 0
UID:
RecurrenceID:
EventCanceled:
Duration: 86340
Location:
------------------------------------
ID: 10
ContentType: Event
Title: 6 - Più giorni
StartTime: 7/24/2013 12:00:00 AM
EndTime: 7/26/2013 11:59:00 PM
AllDayEvent: True
Recurrence: False
RecurrenceData:
EventType: 0
UID:
RecurrenceID:
EventCanceled:
Duration: 259140
Location:
------------------------------------
ID: 11
ContentType: Event
Title: 4 - due giorni ora
StartTime: 7/12/2013 11:00:00 AM
EndTime: 7/13/2013 2:30:00 PM
AllDayEvent: False
Recurrence: False
RecurrenceData:
EventType: 0
UID:
RecurrenceID:
EventCanceled:
Duration: 99000
Location:
------------------------------------
ID: 13
ContentType: Event
Title: 2 - Ogni lunedì
StartTime: 6/10/2013 4:00:00 AM
EndTime: 7/26/2032 5:00:00 PM
AllDayEvent: False
Recurrence: True
RecurrenceData: <recurrence><rule><firstDayOfWeek>mo</firstDayOfWeek><repeat><weekly mo="TRUE" weekFrequency="1" /></repeat><repeatForever>FALSE</repeatForever></rule></recurrence>
EventType: 1
UID: {571153F5-E349-4BAC-A5D7-0A8520E20FEE}
RecurrenceID:
EventCanceled:
Duration: 46800
Location:
------------------------------------
in questo caso l'ho trattata come una normale lista e come puoi vedere gli eventi ricorrenti non vengono spittati, ma compaiono come unico evento dove tutti i dettagli si trovano in altri campi, come ad esempio RecurrenceData.

Per elencare tutti gli eventi di un calendario, comproso quelli ricorrenti, bisogna costruire una query CAML utilizzando l'oggetto SPQuery impostando le proprietà ExpandRecurrence e CalendarDate:

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      using (SPSite site = new SPSite("http://intranet.contoso.com"))
      {
        using (SPWeb web = site.OpenWeb())
        {
          SPList list = web.GetList(web.ServerRelativeUrl.TrimEnd('/') + "/Lists/Calendar");
          SPQuery query = new SPQuery();
          query.Query = @"<Where>
            <DateRangesOverlap>
              <FieldRef Name='EventDate' />
              <FieldRef Name='EndDate' />
              <FieldRef Name='RecurrenceID' />
              <Value Type='DateTime' IncludeTimeValue='TRUE'>
                <Month />
              </Value>
            </DateRangesOverlap>
          </Where>";  // sintassi caml per interrogare una lista di tipo calendario ed ottenere tutti gli eventi di un mese
          query.CalendarDate = DateTime.Today; // passo la data corrente per selezionare il mese
          query.ExpandRecurrence = true;  //faccio in modo che gli eventi ricorrenti vengano espansi
          SPListItemCollection items = list.GetItems(query);
          foreach (SPListItem item in items)
          {
            Console.WriteLine("ID: " + item.ID.ToString());
            Console.WriteLine("ContentType: " + item[SPBuiltInFieldId.ContentType].ToString());
            Console.WriteLine("Title: " + item.Title);
            Console.WriteLine("StartTime: " + item[SPBuiltInFieldId.StartDate].ToString());
            Console.WriteLine("EndTime: " + item[SPBuiltInFieldId.EndDate].ToString());
            Console.WriteLine("AllDayEvent: " + item[SPBuiltInFieldId.fAllDayEvent].ToString());
            Console.WriteLine("Recurrence: " + item[SPBuiltInFieldId.fRecurrence].ToString());
            Console.WriteLine("RecurrenceData: " + (item[SPBuiltInFieldId.RecurrenceData] == null ? "" : item[SPBuiltInFieldId.RecurrenceData].ToString()));
            Console.WriteLine("EventType: " + item[SPBuiltInFieldId.EventType].ToString());
            Console.WriteLine("UID: " + (item[SPBuiltInFieldId.UID] == null ? "" : item[SPBuiltInFieldId.UID].ToString()));
            Console.WriteLine("RecurrenceID: " + (item[SPBuiltInFieldId.RecurrenceID] == null ? "" : item[SPBuiltInFieldId.RecurrenceID].ToString()));
            Console.WriteLine("EventCanceled: " + (item[SPBuiltInFieldId.EventCanceled] == null ? "" : item[SPBuiltInFieldId.EventCanceled].ToString()));
            Console.WriteLine("Duration: " + item[SPBuiltInFieldId.Duration].ToString());

            Console.WriteLine("Location: " + (item[SPBuiltInFieldId.Location] == null ? "" : item[SPBuiltInFieldId.Location].ToString()));
            Console.WriteLine("------------------------------------");
          }
        }
      }
      Console.ReadKey();
    }
  }
}
in questo caso gli eventi ricorrenti vengono espansi, compaiono come singoli item e le date di inizio e fine vengono settate correttamente:

Text

ID: 6
ContentType: Event
Title: 1 - Evento continuo
StartTime: 6/3/2013 10:00:00 PM
EndTime: 9/3/2013 10:00:00 AM
AllDayEvent: False
Recurrence: False
RecurrenceData:
EventType: 0
UID:
RecurrenceID: 6/3/2013 10:00:00 PM
EventCanceled:
Duration: 7905600
Location:
------------------------------------
ID: 13
ContentType: Event
Title: 2 - Ogni lunedì
StartTime: 6/24/2013 4:00:00 AM
EndTime: 6/24/2013 5:00:00 PM
AllDayEvent: False
Recurrence: True
RecurrenceData: <recurrence><rule><firstDayOfWeek>mo</firstDayOfWeek><repeat><weekly mo="TRUE" weekFrequency="1" /></repeat><repeatForever>FALSE</repeatForever></rule></recurrence>
EventType: 5
UID: {571153F5-E349-4BAC-A5D7-0A8520E20FEE}
RecurrenceID: 6/24/2013 4:00:00 AM
EventCanceled:
Duration: 46800
Location:
------------------------------------
ID: 13
ContentType: Event
Title: 2 - Ogni lunedì
StartTime: 7/1/2013 4:00:00 AM
EndTime: 7/1/2013 5:00:00 PM
AllDayEvent: False
Recurrence: True
RecurrenceData: <recurrence><rule><firstDayOfWeek>mo</firstDayOfWeek><repeat><weekly mo="TRUE" weekFrequency="1" /></repeat><repeatForever>FALSE</repeatForever></rule></recurrence>
EventType: 5
UID: {571153F5-E349-4BAC-A5D7-0A8520E20FEE}
RecurrenceID: 7/1/2013 4:00:00 AM
EventCanceled:
Duration: 46800
Location:
------------------------------------
ID: 13
ContentType: Event
Title: 2 - Ogni lunedì
StartTime: 7/8/2013 4:00:00 AM
EndTime: 7/8/2013 5:00:00 PM
AllDayEvent: False
Recurrence: True
RecurrenceData: <recurrence><rule><firstDayOfWeek>mo</firstDayOfWeek><repeat><weekly mo="TRUE" weekFrequency="1" /></repeat><repeatForever>FALSE</repeatForever></rule></recurrence>
EventType: 5
UID: {571153F5-E349-4BAC-A5D7-0A8520E20FEE}
RecurrenceID: 7/8/2013 4:00:00 AM
EventCanceled:
Duration: 46800
Location:
------------------------------------
ID: 8
ContentType: Event
Title: 3 - Evento singolo
StartTime: 7/10/2013 2:00:00 PM
EndTime: 7/10/2013 6:00:00 PM
AllDayEvent: False
Recurrence: False
RecurrenceData:
EventType: 0
UID:
RecurrenceID: 7/10/2013 2:00:00 PM
EventCanceled:
Duration: 14400
Location: Milano
------------------------------------
ID: 11
ContentType: Event
Title: 4 - due giorni ora
StartTime: 7/12/2013 11:00:00 AM
EndTime: 7/13/2013 2:30:00 PM
AllDayEvent: False
Recurrence: False
RecurrenceData:
EventType: 0
UID:
RecurrenceID: 7/12/2013 11:00:00 AM
EventCanceled:
Duration: 99000
Location:
------------------------------------
ID: 13
ContentType: Event
Title: 2 - Ogni lunedì
StartTime: 7/15/2013 4:00:00 AM
EndTime: 7/15/2013 5:00:00 PM
AllDayEvent: False
Recurrence: True
RecurrenceData: <recurrence><rule><firstDayOfWeek>mo</firstDayOfWeek><repeat><weekly mo="TRUE" weekFrequency="1" /></repeat><repeatForever>FALSE</repeatForever></rule></recurrence>
EventType: 5
UID: {571153F5-E349-4BAC-A5D7-0A8520E20FEE}
RecurrenceID: 7/15/2013 4:00:00 AM
EventCanceled:
Duration: 46800
Location:
------------------------------------
ID: 9
ContentType: Event
Title: 5 - All day event
StartTime: 7/19/2013 12:00:00 AM
EndTime: 7/19/2013 11:59:00 PM
AllDayEvent: True
Recurrence: False
RecurrenceData:
EventType: 0
UID:
RecurrenceID: 7/19/2013 2:00:00 AM
EventCanceled:
Duration: 86340
Location:
------------------------------------
ID: 13
ContentType: Event
Title: 2 - Ogni lunedì
StartTime: 7/22/2013 4:00:00 AM
EndTime: 7/22/2013 5:00:00 PM
AllDayEvent: False
Recurrence: True
RecurrenceData: <recurrence><rule><firstDayOfWeek>mo</firstDayOfWeek><repeat><weekly mo="TRUE" weekFrequency="1" /></repeat><repeatForever>FALSE</repeatForever></rule></recurrence>
EventType: 5
UID: {571153F5-E349-4BAC-A5D7-0A8520E20FEE}
RecurrenceID: 7/22/2013 4:00:00 AM
EventCanceled:
Duration: 46800
Location:
------------------------------------
ID: 10
ContentType: Event
Title: 6 - Più giorni
StartTime: 7/24/2013 12:00:00 AM
EndTime: 7/26/2013 11:59:00 PM
AllDayEvent: True
Recurrence: False
RecurrenceData:
EventType: 0
UID:
RecurrenceID: 7/24/2013 2:00:00 AM
EventCanceled:
Duration: 259140
Location:
------------------------------------
ID: 13
ContentType: Event
Title: 2 - Ogni lunedì
StartTime: 7/29/2013 4:00:00 AM
EndTime: 7/29/2013 5:00:00 PM
AllDayEvent: False
Recurrence: True
RecurrenceData: <recurrence><rule><firstDayOfWeek>mo</firstDayOfWeek><repeat><weekly mo="TRUE" weekFrequency="1" /></repeat><repeatForever>FALSE</repeatForever></rule></recurrence>
EventType: 5
UID: {571153F5-E349-4BAC-A5D7-0A8520E20FEE}
RecurrenceID: 7/29/2013 4:00:00 AM
EventCanceled:
Duration: 46800
Location:
------------------------------------
ID: 13
ContentType: Event
Title: 2 - Ogni lunedì
StartTime: 8/5/2013 4:00:00 AM
EndTime: 8/5/2013 5:00:00 PM
AllDayEvent: False
Recurrence: True
RecurrenceData: <recurrence><rule><firstDayOfWeek>mo</firstDayOfWeek><repeat><weekly mo="TRUE" weekFrequency="1" /></repeat><repeatForever>FALSE</repeatForever></rule></recurrence>
EventType: 5
UID: {571153F5-E349-4BAC-A5D7-0A8520E20FEE}
RecurrenceID: 8/5/2013 4:00:00 AM
EventCanceled:
Duration: 46800
Location:

Nella query CAML oltre al tag Month c'è anche il tag Year, Week, Day e Today.

Vedi anche DateRangesOverlap Element (Query).
Tags:
C#236 CAML17 SharePoint498 SharePoint 2010224
Potrebbe interessarti anche: