-
Notifications
You must be signed in to change notification settings - Fork 4
/
easyEws1.0.19.js
1478 lines (1444 loc) · 68.8 KB
/
easyEws1.0.19.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* easyEWS JavaScript Library v1.0.19
* http://theofficecontext.com
*
* Copyright David E. Craig and other contributors
* Portions Copyright (c) 2020 Vijay Samtani
* Released under the MIT license
* https://tldrlegal.com/license/mit-license
*
* Date: 2020-08-13T15:10EST
*/
/**
* The global easyEws object
* @type {__nonInstanceEasyEwsClass}
* */
var easyEws = new __nonInstanceEasyEwsClass();
/**
* @class
*/
function __nonInstanceEasyEwsClass() {
/** @type {string[]} */
var groups = [];
/** @type {string[]} */
var processedGroups = [];
/** @type {MailBoxUser[]} */
var users = [];
/** @type {successCallbackMailboxUserArray} */
var splitGroupSuccessCallback;
/** @type {errorCallback} */
var splitGroupErrorCallback;
/** @type {debugCallback} */
var splitGroupDebugCallback;
/** @type {number} */
var groupCount = 0;
/***********************************************************************************************
***********************************************************************************************
***********************************************************************************************
**** * * **** * ***** ***
* * * * * * * * * *
**** * * **** * * *
* * * * * * * * *
* *** **** ***** ***** ***
***********************************************************************************************
***********************************************************************************************
***********************************************************************************************/
/**
* This function finds the Parent message of a reply/forward by using the InReplyTo
* field to find the InternetMessageId of the parent.
* @param {string} childId The id of the item whose parent if you want to find
* @param {successCallback} successCallback Returns a string with the parent id or null if there is no parent found
* @param {errorCallback} errorCallback Error handler callback - function(Error) { }
* @param {debugCallback} debugCallback Debug handler returns raw XML - function(String) { }
*/
this.getParentId = function (childId, successCallback, errorCallback, debugCallback) {
/** @type {string} */
var soap =
'<m:GetItem>' +
' <m:ItemShape>' +
' <t:BaseShape>AllProperties</t:BaseShape>' +
' </m:ItemShape>' +
' <m:ItemIds>' +
' <t:ItemId Id="' + childId + '"/>' +
' </m:ItemIds>' +
'</m:GetItem>';
soap = getSoapHeader(soap);
// make the EWS call
asyncEws(soap, function (getItemXmlDoc) {
/**@type {string} */
var internetMessageId = "";
try {
internetMessageId = getNodes(getItemXmlDoc, "t:InReplyTo")[0].textContent;
} catch(error) {
// returns null if there is no InReplyTo field which likely means
// that this item is not a reply or forward but a new item
successCallback(null);
return;
}
// fix the message id so it will not error
internetMessageId = internetMessageId.replace("<", "<").replace(">", ">");
soap =
'<m:FindItem Traversal="Shallow">' +
' <m:ItemShape>' +
' <t:BaseShape>IdOnly</t:BaseShape>' +
' </m:ItemShape>' +
' <m:IndexedPageItemView MaxEntriesReturned="50" Offset="0" BasePoint="Beginning" />' +
' <m:Restriction>' +
' <t:Contains ContainmentMode="Substring" ContainmentComparison="IgnoreCase">' +
' <t:FieldURI FieldURI="message:InternetMessageId" />' +
' <t:Constant Value="' + internetMessageId + '" />' +
' </t:Contains>' +
' </m:Restriction>' +
' <m:ParentFolderIds>' +
' <t:DistinguishedFolderId Id="inbox" />' +
' </m:ParentFolderIds>' +
'</m:FindItem>';
soap = getSoapHeader(soap);
asyncEws(soap, function (findItemXmlDoc) {
var parentId = getNodes(findItemXmlDoc, "t:ItemId")[0].attributes["Id"].value;
if (successCallback) successCallback(parentId);
}, function (error) {
if (errorCallback) errorCallback(error);
}, function (debug) {
if (debugCallback) debugCallback(debug);
});
}, function (error) {
if (errorCallback) errorCallback(error);
}, function (debug) {
if (debugCallback) debugCallback(debug);
});
};
/**
* This callback type is called 'resolveNamesCallback' and is displayed as a global symbol.
*
* @callback resolveNamesCallback
* @param {ResolveNamesType[]} resolvedNamesArray
*/
/**
* PUBLIC: Resolves the recipient
* SEE: https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/resolvenames-operation
*
* @param {string} recipient - The recipient name
* @param {resolveNamesCallback} [successCallback] - returns 'succeeeded' is successful - function(ResolveNamesType) { }
* @param {errorCallback} [errorCallback] - Error handler callback - function(Error) { }
* @param {debugCallback} [debugCallback] - Debug handler returns raw XML - function(String) { }
*/
this.resolveRecipient = function (recipient, successCallback, errorCallback, debugCallback) {
var soap = '<ResolveNames xmlns="http://schemas.microsoft.com/exchange/services/2006/messages" '+
'xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" ' +
'ReturnFullContactData="true">' +
'<UnresolvedEntry>' + recipient + '</UnresolvedEntry>' +
'</ResolveNames>';
soap = getSoapHeader(soap);
// make the EWS call
asyncEws(soap, function (xmlDoc) {
if (successCallback) {
var message = getNodes(xmlDoc, "m:MessageText");
if(message != null && message.length > 0 &&
message[0].innerHTML == "No results were found.") {
successCallback(null);
} else {
var returnArray = [];
/**@type {XMLNode[]} */
var nodes = getNodes(xmlDoc,"t:Mailbox");
// this returns an array like this:
// <t:Mailbox>
// <t:Name>David Craig</t:Name>
// <t:EmailAddress>[email protected]</t:EmailAddress>
// <t:RoutingType>SMTP</t:RoutingType>
// <t:MailboxType>Contact</t:MailboxType>
// </t:Mailbox>
for(var idx = 0; idx < nodes.length; idx++) {
/**@type {XMLNode} */
var value = nodes[idx];
var name = getNodes(value, "t:Name")[0].textContent;
var email = getNodes(value, "t:EmailAddress")[0].textContent;
var route = getNodes(value, "t:RoutingType")[0].textContent;
var box = getNodes(value, "t:MailboxType")[0].textContent;
var obj = new ResolveNamesType(name, email, route, box);
returnArray.push(obj);
}
// $.each(nodes, function (index, value) {
// var name = getNodes(value, "t:Name")[0].textContent;
// var email = getNodes(value, "t:EmailAddress")[0].textContent;
// var route = getNodes(value, "t:RoutingType")[0].textContent;
// var box = getNodes(value, "t:MailboxType")[0].textContent;
// var obj = new ResolveNamesType(name, email, route, box);
// returnArray.push(obj);
// });
successCallback(returnArray);
}
}
}, function (errorDetails) {
if (errorCallback != null)
errorCallback(errorDetails);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
};
/**
* Class to hold resolved names from the ResolveNames function
* @class
* @param {string} name
* @param {string} emailAddress
* @param {string} routingType
* @param {string} mailboxType
* @property {string} name
* @property {string} emailAddress
* @property {string} routingType
* @property {string} mailboxType
*/
function ResolveNamesType(name, emailAddress, routingType, mailboxType) {
this.name = name;
this.emailAddress = emailAddress;
this.routiingType = routingType;
this.mailboxType = mailboxType;
}
/**
* PUBLIC: creates and sends a new email message with 1+ recipients, 0+ attachments. Can specify where to save copy.
*
* @param {Object} p - The parameters object
* @param {string} p.body - The body of the message to be sent
* @param {string[]} p.recipients[] - Email address(es) of the recipient(s) as an Array of strings eg ["[email protected]", "[email protected]"]
* @param {Object[]} p.attachments - array of objects of form {name: string, mime: BASE64 string} to be attached. Pass [{}] if no attachments.
* @param {string} p.attachments[].name - Name of the attachment
* @param {string} p.attachments[].mime - MIME content in Base64 for the attachment
* @param {string} p.folderid - distinguished folder id of folder to put the sent item in
* @param {successCallback} p.successCallback - Callback with 'success' if compelted successfully - function(string) { }
* @param {errorCallback} p.rrorCallback - Error handler callback - function(string) { }
* @param {debugCallback} p.debugCallback - Debug handler returns raw XML - function(string) { }
*/
this.sendMailItem = function({subject, body, recipients = [""], attachments = [{name: "", mime:""}], folderid = "sentitems", successCallback, errorCallback, debugCallback}) {
//construct recipients
var xmlRecipients = "";
recipients.forEach( function(address) {
xmlRecipients += '<t:Mailbox><t:EmailAddress>' + address + '</t:EmailAddress></t:Mailbox>';
});
xmlRecipients = '<t:ToRecipients>' + xmlRecipients + '</t:ToRecipients>';
// construct attachments
var xmlAttachments = "";
attachments.forEach( function (attachment) {
// Check if it's an empty object (ie, nothing to attach)
if (Object.keys(attachment).length != 0) {
xmlAttachments += '<t:ItemAttachment>' +
'<t:Name>' + attachment.name + '</t:Name>' +
'<t:IsInline>false</t:IsInline>' +
'<t:Message>' +
'<t:MimeContent CharacterSet="UTF-8">' + attachment.mime + '</t:MimeContent>' +
'</t:Message>' +
'</t:ItemAttachment>';
};
});
if (xmlAttachments != "") xmlAttachments = '<t:Attachments>' + xmlAttachments + '</t:Attachments>';
// construct folder spec to save the sent email in
var xmlSavedFolder = '<m:SavedItemFolderId><t:DistinguishedFolderId Id="' + folderid + '" /></m:SavedItemFolderId>';
// assemble the soap request
var soap = '<m:CreateItem MessageDisposition="SendAndSaveCopy">' +
xmlSavedFolder +
' <m:Items>' +
' <t:Message>' +
' <t:Subject>' + subject + '</t:Subject>' +
' <t:Body BodyType="Text">' + body + '</t:Body>' +
xmlAttachments +
xmlRecipients +
' </t:Message>' +
' </m:Items>' +
'</m:CreateItem>';
soap = getSoapHeader(soap);
// make the EWS call
asyncEws(soap, function (xmlDoc) {
// Get the required response, and if it's NoError then all has succeeded, so tell the user.
// Otherwise, tell them what the problem was. (E.G. Recipient email addresses might have been
// entered incorrectly --- try it and see for yourself what happens!!)
/** @type {string} */
var elem = xmlDoc.getElementsByTagName("m:ResponseCode")[0];
if(elem == null) elem = xmlDoc.getElementsByTagName("ResponseCode")[0];
var result = elem.textContent;
if (result == "NoError") {
successCallback(result);
}
else {
if (errorCallback != null)
errorCallback(result);
}
}, function (errorDetails) {
if (errorCallback != null)
errorCallback(errorDetails);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
};
/**
* PUBLIC: creates a new emails message with a single attachment and sends it
*
* @param {string} subject - The subject for the message to be sent
* @param {string} body - The body of the message to be sent
* @param {string} to - The email address of the recipient
* @param {string} attachmentName - Name of the attachment
* @param {string} attachmentMime - MIME content in Base64 for the attachment
* @param {successCallback} successCallback - Callback with 'success' if compelted successfully - function(string) { }
* @param {errorCallback} errorCallback - Error handler callback - function(string) { }
* @param {debugCallback} debugCallback - Debug handler returns raw XML - function(string) { }
*/
this.sendPlainTextEmailWithAttachment = function (subject, body, to, attachmentName, attachmentMime, successCallback, errorCallback, debugCallback) {
/** @type {string} */
var toArray = new Array(to);
var attachmentObject = new Object({name: attachmentName, mime: attachmentMime});
var attachmentArray = new Array(attachmentObject);
this.sendMailItem(new Object({subject: subject,
body: body,
recipients: toArray,
attachments: attachmentArray,
folderid: "sentitems",
successCallback: successCallback,
errorCallback: errorCallback,
debugCallback: debugCallback}));
};
/**
* PUBLIC: gets the mail item as raw MIME data
*
* @param {string} mailItemId - The id for the item
* @param {successCallback} successCallback - Callback with email message as MIME Base64 string - function(string) { }
* @param {errorCallback} errorCallback - Error handler callback - function(Error) { }
* @param {debugCallback} debugCallback - Debug handler returns raw XML - function(String) { }
*/
this.getMailItemMimeContent = function (mailItemId, successCallback, errorCallback, debugCallback) {
/** @type {string} */
var soap =
'<m:GetItem>' +
' <m:ItemShape>' +
' <t:BaseShape>IdOnly</t:BaseShape>' +
' <t:IncludeMimeContent>true</t:IncludeMimeContent>' +
' </m:ItemShape>' +
' <m:ItemIds>' +
' <t:ItemId Id="' + mailItemId + '"/>' +
' </m:ItemIds>' +
'</m:GetItem>';
soap = getSoapHeader(soap);
// make the EWS call
asyncEws(soap, function (xmlDoc) {
/** @type {array} */
var nodes = getNodes(xmlDoc,"t:MimeContent");
/** @type {string} */
var content = nodes[0].textContent;
successCallback(content);
}, function (errorDetails) {
if (errorCallback != null)
errorCallback(errorDetails);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
};
/**
* PUBLIC: Updates the headers in the mail item
* SEE: https://msdn.microsoft.com/en-us/library/office/dn596091(v=exchg.150).aspx
* SEE: https://msdn.microsoft.com/en-us/library/office/dn495610(v=exchg.150).aspx
*
* @param {string} mailItemId - The id of the item to update
* @param {string} headerName - The header item to add/update
* @param {string} headerValue - The header value to update
* @param {boolean} [isMeeting] - is required to be true for meeting requests
* @param {successCallback} [successCallback] - returns 'succeeeded' is successful - function(String) { }
* @param {errorCallback} [errorCallback] - Error handler callback - function(Error) { }
* @param {debugCallback} [debugCallback] - Debug handler returns raw XML - function(String) { }
*/
this.updateEwsHeader = function (mailItemId, headerName, headerValue, isMeeting,
successCallback, errorCallback, debugCallback) {
/** @type {string} */
var firstLine = '<m:UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AlwaysOverwrite">';
if(isMeeting){
firstLine = '<m:UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AlwaysOverwrite" SendMeetingInvitationsOrCancellations="SendOnlyToChanged">';
}
/** @type {string} */
var soap =
firstLine +
' <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);
});
};
/**
* PUBLIC: Returns a list of items in the folder
*
* @param {string} folderId - The ID of the folder you want to search
* @param {successCallbackArray} successCallback - Callback with array of item IDs - function(String[]) { }
* @param {errorCallback} errorCallback - Error handler callback - function(Error) { }
* @param {errorCallback} errorCallback - Debug handler returns raw XML - function(String) { }
*/
this.getFolderItemIds = function (folderId, successCallback, errorCallback, debugCallback) {
/** @type {string} */
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>';
/** @type {array} */
var returnArray = [];
soap = getSoapHeader(soap);
// call ews
asyncEws(soap, function (xmlDoc) {
/** @type {XMLNode} */
var nodes = getNodes(xmlDoc, "t:ItemId");
// loop through and return an array of ids
for(var idx=0;idx<nodes.length;idx++) {
returnArray.push(nodes[idx].getAttribute("Id"));
}
// $.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);
});
};
/**
* PUBLIC: Gets the item details for a specific item by ID
*
* @param {string} itemId The ID for the item
* @param {successCallbackMailItem} successCallback - Callback with the details of the MailItem - function(MailItem) { }
* @param {errorCallback} errorCallback - Error handler callback - function(Error) { }
* @param {debugCallback} debugCallback - Debug handler returns raw XML - function(String) { }
*/
this.getMailItem = function (itemId, successCallback, errorCallback, debugCallback) {
/** @type {string} */
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) {
/** @type {MailItem} */
var item = new MailItem(xmlDoc);
successCallback(item);
}, function (errorDetails) {
if(errorCallback != null) {
errorCallback(errorDetails);
}
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
};
/**
* PUBLIC: Splits groups and groups in groups, async, until all users are
* found and added to the list. The list of MailBoxUsers[] is
* returned as a paramater of the success callback.
* NOTE: For performance reasons, this will STOP splitting groups after 10
* levels deeps. If groups nesting is greater than this it is bad design
* on the Exchange organization
* @param {string[]} groupList The alias for the group(s) to be split
* @param {successCallbackMailboxUserArray} sucessCallback Callback with array of MailBoxUsers - function(MailBoxUser[]) { }
* @param {errorCallback} errorCallback - Error handler callback - function(Error) { }
* @param {debugCallback} debugCallback - Debug handler returns raw XML - function(String) { }
*/
this.splitGroupsAsync = function(groupList, successCallback, errorCallback, debugCallback) {
// cleanup - reset arrays
groups = [];
users = [];
processedGroups = [];
groupCount = 0;
// add the current groups
groups.push(groupList);
// set callbacks
splitGroupSuccessCallback = successCallback;
splitGroupErrorCallback = errorCallback;
splitGroupDebugCallback = debugCallback;
// now start...
splitGroupsRecursivelyAsync();
};
/**
* PUBLIC: Expands a group and returns all the members
* NOTE: Does not enumerate groups in groups
*
* @param {string} group The alias for the group to be expanded
* @param {successCallbackMailboxUserArray} successCallback - Callback with array of MailBoxUsers - function(MailBoxUser[]) { }
* @param {errorCallback} errorCallback - Error handler callback - function(Error) { }
* @param {errorCallback} errorCallback - Debug handler returns raw XML - function(String) { }
*/
this.expandGroup = function (group, successCallback, errorCallback, debugCallback) {
// NOTE: Some groups might have an unsafe character like &
// So, here we first make sure there is not a mix first by
// converting already safe to unsafe, then converting everything
// to a safe format.
// NOTE: Confirmed in O365 - unable to create groups with &
// NOTE: Confirmed in EX2106/2019 - able to create groups with &
// Microsoft Offical documentation on the matter:
// https://docs.microsoft.com/en-us/office365/troubleshoot/active-directory/email-address-contain-underscore
// NOTE: Fixed per issue #8: Expanding groups with '&' ampersand in the group
/** @type {string} */
var safeGroup = group.replace('&', '&').replace('&', '&');
/** @type {string} */
var soap =
'<m:ExpandDL>' +
' <m:Mailbox>' +
' <t:EmailAddress>' + safeGroup + '</t:EmailAddress>' +
' </m:Mailbox>' +
'</m:ExpandDL>';
soap = getSoapHeader(soap);
// make the EWS call
/** @type {array} */
var returnArray = [];
asyncEws(soap, function (xmlDoc) {
/** @type {array} */
var extendedProps = getNodes(xmlDoc, "t:Mailbox");
// loop through and return an array of properties
for(var idx=0; idx<extendedProps.length; idx++) {
returnArray.push(new MailboxUser(extendedProps[idx]));
}
// $.each(extendedProps, function (index, value) {
// returnArray.push(new MailboxUser(value));
// });
successCallback(returnArray);
}, function (errorDetails) {
if (errorCallback != null)
errorCallback(errorDetails);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
};
/**
* 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
*
* @param {string} converstionId - The conversation to find
* @param {successCallbackArray} successCallback - Callback with array of item IDs - function(String[]) { }
* @param {errorCallback} errorCallback - Error handler callback - function(Error) { }
* @param {errorCallback} errorCallback - Debug handler returns raw XML - function(String) { }
*/
this.findConversationItems = function (conversationId, successCallback, errorCallback, debugCallback) {
/** @type {string} */
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) {
/** @type {array} */
var returnArray = [];
try {
/** @type {array} */
var nodes = getNodes(xmlDoc, "t:ItemId");
if (nodes == null) {
if (errorCallback != null) {
errorCallback(new Error("The XML returned from the server could not be parsed."));
}
} else if (nodes.length == 0) {
successCallback(null);
} else {
// loop through and return an array of ids
for(var idx=0; idx < nodes.length; idx++) {
returnArray.push(nodes[idx].getAttribute("Id"));
}
// $.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);
});
};
/**
* PUBLIC: Gets a specific Internet header for a spific item
* NOTE: https://msdn.microsoft.com/en-us/library/office/aa566013(v=exchg.150).aspx
*
* @param {string} itemId - The item ID to get
* @param {string} headerName - The header to get
* @param {string} headerType - The header type (String, Integer)
* @param {successCallback} successCallback - Returns the value for the header - function(string) { }
* @param {errorCallback} errorCallback - Error handler callback - function(Error) { }
* @param {errorCallback} errorCallback - Debug handler returns raw XML - function(String) { }
*/
this.getSpecificHeader = function (itemId, headerName, headerType, successCallback, errorCallback, debugCallback) {
/** @type {string} */
var soap =
' <m:GetItem>' +
' <m:ItemShape>' +
' <t:BaseShape>IdOnly</t:BaseShape>' +
' <t:AdditionalProperties>' +
' <t:ExtendedFieldURI DistinguishedPropertySetId="InternetHeaders" PropertyName="' + headerName + '" PropertyType="' + headerType + '" />' +
' </t:AdditionalProperties>' +
' </m:ItemShape>' +
' <m:ItemIds>' +
' <t:ItemId Id="' + itemId + '" />' +
' </m:ItemIds>' +
' </m:GetItem>';
soap = getSoapHeader(soap);
// Make the EWS call
/** @type {string} */
var returnValue = "";
asyncEws(soap, function (xmlDoc) {
try {
if (xmlDoc == null) {
successCallback(null);
return;
}
/** @type {array} */
var nodes = getNodes(xmlDoc, "t:ExtendedProperty");
for(var idx=0;idx < nodes.length; idx++) {
/**@type {XMLNode} */
var value = nodes[idx];
/** @type {string} */
var nodeName = getNodes(value, "t:ExtendedFieldURI")[0].getAttribute("PropertyName");
/** @type {string} */
var nodeValue = getNodes(value, "t:Value")[0].textContent;
if (nodeName == headerName) {
returnValue = nodeValue;
}
}
// $.each(nodes, function (index, value) {
// /** @type {string} */
// var nodeName = getNodes(value, "t:ExtendedFieldURI")[0].getAttribute("PropertyName");
// /** @type {string} */
// var nodeValue = getNodes(value, "t:Value")[0].textContent;
// if (nodeName == headerName) {
// returnValue = nodeValue;
// }
// });
successCallback(returnValue);
} catch (error) {
if (errorCallback != null)
errorCallback(error);
}
}, function (errorDetails) {
if (errorCallback != null)
errorCallback(errorDetails);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
};
/**
* PUBLIC: Gets Internet headers for a spific item
* NOTE: https://msdn.microsoft.com/en-us/library/office/aa566013(v=exchg.150).aspx
*
* @param {string} itemId - The item ID to get
* @param {successCallbackDictionary} successCallback - Callback with a Dictionary(key,value) containing the message headers - function(Dictionary) { }
* @param {errorCallback} errorCallback - Error handler callback - function(Error) { }
* @param {errorCallback} errorCallback - Debug handler returns raw XML - function(String) { }
*/
this.getEwsHeaders = function (itemId, successCallback, errorCallback, debugCallback) {
/** @type {string} */
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
/** @type {Dictionary} */
var returnArray = new Dictionary();
asyncEws(soap, function (xmlDoc) {
try {
if (xmlDoc == null) {
successCallback(null);
return;
}
/** @type {array} */
var nodes = getNodes(xmlDoc, "t:InternetMessageHeader");
if(nodes !== null && nodes != undefined) {
for(var i=0;i<nodes.length;i++) {
returnArray.add(nodes[i].getAttribute("HeaderName"), nodes[i].textContent);
}
successCallback(returnArray);
} else {
successCallback(null); // no headers found
}
} catch (error) {
if (errorCallback != null)
errorCallback(error);
}
}, function (errorDetails) {
if (errorCallback != null)
errorCallback(errorDetails);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
};
/**
* PUBLIC: Updates a folder property. If the property does not exist, it will be created.
*
* @param {string} folderId - The ID for the folder
* @param {string} propName - The property on the folder to set
* @param {string} propValue - The value for the propert
* @param {successCallback} successCallback - returns 'succeeeded' is successful - function(String) { }
* @param {errorCallback} errorCallback - Error handler callback - function(Error) { }
* @param {debugCallback} debugCallback - Debug handler returns raw XML - function(String) { }
*/
this.updateFolderProperty = function (folderId, propName, propValue, successCallback, errorCallback, debugCallback) {
/** @type {string} */
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);
});
};
/**
* PUBLIC: Gets a folder property
*
* @param {string} folderId - The ID for the folder
* @param {string} propName - The property to get
* @param {successCallback} successCallback - returns the folder property value if successful - function(String) { }
* @param {errorCallback} errorCallback - Error handler callback - function(Error) { }
* @param {debugCallback} debugCallback - Debug handler returns raw XML - function(String) { }
*/
this.getFolderProperty = function (folderId, propName, successCallback, errorCallback, debugCallback) {
/** @type {string} */
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) {
/** @type {array} */
var nodes = getNodes(xmlDoc, "t:Value");
// return the content of the node
if (nodes.length > 0) {
successCallback(nodes[0].textContent);
} else {
successCallback(null); // no property found
}
}, function (error) {
if (errorCallback != null)
errorCallback(error);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
};
/**
* PUBLIC: Gets the folder id by the given name from the store
*
* @param {string} folderName - Name of the folder to get the ID for
* @param {successCallback} successCallback - returns the folder ID if successful - function(String) { }
* @param {errorCallback} errorCallback - Error handler callback - function(Error) { }
* @param {debugCallback} debugCallback - Debug handler returns raw XML - function(String) { }
*/
this.getFolderId = function (folderName, successCallback, errorCallback, debugCallback) {
/** @type {string} */
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) {
/** @type {array} */
var nodes = getNodes(xmlDoc, "t:FolderId");
if (nodes.length > 0) {
/** @type {string} */
var id = nodes[0].getAttribute("Id");
successCallback(id);
} else {
errorCallback("Unable to get folder ID");
}
}, function (errorDetails) {
if (errorCallback != null)
errorCallback(errorDetails);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
};
/**
* PUBLIC: Moves an item to the specified folder
*
* @param {string} itemId - the item to be moved
* @param {string} folderId - Name or ID of the folder where the item will be moved
* @param {successCallback} successCallback - returns the folder ID if successful - function(String) { }
* @param {errorCallback} errorCallback - Error handler callback - function(Error) { }
* @param {debugCallback} debugCallback - Debug handler returns raw XML - function(String) { }
*/
this.moveItem = function(itemId, folderId, successCallback, errorCallback, debugCallback) {
/** @type {string} */
var soap = '<MoveItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"' +
' xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' +
' <ToFolderId>' +
' <t:DistinguishedFolderId Id="' + folderId + '"/>' +
' </ToFolderId>' +
' <ItemIds>' +
' <t:ItemId Id="' + itemId + '"/>' +
' </ItemIds>' +
'</MoveItem>';
soap = getSoapHeader(soap);
debugger;
// make EWS callback
asyncEws(soap, function (data) {
if(successCallback != null)
successCallback('succeeeded');
}, function (errorDetails) {
if (errorCallback != null)
errorCallback(errorDetails);
}, function (debug) {
if (debugCallback != null)
debugCallback(debug);
});
};
/**
* PUBLIC: Gets all the recipients from the To/CC/BCC lines
* NOTE: Requires the Office.js library to be loaded as it uses its types
*
* @param {Office.Types.ItemCompose} composeItem - the cast of the Office.context.mailbox.item
* @param {getAllRecipientsCallbackDelegate} successCallback - called when completed function(Office.EmailAddressDetails[], Office.EmailAddressDetails[]) { }
* @param {errorCallbackDelegate} errorCallback - Error handler callback - function(Error) { }
*/
this.getAllRecipientsAsync = function(composeItem, successCallback, errorCallback) {
/** @type {Office.EmailAddressDetails[]} */
var users = [];
/** @type {Office.EmailAddressDetails[]} */
var groups = [];
// get the TO line
composeItem.to.getAsync(function(toAsyncResult) {
if(toAsyncResult.error) {
errorCallback(error);
} else {
/** @type {Office.Recipients} */
var recipients = toAsyncResult.value;
// if there are results, add them to the return array
if(recipients.length > 0) {
recipients.forEach(
/**
* @param {Office.EmailAddressDetails} recip
* @param {Number} index
*/
function(recip, index) {
if(recip.recipientType == Office.MailboxEnums.RecipientType.DistributionList) {
/** @type {Boolean} */
var found = false;
for(var i=0; i<groups.length;i++) {
/** @param {EmailAddressDetails} */
var item = groups[i];
if(item.emailAddress == recip.emailAddress) {
found = true;
break;
}
}
if(!found) {
groups.push(recip);
}
} else {
/** @type {Boolean} */
var found = false;
for(var i=0;i<users.length;i++) {
/** @param {Office.EmailAddressDetails} */
var item = users[i];
if(item.emailAddress == recip.emailAddress) {
found = true;
break;
}
}
if(!found) {
users.push(recip);
}
}
}
);
}
// get the CC line
composeItem.cc.getAsync(function(ccAsyncResult) {
if(ccAsyncResult.error) {
errorCallback(error);
} else {
/** @type {Office.Recipients} */
var recipients = ccAsyncResult.value;
// if we have results
if(recipients.length > 0) {
recipients.forEach(
/**
* @param {Office.EmailAddressDetails} recip
* @param {Number} index
*/