Home » Javascript » exchangewebservices – Set HTML in Outlook Cached Mode via Javascript and EWS

exchangewebservices – Set HTML in Outlook Cached Mode via Javascript and EWS

Posted by: admin February 23, 2020 Leave a comment

Questions:

My work Outlook environment is changing from online (continuous connection) to cached mode and that breaks the desktop component of an Outlook add-in designed to save and then forward draft emails to an in-house security check.

My original code would save the draft async, then use the Id and change key to then forward it. In cached mode, there is no Id until the account synchs with the Exchange.

Now broken code (does not work in cached mode)

   //Save current item as draft
   mailForward.SaveDraftIfNecessary = function SaveDraftIfNecessary() {
       if (_item_id == null || _item_id == undefined) {
           Office.context.mailbox.item.saveAsync(function (asyncResult) {
               if (asyncResult.error) {
                   app.showNotification(_appName, "Error: " + asyncResult.error.message);
               }
               else {
                   _item_id = asyncResult.value;
               }
           });
       }
   };

I’ve been able to write code that can replicate the mail and send it, but I need to be able to insert the draft body into the new email body, but it does not accept it. It does not work for text or HTML.

The code for getting the body type and text is as follows:

              item.body.getTypeAsync(
                  function (asyncResultGet) {
                      if (asyncResultGet.status === Office.AsyncResultStatus.Failed) {
                          statusUpdate("icon16", asyncResultGet.error.message);
                          stopSpinner();
                      }
                      else {
                          bodyType = asyncResultGet.value;
                          item.body.getAsync(bodyType,
                              function (asyncResultGet) {
                                  if (asyncResultGet.status === Office.AsyncResultStatus.Failed) {
                                      statusUpdate("icon16", asyncResultGet.error.message);
                                      stopSpinner();
                                  }
                                  else {
                                      bodyText = asyncResultGet.value;

The SOAP that I am trying to use to insert the body text. I have also tried t:NewBodyContent instead of t:Body:

      '               <t:Message>' +
      '                   <t:Subject>' + _subject + '</t:Subject>' +
      '                   <t:Body BodyType="'+ _bodyType +'">' + _bodyText + '</t:Body>' +
      '                   <t:ToRecipients>' + _adddressesSoap + '</t:ToRecipients>' +
      '               </t:Message>' +
How to&Answers:

My issue was encoding. I had an inkling that might be the problem, but didn’t test it out until I found this link:

HTML-encoding lost when attribute read from input field

The code I used is this:

        var htmlEncode = function htmlEncode(s) {
            var ntable = {
                "&": "amp",
                "<": "lt",
                ">": "gt",
                "\"": "quot"
            };
            s = s.replace(/[&<>"]/g, function (ch) {
                return "&" + ntable[ch] + ";";
            });
            s = s.replace(/[^ -\x7e]/g, function (ch) {
                return "&#" + ch.charCodeAt(0).toString() + ";";
            });
            return s;
        };