public GraphUserResponseList GetUserById(string token, GraphUserList graphUserList) { int counter = 0; this.token = token; GraphUserResponseList graphUsers = new GraphUserResponseList(); GraphBatchRequestList requestArray = new GraphBatchRequestList(); foreach (var user in graphUserList.Value) { string url = String.Format("users/{0}?$select=id,displayName,userPrincipalName,birthday,hireDate,givenName,surname,jobTitle,mobilePhone,officeLocation,businessPhones,mail,userType,department", user.Id); requestArray.Requests.Add(new GraphBatchRequest { Id = user.Id, Method = "GET", Url = url }); counter++; if (counter % 20 == 0 || (graphUserList.Value.Count - graphUsers.Responses.Count < 20)) { client = new RestClient(ConfigurationManager.AppSettings["resource"]); var userRequest = new RestRequest("v1.0/$batch", Method.POST); userRequest.AddHeader("Authorization", $"Bearer {token}"); userRequest.AddHeader("Content-Type", "application/json"); userRequest.AddJsonBody(JsonConvert.SerializeObject(requestArray)); IRestResponse userResponse = client.Execute(userRequest); if (userResponse.StatusCode == System.Net.HttpStatusCode.OK) { var users = JsonConvert.DeserializeObject <GraphUserResponseList>(userResponse.Content); foreach (var item in users.Responses) { item.Body.Manager = GetManagerUserPrincipalName(item.Body.Id); graphUsers.Responses.Add(item); } } else { Console.WriteLine("GetUserById Method= " + userResponse.StatusCode); } requestArray.Requests.Clear(); } } return(graphUsers); }
static void Main(string[] args) { try { RestHelper rest = new RestHelper(); string resource = ConfigurationManager.AppSettings["resource"]; string clientId = ConfigurationManager.AppSettings["graph_client_id"]; string clientSecret = ConfigurationManager.AppSettings["graph_client_secret"]; string tenant = new Uri(ConfigurationManager.AppSettings["TenantUrl"]).Host.Split('.')[0]; ADALToken token = rest.AcquireTokenWithClientCredentials(tenant, resource, clientId, clientSecret); AzureUser azureUser = new AzureUser(); if (!string.IsNullOrEmpty(token.AccessToken)) { GraphUserList azureUsersId = azureUser.GetAzureUsers(token.AccessToken); if (azureUsersId != null) { GraphUserResponseList azureUsers = azureUser.GetUserById(token.AccessToken, azureUsersId); Console.WriteLine(string.Format("{0} azure users has found...", azureUsers.Responses.Count)); } else { Console.WriteLine("Azure user not found!"); } } else { Console.WriteLine("Token is null!"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); }