How to pass data from taskpane to a newly-created email?

30 views Asked by At

My addin has a create new email function that would call displayNewMessageFormAsync(). I would like my event-based addin to access some data object originating from the caller (taskpane) in the new email's OnNewMessageCompose event.


What I've tried

So I tried to pass the data object to the options argument in displayNewMessageFormAsync() but found out that it's accessible in the callback of displayNewMessageFormAsync(), which is not accessible in the new email's OnNewMessageCompose event because they are in different runtimes.

Another option that I thought of is to save the data object in Office JS's customSettings or the browser's localStorage just before displayNewMessageFormAsync() is called, and in the OnNewMessageCompose event, my addin access the data from the localStorage. But how does my addin tell that the data there is meant for this particular new email? Because there's a chance that this function can be called multiple times in quick succession, resulting in when the event is called, it sees multiple same data objects, CMIIW.

Is there a straightforward way to solve this? If not, what's the best way to workaround this?

1

There are 1 answers

0
Slava Ivanov On

I would like to offer the combined solution from your two attempts described above, not sure if you would like it or not, but this is the way I would do if I need this functionality.

When calling displayNewMessageFormAsync() I would generate an UUID and use it twice. Once to set the required object/data to be stored in localStorage or customSettings, so the stored data will be somehow identified. And the second time to set the same UUID into the subject field of the new message, let's say with some kind of pattern (suffix or prefix) to recognize it later. On the OnNewMessageCompose event read the subject of the message and if the pattern is recognized, strip it from the subject field and use the UUID to pull your data/object assigned to it from the storage. I would not clear the storage you will be using at this time, instead, I would clear the storage in the caller script after a small time out, let's say 200ms should do. In this case, if something is wrong with the newly opened message or your script failed for some reason, the storage will be cleared anyway after this time.