[英]List Contacts in ContactFolder
我正在嘗試使用 Microsoft Graph API 從ContactFolder
獲取聯系人列表。
我使用的代碼如下:
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();
在最后一行,即使通過 Outlook 查看時該文件夾中有聯系人, Contacts
集合也是空的。
我試圖調用 folder.Contacts.GetAsync() 但是該方法似乎不可用:
任何幫助將不勝感激。
首先,您請求FirstOrDefault
但這不是IUserContactFoldersCollectionPage
公開的方法。 您需要按索引處理folders
的項目:
folders[0]
folders[1]
folders[etc]
您也沒有填充Contacts
對象。 請記住,Microsoft Graph 是一種 REST API。 它提供了一組無狀態的 HTTP 方法,因此它不會自動對您的對象模型進行水合。 您需要專門請求Contacts
的數據:
.Contacts.Request().GetAsync();
另請注意,如果有多個文件夾, ContactFolders
只會返回結果。 永遠不會返回默認的聯系人文件夾。 如果用戶沒有其他文件夾,這將返回空結果。
考慮到這一點,您將像這樣檢索Contacts
:
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.