/
Service.asmx.cs
252 lines (207 loc) · 8.45 KB
/
Service.asmx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Autodiscover;
using System.Xml;
namespace CMCWebService
{
/// <summary>
/// Summary description for Calendar Manipulation Web Service:
/// New - Creates a new Entry on the Calendar
/// Update - Updates an existing Entry on the Calendar
/// Delete - Deletes an existing Entry on the Calendar
/// </summary>
///
[WebService(Namespace = "http://yourdomain.com/namespace")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
/**
* Function GetBinding
* Creates the connection to Office 365
**/
static ExchangeService GetBinding()
{
Console.WriteLine("Creating Binding");
// Create a New Binding
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
// Define Service Credentials (Either AppImp or User's)
service.Credentials = new WebCredentials("username", "password");
// Turn on tracing for debugging
service.TraceListener = new CMCWebService.TraceListener();
service.TraceFlags = TraceFlags.All;
service.TraceEnabled = true;
// Use the AutodiscoverUrl method to locate the service endpoint for a User
try
{
// Tries to find the AutoDiscoverURL and compares it to your ValidationCallback function (below)
// If it does not validate, it throws an exception
service.AutodiscoverUrl("username", RedirectionUrlValidationCallback);
}
catch (AutodiscoverRemoteException ex)
{
Console.WriteLine("Exception thrown: " + ex.Error.Message);
}
// Display the service URL
Console.WriteLine("AutodiscoverURL: " + service.Url);
return service;
}
/**
* Function: RedirectionUrlValidationCallback
* Validates that the returned AutoDiscover URL is indeeded a Microsoft Online URL
**/
static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
// Perform validation.
// Validation is developer dependent to ensure a safe redirect.
return (redirectionUrl == "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml");
}
[WebMethod]
public string NewApp(string subject, string body, string startDate, int duration, string status)
{
try
{
// Reformat date
DateTime date = DateTime.Parse(startDate);
// Create the Service Binding
ExchangeService service = GetBinding();
// Runs the method to create the new appointment
string newAppId = CreateNewAppointment(service, subject, body, date, duration, status);
// Returns the ItemId for future changes.
return newAppId;
}
catch (HttpException h)
{
return "Caught HttpException: " + h;
}
catch (Exception e) {
return "Generic Exception: " + e;
}
}
/**
* Function: CreateNewAppointment
* Creates a new appointment.
* Returns the AppID (appointment id) for future changes
**/
static string CreateNewAppointment(ExchangeService service, string subject, string body, DateTime date, int duration, string status)
{
Console.WriteLine("Creating a New Item: " + subject);
// Create an appointmet and identify the Exchange service.
Appointment appointment = new Appointment(service);
// Set details
appointment.Subject = subject;
appointment.Body = body;
appointment.Start = date;
appointment.End = appointment.Start.AddHours(duration);
StringList categories = new Microsoft.Exchange.WebServices.Data.StringList();
categories.Add(status);
appointment.Categories = categories;
appointment.IsReminderSet = false;
// Save and Send
appointment.Save(SendInvitationsMode.SendToNone);
// Get ItemId for future updates
return ""+appointment.Id+"";
}
[WebMethod]
public string UpdateApp(string appId, string subject, string body, string startDate, string duration, string status)
{
// Reformat date
// Sets a default time
DateTime date = DateTime.Parse("01/01/12 00:00:00");
try
{
// Tries to set the time, succeeds if not null
date = DateTime.Parse(startDate);
}
catch (FormatException)
{
Console.WriteLine("Date not passed in.");
}
finally
{
// Sets an invalid default duration
int dur = -1;
try
{
// Reformat Duration, if exists
dur = int.Parse(duration);
}
catch (FormatException)
{
Console.WriteLine("Duration not passed in.");
}
finally
{
// Create the Service Binding
ExchangeService service = GetBinding();
// Runs the method to update an existing appointment
UpdateAppointment(service, appId, subject, body, date, dur, status);
}
}
return "Success";
}
/**
* Function: UpdateAppointment
* Updates an existing appointment
**/
static void UpdateAppointment(ExchangeService service, string appId, string subject, string body, DateTime date, int duration, string status)
{
Console.WriteLine("Updating Appointment: " + appId);
// Bind existing appointment to an identifier
Appointment appointment = Appointment.Bind(service, new ItemId(appId));
// Make changes depending on the validity of the inputs
if ((subject != null)&&(subject != ""))
{
appointment.Subject = subject;
}
if ((body != null)&&(body != ""))
{
appointment.Body = body;
}
if (date != DateTime.Parse("01/01/12 00:00:00"))
{
// Gets the initial duration to perserve it, if duration also not changed
TimeSpan initialDuration = appointment.Duration;
appointment.Start = date;
appointment.End = appointment.Start.Add(initialDuration);
}
if (duration > 0)
{
appointment.End = appointment.Start.AddHours(duration);
}
if ((status != null)&&(status != ""))
{
StringList categories = new Microsoft.Exchange.WebServices.Data.StringList();
categories.Add(status);
appointment.Categories = categories;
}
// Save changes
appointment.Update(ConflictResolutionMode.AlwaysOverwrite);
}
[WebMethod]
public string DeleteApp(string appId)
{
// Create the Service Binding
ExchangeService service = GetBinding();
DeleteAppointment(service, appId);
return "Success";
}
/**
* Function: DeleteAppointment
* Removes the indicated appointment
**/
static void DeleteAppointment(ExchangeService service, string appId)
{
// Bind existing appointment to an identifier
Appointment appointment = Appointment.Bind(service, new ItemId(appId));
// Delete the appointment
appointment.Delete(DeleteMode.MoveToDeletedItems);
}
}
}