Reputation: 603
I am using below code:-
using Microsoft.Graph;
using Microsoft.Identity.Client;
using System;
using System.Collections;
using System.Text.RegularExpressions;
namespace MSGraphAPI
{
class Program
{
private static string clientId = "XXXX";
private static string tenantID = "XXXXX";
private static string objectId = "XXXXXXXX";
private static string clientSecret = "XXXXX";
static async System.Threading.Tasks.Task Main(string[] args)
{
Console.WriteLine(Regex.IsMatch(strToCheck, pattern, RegexOptions.IgnoreCase));
int Flag = 0;
var tenantId = "XXXX.onmicrosoft.com";
var clientId = "XXXXX";
var scopes = new string[] { "https://graph.microsoft.com/.default" };
var confidentialClient = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithAuthority($"https://login.microsoftonline.com/XXXX.onmicrosoft.com/v2.0")
.WithClientSecret(clientSecret)
.Build();
GraphServiceClient graphServiceClient =
new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) => {
// Retrieve an access token for Microsoft Graph (gets a fresh token if needed).
var authResult = await confidentialClient
.AcquireTokenForClient(scopes)
.ExecuteAsync();
// Add the access token in the Authorization header of the API request.
requestMessage.Headers.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
})
);
// Make a Microsoft Graph API query
var users = await graphServiceClient.Users.Request().GetAsync();
do
{
foreach (User user in users)
{
Console.WriteLine(user.DepartmentName);
Console.WriteLine(user.DisplayName);
var Peoples = await graphServiceClient.Users[user.Id].People.Request().GetAsync();
foreach(IUserPeopleCollectionPage People in Peoples)
{
Console.WriteLine(People.CurrentPage.ToString());
}
}
while (users.NextPageRequest != null && (users = await users.NextPageRequest.GetAsync()).Count > 0);
Console.WriteLine("------");
Console.WriteLine(Flag);
}
}
}
I am unable to get the DepartmentName .
I am able to print other values in Peoples which is IUserPeopleCollectionPage type data, which contains several values like DisplayName , Department ,CurrentPage (contains 10 data ) Basically , i am trying to loop over the data using foreach , but unable to get the result.
Please Help.
Upvotes: 0
Views: 81
Reputation: 3495
For both v1.0 (no beta) API and SDK you need to request the additional field (department not departmentName) using $select.
Replace this:
var users = await graphServiceClient.Users.Request()
.GetAsync();
With this:
var users = await graphServiceClient.Users.Request()
.Select("displayName,department").GetAsync();
Upvotes: 1
Reputation: 603
I got the result by using below code:
var Peoples = await graphServiceClient.Users[user.Id].People.Request().GetAsync();
foreach (Person People in Peoples)
{
if (People.DisplayName.Equals(user.DisplayName))
{
Console.WriteLine(People.Department);
}
}
Thanks all for your support.
Upvotes: 0