I am building an add-in for Outlook, using Office.JS.
My add-in retrieves the (signed-in) user's contacts.
I am trying to achieve the following: After the contacts are retrieved, when the user is in compose mode, add the loaded contacts to the recipient's field. It should only be added for the period of creating the message (I don't want to add it permanently to the user's contact list),
Thanks
I have not found a proper way yet, according to the documentation, that allows achieving this.
when the user is in compose mode, add the loaded contacts to the recipient's field. It should only be added for the period of creating the message (I don't want to add it permanently to the user's contact list)
The Office JavaScript API provides asynchronous methods (Recipients.getAsync
, Recipients.setAsync
, or Recipients.addAsync
) to respectively get, set, or add recipients in a compose form of an appointment or message. These asynchronous methods are available to only compose add-ins. For example, to set recipients for your message you can use the following code:
let item;
Office.initialize = function () {
item = Office.context.mailbox.item;
// Checks for the DOM to load using the jQuery ready method.
$(document).ready(function () {
// After the DOM is loaded, app-specific code can run.
// Set recipients of the composed item.
setRecipients();
});
}
// Set the display name and email addresses of the recipients of
// the composed item.
function setRecipients() {
// Local objects to point to recipients of either
// the appointment or message that is being composed.
// bccRecipients applies to only messages, not appointments.
let toRecipients, ccRecipients, bccRecipients;
// Verify if the composed item is an appointment or message.
if (item.itemType == Office.MailboxEnums.ItemType.Appointment) {
toRecipients = item.requiredAttendees;
ccRecipients = item.optionalAttendees;
}
else {
toRecipients = item.to;
ccRecipients = item.cc;
bccRecipients = item.bcc;
}
// Use asynchronous method setAsync to set each type of recipients
// of the composed item. Each time, this example passes a set of
// names and email addresses to set, and an anonymous
// callback function that doesn't take any parameters.
toRecipients.setAsync(
[{
"displayName":"Graham Durkin",
"emailAddress":"graham@contoso.com"
},
{
"displayName" : "Donnie Weinberg",
"emailAddress" : "donnie@contoso.com"
}],
function (asyncResult) {
if (asyncResult.status == Office.AsyncResultStatus.Failed){
write(asyncResult.error.message);
}
else {
// Async call to set to-recipients of the item completed.
}
}); // End to setAsync.
// Set any cc-recipients.
ccRecipients.setAsync(
[{
"displayName":"Perry Horning",
"emailAddress":"perry@contoso.com"
},
{
"displayName" : "Guy Montenegro",
"emailAddress" : "guy@contoso.com"
}],
function (asyncResult) {
if (asyncResult.status == Office.AsyncResultStatus.Failed){
write(asyncResult.error.message);
}
else {
// Async call to set cc-recipients of the item completed.
}
}); // End cc setAsync.
// If the item has the bcc field, i.e., item is message,
// set bcc-recipients.
if (bccRecipients) {
bccRecipients.setAsync(
[{
"displayName":"Lewis Cate",
"emailAddress":"lewis@contoso.com"
},
{
"displayName" : "Francisco Stitt",
"emailAddress" : "francisco@contoso.com"
}],
function (asyncResult) {
if (asyncResult.status == Office.AsyncResultStatus.Failed){
write(asyncResult.error.message);
}
else {
// Async call to set bcc-recipients of the item completed.
// Do whatever appropriate for your scenario.
}
}); // End bcc setAsync.
}
}
// Writes to a div with id='message' on the page.
function write(message){
document.getElementById('message').innerText += message;
}
And if you need to add recipients:
// Add specified recipients as required attendees of
// the composed appointment.
function addAttendees() {
if (item.itemType == Office.MailboxEnums.ItemType.Appointment) {
item.requiredAttendees.addAsync(
[{
"displayName":"Kristie Jensen",
"emailAddress":"kristie@contoso.com"
},
{
"displayName" : "Pansy Valenzuela",
"emailAddress" : "pansy@contoso.com"
}],
function (asyncResult) {
if (asyncResult.status == Office.AsyncResultStatus.Failed){
write(asyncResult.error.message);
}
else {
// Async call to add attendees completed.
// Do whatever appropriate for your scenario.
}
}); // End addAsync.
}
}
These operations are not temporary, the changes made will be saved to the message.