简体   繁体   中英

Get Contacts from All Outlook Contact folders Microsoft Graph

I am using Microsoft Graph to retrieve Contact Folders using the following code:

GraphServiceClient client = new GraphServiceClient(new DelegateAuthenticationProvider(
    (requestMessage) => {
        requestMessage.Headers.Authorization = 
          new AuthenticationHeaderValue("Bearer", accessToken);
        return Task.FromResult(0);
    }));

var contactsData = await client
    .Me
    .Contacts
    .Request()
    .Top(1000)
    .GetAsync();

This above code returns the Contacts, but only returns Contacts from the default folder. I want to retrieve Contacts from all of the user's folders.

I have tried like getting folders first and then their contacts, but it returns a Null Reference Exception as Contacts are null .

var Folders = client
    .Me
    .ContactFolders
    .Request()
    .Top(1000)
    .GetAsync();

Folders.Wait();
var contacts = Folders.Result.SelectMany(a => a.Contacts).ToList();

I don't have environment now on this machine to test, but from what I understand, you can use option query parameter to filter contacts in subFolders.

  1. you need find out all the sub folders

    GET /users/{id | userPrincipalName}/contactFolders

  2. Collect all the subFolders Id
  3. find contacts in each subFolders

    GET /me/contactFolder/{id}/childFolders/{id}/contacts

For more Contact folder and contact related information. Please read these docs. https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/user_list_contactfolders https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/user_list_contacts

First of all, this sample code is created in .net core, you should set up GraphScopes in configuration by following code:

"GraphScopes": "User.Read User.ReadBasic.All Mail.Send MailBoxSettings.ReadWrite Contacts.ReadWrite"

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. If you want to get the main folder and the additional folders you need to get them respectively then combine the result.

// Get the defaultContacts
var defaultContacts = await graphClient
    .Me
    .Contacts
    .Request()
    .GetAsync();

// Get the contactFolders
var contactFolders = await graphClient
    .Me
    .ContactFolders
    .Request()
    .GetAsync();

// Use this to store the contact from all contact folder.
List<Contact> contactFolderContacts = new List<Contact>();

if (contactFolders.Count > 0) {
    for (int i = 0; i < contactFolders.Count; i++) {
        var folderContacts = await graphClient
            .Me
            .ContactFolders[contactFolders[i].Id]
            .Contacts
            .Request()
            .GetAsync();

        contactFolderContacts.AddRange(folderContacts.AsEnumerable());
    }

    // This will combine the contact from main folder and the additional folders.
    contactFolderContacts.AddRange(defaultContacts.AsEnumerable());
} else {
    // This user only has the default contacts folder
    contactFolderContacts.AddRange(defaultContacts.AsEnumerable());
}

// Use this to test the result.
foreach (var item in contactFolderContacts) {
    Debug.WriteLine("first:" + item.EmailAddresses);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM