In previous post I have shown how to use MS Graph Api to create users in Azure AD. Now Lets see how we can fetch and filter users using MS Graph Api. Go through the Setup Graph Api for uses of microsoft Graph Api from starting. This blog is continuous of the previous blog. To create project see Setup Graph Api.
Get All User By Page size
Create class "UserData": For mapping Azure Ad user data.
public class UserData
{
public string GivenName { get; set; }
public string SurName { get; set; }
public string Id { get; set; }
public IEnumerable<ObjectIdentity> Identities { get; set; }
public string DisplayName { get; set; }
public string Email { get; set; }
public string Country { get; set; }
public string EmployeeId { get; set; }
}
Create another class "UserList" and paste below code
public class UserList
{
public List<UserData> Users { get; set; }
public string SkipToken { get; set; } //in user object graph api return
//token for next paging ,if it reaches last token will be null
}
Now in your controller paste the below code
[HttpGet]
[Route("user-list")]
public async Task<IActionResult> GetAllRegisterdUser(int pageSize=5,string pageNumer="")
{
var _graphClient = await GraphClientHelper.GetGraphApiClient(_configuration);
var userList = new UserList();
var queries = new List<QueryOption>();
queries.Add(new QueryOption("$count","true"));
queries.Add(new QueryOption("$top", pageSize.ToString()));
if (!string.IsNullOrWhiteSpace(pageNumer))
{
queries.Add(new QueryOption("$skiptoken", pageNumer));
}
var result = await _graphClient.Users
.Request(queries)
//use if you need selected data
.Select(x => new
{
x.DisplayName,
x.Id,
x.Identities,
x.GivenName,
x.Mail,
x.Country,
x.Surname,
x.EmployeeId
}).GetAsync();
var data = result.CurrentPage.Select
(p => new UserData
{
DisplayName = p.DisplayName,
Country = p.Country,
Email = p.Mail,
EmployeeId = p.EmployeeId,
GivenName = p.GivenName,
Id = p.Id,
Identities = p.Identities,
SurName = p.Surname,
}).ToList();
userList.Users = data;
userList.SkipToken = result.NextPageRequest?.QueryOptions?.FirstOrDefault(x =>
string.Equals("$skiptoken", x.Name, StringComparison.InvariantCultureIgnoreCase))?.Value;
return Ok(userList);
}
Get user by search text :
[HttpGet]
[Route("user-search/{searchItem}")]
public async Task<IActionResult> GetUserBySearch(string searchItem="",int pageSize = 5, string pageNumer = "")
{
var _graphClient = await GraphClientHelper.GetGraphApiClient(_configuration);
var userList = new UserList();
var queries = new List<QueryOption>();
queries.Add(new QueryOption("$top", pageSize.ToString()));
if (!string.IsNullOrWhiteSpace(pageNumer))
{
queries.Add(new QueryOption("$skiptoken", pageNumer));
}
var result = await _graphClient.Users
.Request(queries)
.Filter(SetFilter(searchItem))
//use if you need selected data
.Select(x => new
{
x.DisplayName,
x.Id,
x.Identities,
x.GivenName,
x.Mail,
x.Country,
x.Surname,
x.EmployeeId
}).GetAsync();
var data = result.CurrentPage.Select
(p => new UserData
{
DisplayName = p.DisplayName,
Country = p.Country,
Email = p.Mail,
EmployeeId = p.EmployeeId,
GivenName = p.GivenName,
Id = p.Id,
Identities = p.Identities,
SurName = p.Surname,
}).ToList();
userList.Users = data;
userList.SkipToken = result.NextPageRequest?.QueryOptions?.FirstOrDefault(x =>
string.Equals("$skiptoken", x.Name, StringComparison.InvariantCultureIgnoreCase))?.Value;
return Ok(userList);
}
private string SetFilter(string searchItem)
{
if (string.IsNullOrWhiteSpace(searchItem))
{
return string.Empty;
}
return $"startswith(givenName, '{searchItem}')" +
$" or startswith(surname, '{searchItem}')"+
$" or startswith(displayName, '{searchItem}')" ;
}
Get user by Email Id / Identity :
[HttpGet]
[Route("search-by-email/{emailId}")]
public async Task<IActionResult> GetUserByEmail(string emailId)
{
var _graphClient = await GraphClientHelper.GetGraphApiClient(_configuration);
var userList = new UserList();
var result = await _graphClient.Users
.Request()
.Filter($"identities/any(c:c/issuerAssignedId eq '{emailId}' and c/issuer eq '{tenant url}') ")
.Select(x => new
{
x.DisplayName,
x.Id,
x.Identities,
x.GivenName,
x.Mail,
x.Country,
x.Surname,
x.EmployeeId
}).GetAsync();
return Ok(result);
}
Get user by User Id:
[HttpGet]
[Route("search-by-Id/{userId}")]
public async Task<IActionResult> GetUserById(string userId)
{
var _graphClient = await GraphClientHelper.GetGraphApiClient(_configuration);
var result = await _graphClient.Users[userId]
.Request()
.GetAsync();
return Ok(result);
}
await GraphClientHelper.GetGraphApiClient(_configuration); //to configure this you follow Setup Graph Api.
you can download all the azure samples code : https://github.com/mkumar8184/azure-sdk-services-samples
No comments:
Post a Comment
Thanks for your valuable comments