I am trying to get a list of Contacts from a ContactFolder
using Microsoft Graph API.
The code I am using is as follows:
GraphServiceClient graphClient = new GraphServiceClient (new CustomAuthentication ());
// build the request
var request = graphClient.Me.ContactFolders.Request();
// get all contact folders
var folders = await request.GetAsync();
// get the first folder
var folder = folders.FirstOrDefault();
// get all contacts in that folder (Contacts is always null)
var contacts = folder.Contacts.ToList();
On the last line, the Contacts
collection is null even though there are contacts in that folder when viewed through Outlook.
I tried to call folder.Contacts.GetAsync() however that method does not appear to be available:
Any help would be appreciated.
First you're requesting FirstOrDefault
but this isn't a method exposed by IUserContactFoldersCollectionPage
. You need to address the items within folders
by index:
folders[0]
folders[1]
folders[etc]
You're also not populating the Contacts
object. Keep in mind that Microsoft Graph is a REST API. It is provides a collection of stateless HTTP methods so it doesn't automatically hydrate your object model. You need to specifically request the data for Contacts
:
.Contacts.Request().GetAsync();
Also note that ContactFolders
will only return results if there are multiple folders. The default Contacts folder is never returned. If a user has no additional folders, this will return an empty result.
With this in mind, you would retrieve the Contacts
like this:
GraphServiceClient graphClient = new GraphServiceClient(new CustomAuthentication());
// get all contact folders
var folders = await graphClient
.Me
.ContactFolders
.Request()
.GetAsync();
if (folders.Count > 0)
{
// Get contacts from that first folder
var folderContacts = await graphClient
.Me
.ContactFolders[folders[0].Id]
.Contacts
.Request()
.GetAsync();
}
else
{
// This user only has the default contacts folder
var defaultContacts = await graphClient
.Me
.Contacts
.Request()
.GetAsync();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.