Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Microsoft Azure Graph Client for .net emit $expand

I'm trying to retrieve a (paged) list of all users in the directory, with the manager property expanded. When I run the following HTTP query it works as I want:

https://graph.windows.net/DOMAIN/users/?$expand=manager&api-version=2013-11-08

However I don't seem to grasp how to make the same query with the Azure AD Graph client. This is what I'm trying:

var userResult = _activeDirectoryClient.Users.Expand(x => x.Manager).ExecuteAsync().Result;
like image 961
Svante Svenson Avatar asked Sep 08 '15 11:09

Svante Svenson


2 Answers

I adapted the following from the example at https://github.com/AzureADSamples/ConsoleApp-GraphAPI-DotNet/blob/master/GraphConsoleAppV3/Program.cs, please give it a shot:

	        List<IUser> usersList = null;
            IPagedCollection<IUser> searchResults = null;
            try
            {
                IUserCollection userCollection = activeDirectoryClient.Users;
                userResult = userCollection.ExecuteAsync().Result;
                usersList = searchResults.CurrentPage.ToList();
            }
            catch (Exception e)
            {
                Console.WriteLine("\nError getting User {0} {1}", e.Message,
                    e.InnerException != null ? e.InnerException.Message : "");
            }

            if (usersList != null && usersList.Count > 0)
            {
                do
                {
                    usersList = searchResults.CurrentPage.ToList();
                    foreach (IUser user in usersList)
                    {
                        Console.WriteLine("User DisplayName: {0}  UPN: {1}  Manager: {2}",
                            user.DisplayName, user.UserPrincipalName, user.Manager);
                    }
                    searchResults = searchResults.GetNextPageAsync().Result;
                } while (searchResults != null);
            }
            else
            {
                Console.WriteLine("No users found");
            }
like image 69
Keith Tuomi - MVP Avatar answered Nov 10 '22 17:11

Keith Tuomi - MVP


The GraphClient does not implement all of the features in the Graph API just yet.
New features are being added to the GraphClient over time and will be announced on the AAD team blog:

http://blogs.msdn.com/b/aadgraphteam/

And updates will be available in the nuget package ( Microsoft Azure Active Directory Graph Client Library ).

You can make what you need by making Http calls to the url you have in the question and get the response back as Json like this:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("https://graph.windows.net/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    HttpResponseMessage response = await client.GetAsync("DOMAIN/users/?$expand=manager&api-version=2013-11-08");
    if (response.IsSuccessStatusCode)
    {
         // TODO: Deserialize the response here...
    }
}
like image 22
Aram Avatar answered Nov 10 '22 19:11

Aram