javascriptexchangewebservicesoutlook-addinoutlook-web-addinsyo

Outlook Plugin upload FileReference as attachment


I am developing an outlook plugin that needs to upload not a file attachment but a reference file when the user compose a message.

I've tried to use these 3 methods :

  1. addFileAttachmentAsync(uri, attachmentName, [options], [callback])
  2. addItemAttachmentAsync(itemId, attachmentName, [options], [callback])
  3. makeEwsRequestAsync(data, callback, [userContext])

Results of the 3 methods :

  1. Not able to add referenceFile
  2. MessageText":"You cannot attach yourself.","ResponseCode":"ErrorCannotAttachSelf","ResponseClass":"Error"}]}}}
  3. HTTP 500 error.

Code of the second method :

var messageId = await getMessageItemId();
var exchangeAttachment = await uploadAttachmentToExchange(accessToken, messageId, attachment.name,fileUploaded.webUrl);
await addReferenceAttachment(exchangeAttachment.id, attachment.name);

var uploadAttachmentToExchange = async function(accessToken, messageId, fileName, fileUrl){
    var OneDriveURL = "https://graph.microsoft.com/beta/me/messages/"+messageId+"/attachments";
    var payload = JSON.stringify({ 
        "@odata.type": "#microsoft.graph.referenceAttachment", 
        "name": fileName, 
        "sourceUrl": fileUrl, 
        "providerType": "oneDriveBusiness", 
        "permission": "organizationEdit",
        "isFolder" : false,
        "isInline" : false
    }) ;
    
    return new Promise((successCallback, failureCallback) => {
        $.ajax({
            method: "POST",
            contentType: 'application/json',
            dataType: 'json',
            headers: {
                'Authorization': 'Bearer '+ accessToken
            },
            url: OneDriveURL,
            data : payload
        })
        .done(function( response ) {
            successCallback(response);
        })
        .fail(function(resultat, status, error) {
            failureCallback(resultat);
        });
    }); 
};

var getMessageItemId = async function(){
    return new Promise((successCallback, failureCallback) => {
        mailboxItem.getItemIdAsync(function(asyncResult){
            if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                failureCallback(asyncResult.error.message);
            } else {
                successCallback(asyncResult.value);
            }
        });
    });  
};

var addReferenceAttachment = async function(itemId, attachmentName){
    return new Promise((successCallback, failureCallback) => {
        mailboxItem.addItemAttachmentAsync(itemId, attachmentName, {}, function(asyncResult){
            if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                failureCallback(asyncResult.error.message);
            } else {
                successCallback(asyncResult);
            }
        });
    });
};

Code of the third method :

var messageId = await getMessageItemId();
await uploadAttachmentUsingEWSAPI(messageId);

var uploadAttachmentUsingEWSAPI = async function (messageId){
    var request = 
    '<?xml version="1.0" encoding="utf-8"?>'+
    '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'+
    '               xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages"'+
    '               xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types"'+
    '               xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">'
    '  <soap:Header>'
    '    <t:RequestServerVersion Version="Exchange2007_SP1" />'
    '    <t:TimeZoneContext>'
    '      <t:TimeZoneDefinition Id="Central Standard Time" />'
    '    </t:TimeZoneContext>'
    '  </soap:Header>'
    '  <soap:Body>'
    '    <m:CreateAttachment>'
    '      <m:ParentItemId Id="'+ messageId +'" />'
    '      <m:Attachments>'
    '        <t:FileAttachment>'
    '          <t:Name>FileAttachment.txt</t:Name>'
    '          <t:Content>VGhpcyBpcyBhIGZpbGUgYXR0YWNobWVudC4=</t:Content>'
    '        </t:FileAttachment>'
    '      </m:Attachments>'
    '    </m:CreateAttachment>'
    '  </soap:Body>'
    '</soap:Envelope>';

    console.log(request);
    return sendEWSRequest(request);
};

var sendEWSRequest = async function(request){
    return new Promise((successCallback, failureCallback) => {
        Office.context.mailbox.makeEwsRequestAsync(request, function(asyncResult){
            if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                failureCallback(asyncResult.error.message);
            } else {
                successCallback(asyncResult);
            }
        });
    });
};

None of the three methods works. Is there a way to upload a cloud attachment like this one ? :

enter image description here

If yes, what is the method to use ?

Regards


Solution

  • Currently the feature: Adding cloud attachment as reference using Addins APIs, you requested, is not a part of the product. We track Outlook add-in feature requests on our Tech Community Page. Please submit your request there and choose the appropriate label(s). Feature requests on Tech Community are considered, when we go through our planning process.