1
+ using Newtonsoft . Json ;
2
+ using OfficeDevPnP . MSGraphAPIDemo . Models ;
3
+ using System ;
4
+ using System . Collections . Generic ;
5
+ using System . Linq ;
6
+ using System . Web ;
7
+
8
+ namespace OfficeDevPnP . MSGraphAPIDemo . Components
9
+ {
10
+ public static class CalendarHelper
11
+ {
12
+ /// <summary>
13
+ /// This method retrieves the calendars of the current user
14
+ /// </summary>
15
+ /// <param name="startIndex">The startIndex (0 based) of the items to retrieve, optional</param>
16
+ /// <returns>A page of up to 10 calendars</returns>
17
+ public static List < Calendar > ListCalendars ( Int32 startIndex = 0 )
18
+ {
19
+ String jsonResponse = MicrosoftGraphHelper . MakeGetRequestForString (
20
+ String . Format ( "{0}me/calendars?$skip={1}" ,
21
+ MicrosoftGraphHelper . MicrosoftGraphV1BaseUri ,
22
+ startIndex ) ) ;
23
+
24
+ var calendarList = JsonConvert . DeserializeObject < CalendarList > ( jsonResponse ) ;
25
+ return ( calendarList . Calendars ) ;
26
+ }
27
+
28
+ /// <summary>
29
+ /// This method retrieves the calendars of the current user
30
+ /// </summary>
31
+ /// <param name="id">The ID of the calendar</param>
32
+ /// <returns>The calendar</returns>
33
+ public static Calendar GetCalendar ( String id )
34
+ {
35
+ String jsonResponse = MicrosoftGraphHelper . MakeGetRequestForString (
36
+ String . Format ( "{0}me/calendars/{1}" ,
37
+ MicrosoftGraphHelper . MicrosoftGraphV1BaseUri ,
38
+ id ) ) ;
39
+
40
+ var calendar = JsonConvert . DeserializeObject < Calendar > ( jsonResponse ) ;
41
+ return ( calendar ) ;
42
+ }
43
+
44
+ /// <summary>
45
+ /// This method retrieves the events of the current user's calendar
46
+ /// </summary>
47
+ /// <param name="calendarId">The ID of the calendar</param>
48
+ /// <param name="startIndex">The startIndex (0 based) of the items to retrieve, optional</param>
49
+ /// <returns>A page of up to 10 events</returns>
50
+ public static List < Event > ListEvents ( String calendarId , Int32 startIndex = 0 )
51
+ {
52
+ String jsonResponse = MicrosoftGraphHelper . MakeGetRequestForString (
53
+ String . Format ( "{0}me/calendars/{1}/events?$skip={2}" ,
54
+ MicrosoftGraphHelper . MicrosoftGraphV1BaseUri ,
55
+ calendarId ,
56
+ startIndex ) ) ;
57
+
58
+ var eventList = JsonConvert . DeserializeObject < EventList > ( jsonResponse ) ;
59
+ return ( eventList . Events ) ;
60
+ }
61
+
62
+ /// <summary>
63
+ /// This method retrieves the events of the current user's calendar within a specific date range
64
+ /// </summary>
65
+ /// <param name="calendarId">The ID of the calendar</param>
66
+ /// <param name="startDate">The start date of the range</param>
67
+ /// <param name="endDate">The end date of the range</param>
68
+ /// <param name="startIndex">The startIndex (0 based) of the items to retrieve, optional</param>
69
+ /// <returns>A page of up to 10 events</returns>
70
+ public static List < Event > ListEvents ( String calendarId , DateTime startDate ,
71
+ DateTime endDate , Int32 startIndex = 0 )
72
+ {
73
+ String jsonResponse = MicrosoftGraphHelper . MakeGetRequestForString (
74
+ String . Format ( "{0}me/calendars/{1}/calendarView?startDateTime={2:o}&endDateTime={3:o}&$skip={4}" ,
75
+ MicrosoftGraphHelper . MicrosoftGraphV1BaseUri ,
76
+ calendarId ,
77
+ startDate . ToUniversalTime ( ) ,
78
+ endDate . ToUniversalTime ( ) ,
79
+ startIndex ) ) ;
80
+
81
+ var eventList = JsonConvert . DeserializeObject < EventList > ( jsonResponse ) ;
82
+ return ( eventList . Events ) ;
83
+ }
84
+
85
+ /// <summary>
86
+ /// This method retrieves the events of a series within a specific date range
87
+ /// </summary>
88
+ /// <param name="calendarId">The ID of the calendar</param>
89
+ /// <param name="masterSeriesId">The ID of the master event of the series</param>
90
+ /// <param name="startDate">The start date of the range</param>
91
+ /// <param name="endDate">The end date of the range</param>
92
+ /// <param name="startIndex">The startIndex (0 based) of the items to retrieve, optional</param>
93
+ /// <returns>A page of up to 10 events</returns>
94
+ public static List < Event > ListSeriesInstances ( String calendarId ,
95
+ String masterSeriesId ,
96
+ DateTime startDate ,
97
+ DateTime endDate ,
98
+ Int32 startIndex = 0 )
99
+ {
100
+ String jsonResponse = MicrosoftGraphHelper . MakeGetRequestForString (
101
+ String . Format ( "{0}me/calendars/{1}/events/{2}/instances?startDateTime={3:o}&endDateTime={4:o}&$skip={5}" ,
102
+ MicrosoftGraphHelper . MicrosoftGraphV1BaseUri ,
103
+ calendarId ,
104
+ masterSeriesId ,
105
+ startDate . ToUniversalTime ( ) ,
106
+ endDate . ToUniversalTime ( ) ,
107
+ startIndex ) ) ;
108
+
109
+ var eventList = JsonConvert . DeserializeObject < EventList > ( jsonResponse ) ;
110
+ return ( eventList . Events ) ;
111
+ }
112
+
113
+ /// <summary>
114
+ /// This method creates an event in a target calendar
115
+ /// </summary>
116
+ /// <param name="calendarId">The ID of the target calendar</param>
117
+ /// <param name="calendarEvent">The event to add</param>
118
+ /// <returns>The added event</returns>
119
+ public static Event CreateEvent ( String calendarId , Event calendarEvent )
120
+ {
121
+ String jsonResponse = MicrosoftGraphHelper . MakePostRequestForString (
122
+ String . Format ( "{0}me/calendars/{1}/events" ,
123
+ MicrosoftGraphHelper . MicrosoftGraphV1BaseUri ,
124
+ calendarId ) ,
125
+ calendarEvent , "application/json" ) ;
126
+
127
+ var createdEvent = JsonConvert . DeserializeObject < Event > ( jsonResponse ) ;
128
+ return ( createdEvent ) ;
129
+ }
130
+
131
+ /// <summary>
132
+ /// This method retrieves an event from a calendar
133
+ /// </summary>
134
+ /// <param name="calendarId">The ID of the calendar</param>
135
+ /// <param name="eventId">The ID of the event</param>
136
+ /// <returns>The retrieved event</returns>
137
+ public static Event GetEvent ( String calendarId , String eventId )
138
+ {
139
+ String jsonResponse = MicrosoftGraphHelper . MakeGetRequestForString (
140
+ String . Format ( "{0}me/calendars/{1}/events/{2}" ,
141
+ MicrosoftGraphHelper . MicrosoftGraphV1BaseUri ,
142
+ calendarId , eventId ) ) ;
143
+
144
+ var calendarEvent = JsonConvert . DeserializeObject < Event > ( jsonResponse ) ;
145
+ return ( calendarEvent ) ;
146
+ }
147
+
148
+ /// <summary>
149
+ /// This method updates an event in a calendar
150
+ /// </summary>
151
+ /// <param name="calendarId">The ID of the calendar</param>
152
+ /// <param name="eventId">The event to update</param>
153
+ /// <returns>The updated event</returns>
154
+ public static Event UpdateEvent ( String calendarId , Event eventToUpdate )
155
+ {
156
+ String jsonResponse = MicrosoftGraphHelper . MakePatchRequestForString (
157
+ String . Format ( "{0}me/calendars/{1}/events/{2}" ,
158
+ MicrosoftGraphHelper . MicrosoftGraphV1BaseUri ,
159
+ calendarId , eventToUpdate . Id ) ,
160
+ eventToUpdate , "application/json" ) ;
161
+
162
+ var updatedEvent = JsonConvert . DeserializeObject < Event > ( jsonResponse ) ;
163
+ return ( updatedEvent ) ;
164
+ }
165
+
166
+ /// <summary>
167
+ /// This method deletes an event from a calendar
168
+ /// </summary>
169
+ /// <param name="calendarId">The ID of the calendar</param>
170
+ /// <param name="eventId">The ID of the event to delete</param>
171
+ public static void DeleteEvent ( String calendarId , String eventId )
172
+ {
173
+ MicrosoftGraphHelper . MakeDeleteRequest (
174
+ String . Format ( "{0}me/calendars/{1}/events/{2}" ,
175
+ MicrosoftGraphHelper . MicrosoftGraphV1BaseUri ,
176
+ calendarId , eventId ) ) ;
177
+ }
178
+
179
+ /// <summary>
180
+ /// This method provides a feedback for a received meeting request
181
+ /// </summary>
182
+ /// <param name="calendarId">The ID of the calendar</param>
183
+ /// <param name="eventId">The ID of the meeting request</param>
184
+ /// <param name="feedback">The feedback for the meeting request</param>
185
+ /// <param name="comment">Any comment to include in the feedback, optional</param>
186
+ public static void SendFeedbackForMeetingRequest ( String calendarId ,
187
+ String eventId ,
188
+ MeetingRequestFeedback feedback ,
189
+ String comment = null )
190
+ {
191
+ MicrosoftGraphHelper . MakePostRequest (
192
+ String . Format ( "{0}me/calendars/{1}/events/{2}/{3}" ,
193
+ MicrosoftGraphHelper . MicrosoftGraphV1BaseUri ,
194
+ calendarId , eventId , feedback ) ,
195
+ content : ! String . IsNullOrEmpty ( comment ) ? new { Comment = comment } : null ,
196
+ contentType : "application/json" ) ;
197
+ }
198
+ }
199
+
200
+ /// <summary>
201
+ /// Defines the possible feedbacks for a meeting request
202
+ /// </summary>
203
+ public enum MeetingRequestFeedback
204
+ {
205
+ Accept ,
206
+ Decline ,
207
+ TentativelyAccept ,
208
+ }
209
+ }
0 commit comments