Updating X-Headers using EWS in Mail Apps


Transport headers are a very useful tool to help pass application settings from one user mailbox to another. It is also useful in some rules on transport and edge servers to determine how to treat a specific message. While manipulating these headers is easier in Outlook (full client) doing this from a Mail App is a little less clear.

The following code is how I managed to get this to work. First, you have to SAVE the mail item so that you can get a EWS ID. Once you get the ID, we then build the soap message (I declared this in the ‘app’ namespace). Once we have the SOAP message we then make the EWS call.

NOTE: I am using the window.alert() which you can find how to use here.

    function setXHeader() {

        // first save the item
        Office.cast.item.toItemCompose(Office.context.mailbox.item).saveAsync(function (composeId)
        {
            var id = composeId.value;

            // update the x-headers
            var xml = app.updateXHeaderSoap(id, "X-Test", "ValueHere");
            Office.context.mailbox.makeEwsRequestAsync(xml, function (ewsResult) {
                if (ewsResult.status != "succeeded") {
                    window.alert("Unable to attach x-headers.\n" + ewsResult.error.message);
                }
            });
        });
    }

        // updates the x-headers in the mail item
        // SEE: https://msdn.microsoft.com/en-us/library/office/dn596091(v=exchg.150).aspx
        app.updateXHeaderSoap = function (id, property, value) {
            var soap =
                '<UpdateItem MessageDisposition="SaveOnly" ConflictResolution="AlwaysOverwrite"' +
                '            xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">' +
                '   <ItemChanges>' +
                '       <t:ItemChange>' +
                '           <t:ItemId Id="' + id + '"/>' +
                '           <t:Updates>' +
                '               <t:SetItemField>' +
                '                   <t:ExtendedFieldURI DistinguishedPropertySetId="InternetHeaders"' +
                '                                       PropertyName="' + property + '"' +
                '                                       PropertyType="String" />' +
                '                   <t:Message>' +
                '                       <t:ExtendedProperty>' +
                '                           <t:ExtendedFieldURI DistinguishedPropertySetId="InternetHeaders"' +
                '                                               PropertyName="' + property + '"' +
                '                                               PropertyType="String" />' +
                '                           <t:Value>' + value + '</t:Value>' +
                '                       </t:ExtendedProperty>' +
                '                   </t:Message>' +
                '               </t:SetItemField>' +
                '           </t:Updates>' +
                '       </t:ItemChange>' +
                '   </ItemChanges>' +
                '</UpdateItem>';

            return app.getSoapHeader(soap);
        };

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s