Outlook Web App: Retrieve contacts - outlook

I'm working on developing outlook web addin which runs on both outlook365 and outlook.com. I have a requirement to create, read and update contacts using that web addin. Below is the sample which add current user to To field of an email.
function addToRecipients() {
var item = Office.context.mailbox.item;
var addressToAdd = {
displayName: Office.context.mailbox.userProfile.displayName,
emailAddress: Office.context.mailbox.userProfile.emailAddress
};
if (item.itemType === Office.MailboxEnums.ItemType.Message) {
Office.cast.item.toMessageCompose(item).to.addAsync([addressToAdd]);
} else if (item.itemType === Office.MailboxEnums.ItemType.Appointment) {
Office.cast.item.toAppointmentCompose(item).requiredAttendees.addAsync([addressToAdd]);
}
}
Can anyone point out me how i can retrieve the outlook contacts using outlook web addin ?
Thanks

Yes, makeEwsRequestAsync API is one way to do it with JS API.

Related

Is there any way to get the files from some external source (API) and attach them in the new email through Outlook Add-in using OfficeJs

Is there any way to get the files from some external source (API) and attach them in the new email through Outlook Add-in using OfficeJs apart from Share point online (Office365) and OneDrive.
Solution approach for implementing attachments functionality from the add-in for external source files and emails
Yes, there is. You may be interested in the addFileAttachmentAsync method which adds a file to a message or appointment as an attachment. The addFileAttachmentAsync method downloads the file at the specified URI and attaches it to the item in the compose form.
Office.context.mailbox.item.addFileAttachmentAsync(
`https://webserver/picture.png`,
'picture.png',
{ asyncContext: null },
function (asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Failed){
write(asyncResult.error.message);
} else {
// Get the ID of the attached file.
const attachmentID = asyncResult.value;
write('ID of added attachment: ' + attachmentID);
}
});
See Manage an item's attachments in a compose form in Outlook for more information.

Is it possible to programmatic-ally access the list of contacts in outlook using Office Add In

I am building an Add In which is supposed to grab in addition to the list of contacts an account has, the contacts (to, from, cc and bcc) that are used in the current Item (Message).
As per the documentation, the following instruction gave me zero contacts, although I have contacts in the contacts book, and reading a message with a sender email.
var contacts = Office.context.mailbox.item.getEntities().contacts;
I need to grab the list of contacts I manage in my account:
This list is accessible with the open graph APIs, I wonder if it's also accessible locally with the Office object for Office Add-Ins
Office Js does not provide APIs to get the list of contacts in the account.
But you can get an auth token from Outlook using authentication APIs, then use this token to acquire Graph token to interact with Graph APIs and get the list of contacts
Office.context.auth.getAccessTokenAsync(function (result) {
if (result.status === "succeeded") {
// Use this token to call Web API
var ssoToken = result.value;
// Now send this token to your server and acquire a Graph token
// Server can talk to Graph APIs and get contacts to display
} else {
// Handle error
}
});
Create a Node.js Office Add-in that uses single sign-on
It looks you misunderstood the documentation.
A quote:
The following example accesses the contacts entities in the current item's body.
var contacts = Office.context.mailbox.item.getEntities().contacts;
You could get all contacts using the below link:
Microsoft.Office.Interop.Outlook.Items OutlookItems;
Microsoft.Office.Interop.Outlook.Application outlookObj = new Microsoft.Office.Interop.Outlook.Application();
MAPIFolder Folder_Contacts;
Folder_Contacts = (MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
OutlookItems = Folder_Contacts.Items;
MessageBox.Show("Wykryto kontaktów: " + OutlookItems.Count.ToString());
for (int i = 0; i < OutlookItems.Count; i++)
{
Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[i+1];
sNazwa = contact.FullName;
sFirma = contact.CompanyName;
sAdress = contact.BusinessAddressStreet;
sMiejscowosc = contact.BusinessAddressPostalCode + " " + contact.BusinessAddressCity;
sEmail = contact.Email1Address;
dataGridView1.Rows.Add(sNazwa, sFirma, sAdress, sMiejscowosc, sEmail);
}
For more information, please refer to the below link:
Get Outlook contacts into C# form-based application

Get the list of Contacts on Windows 10 Phone

I am needing to know how to read the contact list on a Windows 10 Phone. I don't want to use a contact picker; I just need to be able to iterate through all contacts to access their name and phone number, and store it in a List. (Similar to how WhatsApp is able to read your contact list and display it in their app)
I've taken some code from another answer here.
With this code you should be able to get the contacts out.
public async Task IterateThroughContactsForContactListId()
{
ContactStore allAccessStore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly);
var contacts = await allAccessStore.FindContactsAsync();
foreach (var contact in contacts)
{
//process aggregated contacts
if (contact.IsAggregate)
{
//here contact.ContactListId is "" (null....)
//in this case if you need the the ContactListId then you need to iterate through the raw contacts
var rawContacts = await allAccessStore.AggregateContactManager.FindRawContactsAsync(contact);
foreach (var rawContact in rawContacts)
{
//Here you should have ContactListId
Debug.WriteLine($"aggregated, name: {rawContact.DisplayName }, ContactListId: {rawContact.ContactListId}");
}
}
else //not aggregated contacts should work
{
Debug.WriteLine($"not aggregated, name: {contact.DisplayName }, ContactListId: {contact.ContactListId}");
}
}
}
He also notes:
And very important: In the appxmanifest you have to add the contacts
capability. Right click to it in the solution explorer and "View Code"
and then under Capabilities put
<uap:Capability Name="contacts" />
There is no UI for this. See this.

Office Outlook Add-in- Adding an attachment from a secure location

I'm adding an "Add attachment" command as part of an Office Outlook add-in.
I want to find a way to add file from a URL with authorization.
I thought downloading it with ajax and then saving it from a blob but looks like the command won't support it.
My code testing it that fails:
const text = 'attachment content';
const blob = new Blob([text], {type: 'text/plain'});
const attachmentURI = window.URL.createObjectURL(blob);
Office.context.mailbox.item.addFileAttachmentAsync(
attachmentURI,
'file.txt',
{ asyncContext: null },
function (asyncResult) {
if(asyncResult.status == Office.AsyncResultStatus.Failed){
console.log('error adding attachment: ' + asyncResult.error.message);
}
else {
const attachmentID = asyncResult.value;
console.log('added attachment: ' + attachmentID);
}
}
);
Any suggestions on saving an attachment from a URL with permissions?
Function documentation:
https://dev.office.com/docs/add-ins/outlook/add-and-remove-attachments-to-an-item-in-a-compose-form
If you are trying to attach a local file from the user's PC to the email then unfortunately you cannot do so since of course this is JavaScript. The attachment methods in the Outlook Add-in API can only deal with web-based files. You would need a web form or other mechanism to upload the file to your web service to an accessible URI location which you can then point to with the addFileAttachmentAsync method. The ASP.NET Web API would be one alternative to implement your web service in.
In addFileAttachmentAsync the attachmentURI parameter is sent to the Server (in OWA's case), or to the Outlook App (desktop Outlook). Then, either the Server or Outlook goes and downloads the file, and attaches it to the e-mail. If OWA/Outlook can't reach the URI that you provide, then it won't work.

Office365 API access for all network users' calendars using c#

So my main objective is to update network user's outlook calendar from sql server data using office365 api every few minutes. I am stuck at how to get access for other user's outlook calendar? Looked at below link but didnt asnwser much...do i need azure subscription in order to do this? If someone can point me to right direction, that would be great
https://msdn.microsoft.com/en-us/office/office365/howto/common-app-authentication-tasks
I am stuck at how to get access for other user's outlook calendar?
In this case, you can consider using the application permission.
In Azure AD:
register a Web Application in your Azure AD.
add “Read and write calendars in all mail boxes” permission
generate the application secret key
In your application, call Office 365 Graph API - create events by using application token.
http://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/user_post_events
var tmgr = new ApplicationTokenManagement();
var token = tmgr.AcquireToken(Settings.ResourceUrlOfGraph);
var api = new Graph.GraphCalendarAPI(token);
JObject body = new JObject
{
{"subject", "Create from Office 365 API"},
{"start", new JObject { { "DateTime", "2016-03-09T00:00:00"}, { "TimeZone", "China Standard Time" } } },
{"end", new JObject { { "DateTime", "2016-03-10T00:00:00"}, { "TimeZone", "China Standard Time" } } },
{"isAllDay", true }
};
var task = api.CreateEventAsync(body, "user#youcompany.com");
task.Wait();
You can find the complete sample here.

Resources