I am developing a new web addin for Outlook using EWS and JavaScript. The scope is to get the current email selected and add it to a new email as attachement. Following the instructions found here: https://msdn.microsoft.com/en-us/library/office/dn726694(v=exchg.150).aspx#bk_createattachews -> I have created some functions to do those steps explained in the MSDN documentation. The problem is that I don't get any errors or something that can tell me what I am doing wrong. Here is my Code:
function soapToForwardItemCallback(asyncResult) {
var parser;
var xmlDoc;
if (asyncResult.error != null) {
app.showNotification("EWS Status", asyncResult.error.message);
}
else {
var response = asyncResult.value;
if (window.DOMParser) {
parser = new DOMParser();
xmlDoc = parser.parseFromString(response, "text/xml");
}
else // Older Versions of Internet Explorer
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(response);
}
// 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!!)
var result = xmlDoc.getElementsByTagName("m:ResponseCode")[0].textContent;
if (result == "NoError") {
app.showNotification("EWS Status", "Success!");
}
else {
app.showNotification("EWS Status", "The following error code was recieved: " + result);
}
}
}
function getCurrentEmail() {
var item = Office.context.mailbox.item;
item_id = item.itemId;
var getMimeContent = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
'xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"' +
'xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"' +
'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
// ' <soap:Header>' +
// ' <RequestServerVersion Version="Exchange2013" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" soap:mustUnderstand="0" />' +
// ' </soap:Header>' +
'<soap:Body>' +
'<m:GetItem>' +
// ' xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"' +
// ' xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' +
'<m:ItemShape>' +
'<t:BaseShape>IdOnly</t:BaseShape>' +
'<t:AdditionalProperties>' +
'<t:FieldURI FieldURI="item:MimeContent" />' +
'<t:FieldURI FieldURI="item:Subject" />' +
'</t:AdditionalProperties>' +
'</m:ItemShape>' +
'<m:ItemIds>' +
'<t:ItemId Id="' + item_id + '"/>' +
'</m:ItemIds>' +
'</m:GetItem>' +
'</soap:Body>' +
'</soap:Envelope>'
mailbox.makeEwsRequestAsync(getMimeContent, createMail);
}
function createMail(asyncResult) {
var parser = new DOMParser();
var xmlDoc;
if (asyncResult.error !== null) {
app.showNotification("EWS Status", asyncResult.error.message);
}
else {
var response = asyncResult.value;
if (window.DOMParser) {
xmlDoc = parser.parseFromString(response, "text/xml");
}
else // Older Versions of Internet Explorer
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(response);
}
var toAddresses = 'alexandru.banica@rodacsoft.ro';
var addressesSoap = "";
addressesSoap += "<t:Mailbox><t:EmailAddress>" + toAddresses + "</t:EmailAddress></t:Mailbox>";
var soapToCreateNewEmail = '<?xml version="1.0" encoding="utf-8"?>'+
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'+
'xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"'+
'xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"'+
' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
' <soap:Header>'+
' <t:RequestServerVersion Version="Exchange2013" />'+
' </soap:Header>'+
' <soap:Body>'+
' <m:CreateItem MessageDisposition="SaveOnly">'+
' <m:Items>'+
' <t:Message>'+
' <t:Subject>Message with Item Attachment (MimeContent)</t:Subject>'+
' <t:Body BodyType="HTML">The attachmen</t:Body>'+
' <t:ToRecipients>'+ addressesSoap
+
' </t:ToRecipients>'+
' </t:Message>'+
' </m:Items>'+
' </m:CreateItem>'+
' </soap:Body>'+
' </soap:Envelope>'
mailbox.makeEwsRequestAsync(soapToCreateNewEmail,soapToGetItemDataCallback);
}
}
function createAttachement(asyncResult) {
var parser = new DOMParser();
var xmlDoc;
if (asyncResult.error !== null) {
app.showNotification("EWS Status", asyncResult.error.message);
}
else {
var response = asyncResult.value;
if (window.DOMParser) {
xmlDoc = parser.parseFromString(response, "text/xml");
}
else // Older Versions of Internet Explorer
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(response);
}
var mimeTag = xmlDoc.getElementsByTagName("t:MimeContent")[0].innerText;
var soapToCreateAttachement = '<?xml version="1.0" encoding="utf-8"?>'+
' <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'+
' xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"'+
' xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"'+
' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
' <soap:Header>'+
' <t:RequestServerVersion Version="Exchange2013" />'+
' </soap:Header>'+
' <soap:Body>'+
' <m:CreateAttachment>'+
' <m:ParentItemId Id="'+item_id+'" />'+
' <m:Attachments>'+
' <t:ItemAttachment>'+
' <t:Name>Play tennis?</t:Name>'+
' <t:IsInline>false</t:IsInline>'+
' <t:Message>'+
' <t:MimeContent CharacterSet="UTF-8">'+ mimeTag +'</t:MimeContent>'+
' </t:Message>'+
' </t:ItemAttachment>'+
' </m:Attachments>'+
' </m:CreateAttachment>'+
' </soap:Body>'+
' </soap:Envelope>'
mailbox.makeEwsRequestAsync(soapToCreateAttachement,sendEmailAsAttachement);
}
}
function sendEmailAsAttachement(asyncResult) {
var parser = new DOMParser();
var xmlDoc;
if (asyncResult.error !== null) {
app.showNotification("EWS Status", asyncResult.error.message);
}
else {
var response = asyncResult.value;
if (window.DOMParser) {
xmlDoc = parser.parseFromString(response, "text/xml");
}
else // Older Versions of Internet Explorer
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(response);
}
var changeKey = xmlDoc.getElementsByTagName("t:ItemId")[0].getAttribute("ChangeKey");
var soapToSendEmailAttachment = ' <?xml version="1.0" encoding="utf-8"?>' +
' <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
' xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages"' +
' xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"' +
' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
' <soap:Header>' +
' <t:RequestServerVersion Version="Exchange2013" />' +
' </soap:Header>' +
' <soap:Body>' +
' <m:SendItem SaveItemToFolder="true">' +
' <m:ItemIds>' +
' <t:ItemId Id="'+ item_id +'"' +
' ChangeKey="'+ changeKey +'" />' +
' </m:ItemIds>' +
' <m:SavedItemFolderId>' +
' <t:DistinguishedFolderId Id="sentitems" />' +
' </m:SavedItemFolderId>' +
' </m:SendItem>' +
' </soap:Body>' +
' </soap:Envelope>'
mailbox.makeEwsRequestAsync(soapToSendEmailAttachment, soapToForwardItemCallback);
}
}
The first function called on button click is getCurrentEmail(). I am not sure if I can do the SOAP calls like that. Any help would be greatly appreciated! Please tell me if you need additional information. Thank you!
I am not sure if I can do the SOAP calls like that.
The following resource describes EWS operations that add-ins support. "m:CreateAttachment" is not one of them; probably you should start from there.
EDIT:
I don't understand how comes you don't get any errors. Are you debugging? Can you hit break point? Well this is all weird. Now let me tell you what I've done and what issues with your code I noticed ...
I created the simple project and added your code. I called "getCurrentEmail()" function and work just with this function. I didn't verify any other function, because I believe you should do this on your own.
Javascript errors: If you use "strict" JS mode (and you should!) the function has "item_id" variable which is not defined as "var". You should make it local by using "var" keyword. Next when you call "mailbox.makeEwsRequestAsync" the "mailbox" variable is undefined as well. you should call it as "Office.context.mailbox.makeEwsRequestAsync".
I will share my code just for the function you have asked and the rest try to do on your own. Here you go ...
function _wrapSoapEnvelope(payload) {
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:soap="http://schemas.xmlsoap.org/soap/envelope/" ' +
'xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" ' +
'xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' +
'<soap:Header>' +
'<t:RequestServerVersion Version="Exchange2013" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" soap:mustUnderstand="0" />' +
'</soap:Header>' +
'<soap:Body>' + payload + '</soap:Body>' +
'</soap:Envelope>';
return result;
};
function getCurrentEmail() {
var item = Office.context.mailbox.item,
item_id = item.itemId;
var getMimeContent = '<m:GetItem>' +
'<m:ItemShape>' +
'<t:BaseShape>IdOnly</t:BaseShape>' +
'<t:AdditionalProperties>' +
'<t:FieldURI FieldURI="item:MimeContent" />' +
'<t:FieldURI FieldURI="item:Subject" />' +
'</t:AdditionalProperties>' +
'</m:ItemShape>' +
'<m:ItemIds>' +
'<t:ItemId Id="' + item_id + '"/>' +
'</m:ItemIds>' +
'</m:GetItem>'
Office.context.mailbox.makeEwsRequestAsync(_wrapSoapEnvelope(getMimeContent), createMail);
}