Search code examples
outlookexchangewebservicesoutlook-addinoffice-jsadd-in

Unable to add attachment to Office JS Add-in


I have created an EWS request to attach an email but i receive empty value from it but has "succeeded" status. Btw.

I first created an makeEwsRequestAsync request for saving email to draft and it already works but when i try to add attachment to it using this request it didnt add. Any suggestions or help please

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
function createAttachment() {

    var request =
        '<?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:t="http://schemas.microsoft.com/exchange/services/2006/types">' +
        '  <soap:Body>' +
        '    <CreateAttachment xmlns="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">' +
        '      <ParentItemId Id="'+itemID+'" />' +
        '      <Attachments>' +
        '        <t:ItemAttachment>' +
        '          <t:Name>Please</t:Name>' +
        '           <t:Message>' +
        '               <t:ItemClass>IPM>Note</t:ItemClass>' +
        '               <t:Subject>test</t:Subject>' +
        '               <t:Body BodyType="Text">my test</t:Body>' +
        '           </t:Message>' +
        '        </t:ItemAttachment>' +
        '      </Attachments>' +
        '    </CreateAttachment>' +
        '  </soap:Body>' +
        '</soap:Envelope>';
    return request;
}

</script>

This is my create Attachment EWS request

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">


    function composeMail(emailSubject, emailDescription) {
        var subject= subjectPrefix + " " + emailSubject;
        var request =
            '<?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:t="http://schemas.microsoft.com/exchange/services/2006/types"' +
            '               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
            '  <soap:Header>' +
            '    <RequestServerVersion Version="Exchange2007_SP1" />' +
            '  </soap:Header>' +
            '  <soap:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">' +
            '    <m:CreateItem MessageDisposition="SaveOnly">' +
            '      <m:SavedItemFolderId>' +
            '        <t:DistinguishedFolderId Id="drafts" />' +
            '      </m:SavedItemFolderId>' +
            '      <m:Items>' +
            '            <t:Message>' +
            '              <t:ItemClass>IPM.Note</t:ItemClass>' +
            '              <t:Subject>' + subject + '</t:Subject>' +
            '              <t:Body BodyType="HTML">' + emailDescription + '</t:Body>' +
            '              <t:Importance>Low</t:Importance>' +
            '              <t:ToRecipients>' +
            '                <t:Mailbox>' +
            '                  <t:EmailAddress>' + recepient + '</t:EmailAddress>' +
            '                </t:Mailbox>' +
            '              </t:ToRecipients>' +
            '              <t:IsRead>false</t:IsRead>' +
            '            </t:Message>' +
            '          </m:Items>' +
            '        </m:CreateItem>' +
            '  </soap:Body>' +
            '</soap:Envelope>';
        return request;
    }
</script>

This is my create Email EWS request

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
    function send() {
        var request =
            '<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:t="http://schemas.microsoft.com/exchange/services/2006/types">' +
            '  <soap:Body>' +
            '    <SendItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"' +
            '    SaveItemToFolder="true">' +
            '      <ItemIds>' +
            '        <t:ItemId Id="' + itemID + '"/>' +
            '      </ItemIds>' +
            '    </SendItem>' +
            '  </soap:Body>' +
            '</soap:Envelope>';
        return request;
    }

</script>

This is my send EWS Request and i'm calling them using

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
    function sendRequest(emailSubject, emailDescription, emailItemID) {
        // Create a local variable that contains the mailbox.
        try {
            itemID = Office.context.mailbox.item.itemId;
            Office.context.mailbox.makeEwsRequestAsync(
                composeMail(emailSubject, emailDescription), callbackCompose);
            Office.context.mailbox.makeEwsRequestAsync(
                createAttachment(), callbackAttachment);
            Office.context.mailbox.makeEwsRequestAsync(
                send(), callbackSend);
        } catch (error) {
            $("#id-error-msg").text(error);
        }
</script>


Solution

  • When you want to manipulate emails from a compose mode, you may refer to this documentation by Microsoft team. I believe its quite more simple and comprehensive.

    -https://learn.microsoft.com/en-us/outlook/add-ins/add-and-remove-attachments-to-an-item-in-a-compose-form

    Otherwise if you want to manipulate emails from a read mode. There can be two options to do it.

    One is by using Rest API. Though, there are some requirements you need to met to make this request. Mine doesn't work on Desktop but works on Web but you can refer to this link if you want to try it: https://learn.microsoft.com/en-us/outlook/add-ins/use-rest-api

    Upon encountering the minimum requirement issue. I found a way to make it work using Exchange Web Service request (EWS). It mainly uses XML files to send request to exchange web service and return a response.

    However, i found that i need to get the MimeContent of that email to be able to add to my CreateItem xml request. Please check this link for more info. https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/getitem-operation-email-message

    Then, I added the existing item to a new email by using its MimeContent. You can check it out using this link: https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-add-attachments-by-using-ews-in-exchange