从所有Outlook联系人文件夹中获取联系人Microsoft Graph [英] Get Contacts from All Outlook Contact folders Microsoft Graph

查看:102
本文介绍了从所有Outlook联系人文件夹中获取联系人Microsoft Graph的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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.

我尝试过先获取文件夹,然后再获取其联系人,但由于联系人为null,因此它返回Null Reference Exception.

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();

推荐答案

首先,此示例代码是在.net核心中创建的,您应该通过以下代码在配置中设置GraphScopes:

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"

还请注意,ContactFolders仅在有多个文件夹的情况下才返回结果.默认的联系人"文件夹从不返回.如果用户没有其他文件夹,则将返回空结果.如果要分别获取主文件夹和其他文件夹,请合并结果.

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);
}

这篇关于从所有Outlook联系人文件夹中获取联系人Microsoft Graph的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆