2017-02-21 71 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文檔上花費精力......週末愉快。 –