2017-02-21 77 views
0

我正在开发一个新的使用EWS和JavaScript的Outlook Web插件。范围是选择当前的电子邮件并将其添加到新的电子邮件作为附件。按照这里找到的说明:https://msdn.microsoft.com/en-us/library/office/dn726694(v=exchg.150).aspx#bk_createattachews - >我已经创建了一些功能来完成MSDN文档中说明的那些步骤。问题是我没有得到任何错误或者可以告诉我我做错了什么。这里是我的代码:Web地址展望

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 = '[email protected]'; 

        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); 
    } 
} 

第一个调用按钮点击的函数是getCurrentEmail()。我不确定是否可以像这样做SOAP调用。任何帮助将不胜感激!如果您需要更多信息,请告诉我。谢谢!

+0

你期待有人应该创建一个项目,添加你的代码到它,调试它,并告诉你什么是错的代码?我相信你应该想出特别的问题,例如“m:CreateItem请求到EWS返回以下错误......我的请求有什么问题?”有人愿意帮忙。 –

+0

我不会犯任何错误,这是问题之一!如果我确实...确定我会知道该问什么。并感谢您对如何发布问题的热烈建议... –

+0

我已编辑我的答案。 –

回答

-1

我不确定是否可以做这样的SOAP调用。

以下资源描述EWS operations that add-ins support。 “m:CreateAttachment”不是其中之一;可能你应该从那里开始。

编辑:

我不明白你怎么没有得到任何错误。你在调试吗?你能打中断点吗?这很奇怪。 现在让我告诉你,我做了什么,我注意到哪些问题与您的代码...

  1. 我创建了简单的项目并添加代码。我调用了“getCurrentEmail()”函数,并且只使用这个函数。我没有验证任何其他功能,因为我相信你应该自己做。如果您使用“严格”JS模式(并且您应该!),该函数具有未定义为“var”的“item_id”变量。你应该使用“var”关键字使其成为本地。接下来,当您调用“mailbox.makeEwsRequestAsync”时,“邮箱”变量也是未定义的。你应该把它叫做“Office.context.mailbox.makeEwsRequestAsync”。

  2. EWS请求没有正确地形成。我不知道,可能是你玩过它,但是你提供的资源的例子EWS XML与你通过插入消息ID创建的不同。当我修复JS错误(查看上面的第2点)并发送EWS请求时,它返回开始“错误”并且正确描述该请求是错误的。我更改了请求以匹配示例中提供的请求,并且它返回了“成功”和您请求的信息,包括主题和MIME内容。

我会分享我的代码,只是为了您已经问过的功能,其余的都是自己做的。在这里你去...

 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); 
     } 
+0

嗨伊凡诺夫,谢谢你在这样做的时间。我只发布了我的代码的一部分(变量被声明在第一个函数之上......总之,我做了它。似乎我需要像你一样编写所有的请求,并且在我解决了一些问题之后它们实际上工作其他问题来自回调函数。谢谢!这对我来说是一个新话题,我在MSDN文档上花费精力......周末愉快。 –