-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheasyEws1.0.5.js
529 lines (492 loc) · 25.5 KB
/
easyEws1.0.5.js
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*!
* easyEWS JavaScript Library v1.0.2
* http://davecra.com
*
* Copyright David E. Craig and other contributors
* Released under the MIT license
* https://tldrlegal.com/license/mit-license
*
* Date: 2016-07-13T09:09EST
*/
var easyEws = (function () {
"use strict";
var easyEws = {};
easyEws.initialize = function () {
/// <summary>
/// PRIVATE: creates a SOAP EWS wrapper
/// </summary>
function getSoapHeader(request) {
var result =
'<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' xmlns:xsd="http://www.w3.org/2001/XMLSchema"' +
' xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"' +
' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"' +
' xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' +
' <soap:Header>' +
' <RequestServerVersion Version="Exchange2013" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" soap:mustUnderstand="0" />' +
' </soap:Header>' +
' <soap:Body>' + request + '</soap:Body>' +
'</soap:Envelope>';
return result;
};
function asyncEws(soap, successCallback, errorCallback, debugCallback) {
/// <summary>
/// PRIVATE: Makes an EWS callback with promise
/// </summary>
/// <param name="soap" type="String">XML Soap message</param>
/// <param name="successCallback" type="Function">Success callback - function(DOMDocument) { }</param>
/// <param name="errorCallback" type="Function">Error handler callback - function(Error) { }</param>
/// <param name="debugCallback" type="Function">Debug callback - function(String) { }</param>
Office.context.mailbox.makeEwsRequestAsync(soap, function (ewsResult) {
if (ewsResult.status == "succeeded") {
var xmlDoc = $.parseXML(ewsResult.value);
successCallback(xmlDoc);
debugCallback(ewsResult.value); // return raw result
} else {
if (errorCallback != null) {
errorCallback("makeEwsRequestAsync failed.");
debugCallback(ewsResult.value); // return raw result
}
}
});
};
easyEws.updateEwsHeader = function (mailItemId, headerName, headerValue,
successCallback, errorCallback, debugCallback) {
/// <summary>
/// PUBLIC: Updates the
/// headers in the mail item
/// SEE: https://msdn.microsoft.com/en-us/library/office/dn596091(v=exchg.150).aspx
/// </summary>
/// <param name="mailItemId" type="String">The id of the item to update</param>
/// <param name="headerName" type="String">The header item to add/update</param>
/// <param name="headerValue" type="String">The header value to update</param>
/// <param name="successCallback" type="Function">returns 'succeeeded' is successful - function(String) { }</param>
/// <param name="errorCallback" type="Function">Error handler callback - function(Error) { }</param>
/// <param name="debugCallback" type="Function">Debug handler returns raw XML - function(String) { }</param>
var soap =
'<m:UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AlwaysOverwrite">' +
' <m:ItemChanges>' +
' <t:ItemChange>' +
' <t:ItemId Id="' + mailItemId + '"/>' +
' <t:Updates>' +
' <t:SetItemField>' +
' <t:ExtendedFieldURI DistinguishedPropertySetId="InternetHeaders"' +
' PropertyName="' + headerName + '"' +
' PropertyType="String" />' +
' <t:Message>' +
' <t:ExtendedProperty>' +
' <t:ExtendedFieldURI DistinguishedPropertySetId="InternetHeaders"' +
' PropertyName="' + headerName + '"' +
' PropertyType="String" />' +
' <t:Value>' + headerValue + '</t:Value>' +
' </t:ExtendedProperty>' +
' </t:Message>' +
' </t:SetItemField>' +
' </t:Updates>' +
' </t:ItemChange>' +
' </m:ItemChanges>' +
'</m:UpdateItem>';
soap = getSoapHeader(soap);
// make the EWS call
asyncEws(soap, function (xmlDoc) {
if (successCallback)
successCallback("succeeded");
}, function (errorDetails) {
if (errorCallback != null)
errorCallback(errorDetails);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
};
easyEws.getFolderItemIds = function (folderId, successCallback, errorCallback, debugCallback) {
/// <summary>
/// PUBLIC: Returns a list of items in the folder
/// </summary>
/// <param name="folderId" type="String">The ID of the folder you want to search</param>
/// <param name="successCallback" type="Function">Callback with array of item IDs - function(String[]) { }</param>
/// <param name="errorCallback" type="Function">Error handler callback - function(Error) { }</param>
/// <param name="debugCallback" type="Function">Debug handler returns raw XML - function(String) { }</param>
var soap =
'<m:FindItem Traversal="Shallow">' +
' <m:ItemShape> ' +
' <t:BaseShape>IdOnly</t:BaseShape>' +
' </m:ItemShape>' +
' <m:ParentFolderIds>' +
' <t:FolderId Id="' + folderId + '"/>' +
' </m:ParentFolderIds>' +
'</m:FindItem>';
var returnArray = [];
soap = getSoapHeader(soap);
// call ews
asyncEws(soap, function (xmlDoc) {
var nodes = xmlDoc.getElementsByTagName("t:ItemId");
if (nodes == null || nodes.length == 0) {
nodes = xmlDoc.getElementsByTagName("ItemId");
}
$.each(nodes, function (index, value) {
returnArray.push(value.getAttribute("Id"));
});
successCallback(returnArray);
}, function (errorDetails) {
if (errorCallback != null) {
errorCallback(errorDetails);
}
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
}
easyEws.getMailItem = function (ItemId, successCallback, errorCallback, debugCallback) {
/// <summary>
/// PUBLIC: Gets the item details for a specific item by ID
/// </summary>
/// <param name="ItemId" type="String">The ID for the item</param>
/// <param name="successCallback" type="Function">Callback with the details of the MailItem - function(MailItem) { }</param>
/// <param name="errorCallback" type="Function">Error handler callback - function(Error) { }</param>
/// <param name="debugCallback" type="Function">Debug handler returns raw XML - function(String) { }</param>
var soap =
'<m:GetItem>' +
' <m:ItemShape>' +
' <t:BaseShape>Default</t:BaseShape>' +
' <t:IncludeMimeContent>true</t:IncludeMimeContent>' +
' </m:ItemShape>' +
' <m:ItemIds>' +
' <t:ItemId Id="' + ItemId + '" />' +
' </m:ItemIds>' +
'</m:GetItem>';
soap = getSoapHeader(soap);
// make call to EWS
asyncEws(soap, function (xmlDoc) {
var item = new MailItem(xmlDoc);
successCallback(item);
}, function (errorDetails) {
if(errorCallback != null) {
errorCallback(errorDetails);
}
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
}
easyEws.expandGroup = function (group, successCallback, errorCallback, debugCallback) {
/// <summary>
/// PUBLIC: Expands a group and returns all the members
/// NOTE: Does not enumerate groups in groups
/// </summary>
/// <param name="group" type="String">The alias for the group to be expanded</param>
/// <param name="successCallback" type="Function">Callback with an array of aliases - function(String[]) { }</param>
/// <param name="errorCallback" type="Function">Error handler callback - function(Error) { }</param>
/// <param name="debugCallback" type="Function">Debug handler returns raw XML - function(String) { }</param>
var soap =
'<m:ExpandDL>' +
' <m:Mailbox>' +
' <t:EmailAddress>" + group + "</t:EmailAddress>' +
' </m:Mailbox>' +
'</m:ExpandDL>';
soap = getSoapHeader(soap);
// make the EWS call
var returnArray = [];
asyncEws(soap, function (xmlDoc) {
var extendedProps = xmlDoc.getElementsByTagName("t:EmailAddress");
if (extendedProps == null || extendedProps.length == 0) {
extendedProps = xmlDoc.getElementsByTagName("EmailAddress");
}
$.each(extendedProps, function (index, value) {
returnArray.push(value);
});
successCallback(returnArray);
}, function (errorDetails) {
if (errorCallback != null)
errorCallback(errorDetails);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
};
easyEws.findConversationItems = function (conversationId, successCallback, errorCallback, debugCallback) {
/// <summary>
/// PUBLIC: Find a given conversation by the ID
/// NOTE: Search for parent:
/// http://stackoverflow.com/questions/19008696/exchange-find-items-in-ews-conversation-using-xml-request
/// http://www.outlookcode.com/codedetail.aspx?id=1714
/// https://msdn.microsoft.com/en-us/library/office/dn610351(v=exchg.150).aspx
/// </summary>
/// <param name="conversationId" type="String">The conversation to find</param>
/// <param name="successCallback" type="Function">Callback with an array of ids - function(String[]) { }</param>
/// <param name="errorCallback" type="Function">Error handler callback - function(Error) { }</param>
/// <param name="debugCallback" type="Function">Debug handler returns raw XML - function(String) { }</param>
var soap =
' <m:GetConversationItems>' +
' <m:ItemShape>' +
' <t:BaseShape>IdOnly</t:BaseShape>' +
' <t:AdditionalProperties>' +
' <t:FieldURI FieldURI="item:Subject" />' +
' <t:FieldURI FieldURI="item:DateTimeReceived" />' +
' </t:AdditionalProperties>' +
' </m:ItemShape>' +
' <m:FoldersToIgnore>' +
' <t:DistinguishedFolderId Id="deleteditems" />' +
' <t:DistinguishedFolderId Id="drafts" />' +
' </m:FoldersToIgnore>' +
' <m:SortOrder>TreeOrderDescending</m:SortOrder>' +
' <m:Conversations>' +
' <t:Conversation>' +
' <t:ConversationId Id="' + conversationId + '" />' +
' </t:Conversation>' +
' </m:Conversations>' +
' </m:GetConversationItems>';
soap = getSoapHeader(soap);
// Make EWS call
asyncEws(soap, function (xmlDoc) {
var returnArray = [];
try {
var xmlNodes = null;
if (xmlDoc == null) {
if (errorCallback != null) {
errorCallback(new Error("The XML returned from the server could not be parsed."));
}
} else if (xmlDoc.getElementsByTagName("t:ItemId").length == 0 &&
xmlDoc.getElementsByTagName("ItemId").length == 0) {
if (errorCallback != null) {
errorCallback(new Error("The XML returned does not contain results."));
}
} else {
var nodes = xmlDoc.getElementsByTagName("t:ItemId");
if (nodes == null || nodes.length == 0) {
nodes = xmlDoc.getElementsByTagName("ItemId");
}
$.each(nodes, function (index, value) {
returnArray.push(value.getAttribute("Id"));
});
successCallback(returnArray);
}
} catch (error) {
if (errorCallback != null)
errorCallback(error);
}
}, function (errorDetails) {
if (errorCallback != null)
errorCallback(errorDetails);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
};
easyEws.getEwsHeaders = function (itemId, successCallback, errorCallback, debugCallback) {
/// <summary>
/// PUBLIC: Gets Internet headers for a spific item
/// NOTE: https://msdn.microsoft.com/en-us/library/office/aa566013(v=exchg.150).aspx
/// </summary>
/// <param name="itemId" type="String">The item ID to get</param>
/// <param name="successCallback" type="Function">Callback with a Dictionary(key,value) containing the message headers - function(Dictionary) { }</param>
/// <param name="errorCallback" type="Function">Error handler callback - function(Error) { }</param>
/// <param name="debugCallback" type="Function">Debug handler returns raw XML - function(String) { }</param>
var soap =
' <m:GetItem>' +
' <m:ItemShape>' +
' <t:BaseShape>AllProperties</t:BaseShape>' +
' <t:IncludeMimeContent>true</t:IncludeMimeContent>' +
' </m:ItemShape>' +
' <m:ItemIds>' +
' <t:ItemId Id="' + itemId + '" />' +
' </m:ItemIds>' +
' </m:GetItem>';
soap = getSoapHeader(soap);
// Make the EWS call
var returnArray = new Dictionary();
asyncEws(soap, function (xmlDoc) {
try {
var nodes = xmlDoc.getElementsByTagName("t:InternetMessageHeader");
if (nodes == null || nodes.length == 0) {
nodes = xmlDoc.getElementsByTagName("InternetMessageHeader");
}
$.each(nodes, function (index, value) {
returnArray.add(value.getAttribute("HeaderName"), value.textContent);
});
successCallback(returnArray);
} catch (error) {
if (errorCallback != null)
errorCallback(error);
}
}, function (errorDetails) {
if (errorCallback != null)
errorCallback(errorDetails);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
};
easyEws.updateFolderProperty = function (folderId, propName, propValue, successCallback, errorCallback, debugCallback) {
/// <summary>
/// PUBLIC: Updates a folder property. If the property does not exist, it will be created.
/// </summary>
/// <param name="folderId" type="String">The ID for the folder</param>
/// <param name="propName" type="String">The property on the folder to set</param>
/// <param name="propValue" type="String">The value for the property</param>
/// <param name="successCallback" type="Function">Callback with the string 'succeeeded' if successful - function(String) { }</param>
/// <param name="errorCallback" type="Function">Error handler callback - function(Error) { }</param>
/// <param name="debugCallback" type="Function">Debug handler returns raw XML - function(String) { }</param>
var soap =
' <m:UpdateFolder>' +
' <m:FolderChanges>' +
' <t:FolderChange>' +
' <t:FolderId Id="' + folderId + '" />' +
' <t:Updates>' +
' <t:SetFolderField>' +
' <t:ExtendedFieldURI ' +
' DistinguishedPropertySetId="PublicStrings" ' +
' PropertyName="' + propName + '" ' +
' PropertyType="String" />' +
' <t:Folder>' +
' <t:ExtendedProperty>' +
' <t:ExtendedFieldURI ' +
' DistinguishedPropertySetId="PublicStrings" ' +
' PropertyName="' + propName + '" ' +
' PropertyType="String" />' +
' <t:Value>' + propValue + '</t:Value>' +
' </t:ExtendedProperty>' +
' </t:Folder>' +
' </t:SetFolderField>' +
' </t:Updates>' +
' </t:FolderChange>' +
' </m:FolderChanges>' +
' </m:UpdateFolder>';
soap = getSoapHeader(soap);
// make the EWS call
asyncEws(soap, function(data) {
if(successCallback != null)
successCallback('succeeeded');
}, function (error) {
if (errorCallback != null)
errorCallback(error);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
}
//
// RETURNS: property value if process completed successfully
easyEws.getFolderProperty = function (folderId, propName, successCallback, errorCallback, debugCallback) {
/// <summary>
/// PUBLIC: Gets a folder property
/// </summary>
/// <param name="folderId" type="String">The ID for the folder</param>
/// <param name="propName" type="String">The property to get</param>
/// <param name="successCallback" type="Function">Callback with the string value of the property - function(String) { }</param>
/// <param name="errorCallback" type="Function">Error handler callback - function(Error) { }</param>
/// <param name="debugCallback" type="Function">Debug handler returns raw XML - function(String) { }</param>
var soap =
'<m:GetFolder>' +
'<m:FolderShape>' +
'<t:BaseShape>IdOnly</t:BaseShape>' +
'<t:AdditionalProperties>' +
'<t:ExtendedFieldURI ' +
' DistinguishedPropertySetId="PublicStrings" ' +
' PropertyName="' + propName + '" ' +
' PropertyType="String" />' +
'</t:AdditionalProperties>' +
'</m:FolderShape>' +
'<m:FolderIds>' +
'<t:FolderId Id="' + folderId + '"/>' +
'</m:FolderIds>' +
'</m:GetFolder>';
soap = getSoapHeader(soap);
// make the EWS call
asyncEws(soap, function (xmlDoc) {
var nodes = xmlDoc.getElementsByTagName("t:Value");
if (nodes == null || nodes.length == 0) {
nodes = xmlDoc.getElementsByTagName("Value");
}
successCallback(nodes[0].textContent);
}, function (error) {
if (errorCallback != null)
errorCallback(error);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
}
easyEws.getFolderId = function (folderName, successCallback, errorCallback, debugCallback) {
/// <summary>
/// PUBLIC: Gets the folder id by the given name from the store
/// </summary>
/// <param name="folderName" type="String">Name of the folder to get the ID for</param>
/// <param name="successCallback" type="Function">Callback with the folder ID - function(String) { }</param>
/// <param name="errorCallback" type="Function">Error handler callback - function(Error) { }</param>
/// <param name="debugCallback" type="Function">Debug handler returns raw XML - function(String) { }</param>
var soap =
' <m:GetFolder>' +
' <m:FolderShape>' +
' <t:BaseShape>IdOnly</t:BaseShape>' +
' </m:FolderShape>' +
' <m:FolderIds>' +
' <t:DistinguishedFolderId Id="' + folderName + '" />' +
' </m:FolderIds>' +
' </m:GetFolder>';
soap = getSoapHeader(soap);
// make EWS callback
asyncEws(soap, function (xmlDoc) {
var nodes = xmlDoc.getElementsByTagName("t:FolderId");
if (nodes == null || nodes.length == 0) {
nodes = xmlDoc.getElementsByTagName("FolderId");
}
var id = nodes[0].getAttribute("Id");
successCallback(id);
}, function (errorDetails) {
if (errorCallback != null)
errorCallback(errorDetails);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
}
}
return easyEws;
})();
////////////////////////////////////////////////
////////////////////////////////////////////////
easyEws.initialize(); // initialize the class
////////////////////////////////////////////////
////////////////////////////////////////////////
/* HELPER FUNCTIONS AND CLASSES */
function MailItem(value) {
this.value = value || {};
MailItem.prototype.MimeContent = function () {
return this.value.getElementsByTagName("t:MimeContent")[0].textContent;
};
MailItem.prototype.MimeContent.CharacterSet = function () {
return this.value.getElementsByTagName("t:MimeContent")[0].getAttribute("CharacterSet");
};
MailItem.prototype.Subject = function () {
return this.value.getElementsByTagName("t:Subject")[0].textContent;
};
}
function Dictionary(values) {
this.values = values || {};
var forEachIn = function (object, action) {
for (var property in object) {
if (Object.prototype.hasOwnProperty.call(object, property))
action(property, object[property]);
}
};
Dictionary.prototype.containsKey = function (key) {
return Object.prototype.hasOwnProperty.call(this.values, key) &&
Object.prototype.propertyIsEnumerable.call(this.values, key);
};
Dictionary.prototype.forEach = function (action) {
forEachIn(this.values, action);
};
Dictionary.prototype.lookup = function (key) {
return this.values[key];
};
Dictionary.prototype.add = function (key, value) {
this.values[key] = value;
};
Dictionary.prototype.length = function () {
var len = 0;
forEachIn(this.values, function () { len++ });
return len;
};
};