More about the Microsoft Graph .NET Client Library

A couple weeks ago, I posted an article about an Intro to the Microsoft Graph .NET Client Library.  That article was really focused on setting up the environment and making some initial calls to retrieve some data.  This article is focused on some additional operations, as well as some more advanced capabilities of the Microsoft Graph .NET Client Library.

I think for this post, instead of writing a bunch of verbiage, I’ll just describe some basic scenarios and then show the code that might be an option to solve the problem.  Everyone likes seeing code the best anyway, right?

Code!

Scenario:  We’ve written some code that gathers up thousands of users in our organization.  However, the call is pretty slow. All we really need is the first name, last name, city, and state of the user.  Can we reduce the amount of data being returned during the call to help mitigate some of the network payload?

Absolutely we can, using the Select function from the Request function, much like SELECT in SQL.  As you can see, all of the fields are null except for the fields that we requested be returned.

image

 

Scenario:  We’d like to retrieve the e-mail messages in a mailbox so that they can be displayed in some kind of external client.  Maybe something like The Help Desk Demo.

Sure, we’ll just find the mailbox, retrieve the messages, and then sort by the receivedDateTime desc so that the newest messages are at the front of the list.

image

Scenario:  So far, we’ve just talked about retrieving data.  What if we want to create a new group?  Sure, no problem.  Note, at this time, you cannot create mail enabled groups via the Microsoft Graph.  Total bummer.

image

I suppose once we’ve create a group, it might make sense to also create a user as well.  Easy enough:

image

Now, what if we want to add that user to the group that we created previously?  At first glance, it would seem that maybe we’d want to do something like newGroup.Members.Add(user).  It turns out that what we really want to do is to create a reference between the user and group instead, like so:

image

And sure enough, we find that Frank Jones is now a member of the Finance Department group:

image

What about deleting that member?  Sure, we’ll just delete the reference, like so:

image

And we can see that Frank Jones was, in fact, removed from the Finance Department group:

image

 

What else?  What else would be helpful to see demonstrated?  Leave a comment and I’ll try and make it happen!

15 comments on “More about the Microsoft Graph .NET Client Library

  1. Hi Jonathan,

    Your articles on Microsoft Grap client library really was wonderful which gave me an idea on how to start using the library. Could you please show me how can we get users from a specific domain inside a tenant? Also could you please demo how can we add members to an 0365 group in a single api call?

    Thanks a lot in advance,
    Vijay

    1. Hi Vijay,

      Regarding querying specific domains in a tenant, I don’t believe you can. We’ve struggled with the same situation in a couple environments. The best solution that we’ve found so far, and really, it’s a poor solution, is to query ALL of the users and then filter on the client side. It performs poorly and, depending on how it’s implemented, requires quite a lot of service calls. To date, it just seems that the Graph doesn’t support sufficient segments/filters to do this type of operation yet. If you come up with another solution, I’d love to hear about it.

      Unfortunately, I don’t think you can add multiple members to a group in a single call. I played with it a bit this afternoon and it looks like the /members/ segment is expecting a single identifier for the members/group to be added to the parent group. You could probably multi-thread this such that you’re sending multiple calls at once, but ultimately, it looks like one id per request.

      Not great answers, I know, but hopefully they help a bit.

      Jonathan

  2. Thanks for your valuable posts about Microsoft Graph.
    Can you tell me if you managed to find a workaround about the creation of mail enabled groups ?

    Regards,

    Patrice

    1. Hi Patrice,

      Unfortunately not. In fact, I had a conversation with the Microsoft Graph PM a few days ago and she confirmed that it’s still not possible. Sorry for the bad news.

      Jonathan

      1. Thanks a lot for your answer.

        Do you know if (outside Microsoft Graph) there is any API (managed API or REST API) available to create distribution list, or the use of powershell scripts is the only way to create them ?

        Patrice

        1. Hi Patrice,

          If you want a genuine Distribution Group, then no, there are no APIs that I’m aware of to create (or manage) them. It’s unfortunate, but I think the direction for now is really focused on Office 365 groups. I’m still holding out hope that there might be support for the other types of groups at some point in the future, though.

          Jonathan

  3. Hi,
    Is it possible to do Search operation in Site level similar to the one present in DocumentLibrary in SharePointOnline using GraphServiceClient.
    For example, we are using below code for doing a search in a DocumentLibrary GraphServiceClient.Sites[“SiteID”].Drives[“DriveID”].Items[“ItemID”].Search(searchText).Request().GetAsync();

    Regards,
    Harsha

  4. I’m just getting familiar with Office 365 and Microsoft Graph. I’ll eventually be working on an app that pulls in a users Office 365 Contacts. I’ve been able to successfully retrieve some of a user’s info, but can’t find anything about accessing contacts. Do you have any suggestions?

  5. Hi Jonathon,
    Thanks for posting this – very useful 🙂
    Following the aspnet core example on github I have been able to get the basic profile data of both myself as the signed in user and a colleague. However, if try to retrieve calendar information for the colleague, I get an ‘itemNotFound’ response, even though the calendar has been shared with me. I have also checked the delegate permissions for the application in Azure to add calendar read.
    So, the following:
    await graphClient.Users[email].Calendars.Request().GetAsync()
    Is returning the error code for the user id.
    Kind Regards
    Ben
    Am I missing a permission or would this approach only ever work for the authenticated user’s calendars and I should be using a different approach for shared calendars?

  6. So, I have gathered the users, and now I want to download their userprofiles, how do I do this?

    With LDAP to a traditional ActiveDirectory I get the data as BASE64, here, I get anobject with an ID, a width and a height? I can’t seem to figure out what to do next, is there an another endpoint I should call in order to get some bytes back? 🙂

  7. Thanks Jonathan for your awesome post, finally a document that is well written and actually works!
    Can you please let me know how can I retrieve for a given user_id, the groups that he is part of?
    I have tried the following:
    var allUserGroupsRequest = graph.Users.Request().Select(“MemberOf”);
    var allUserGroupsRequest = graph.Users.Request().ExpandMemberOf”);

    And none worked for me…

    1. Hi Revital,

      Try something like this:

      IDirectoryObjectGetMemberGroupsCollectionPage result = await graphClient.Users[userPrincipalName].GetMemberGroups(false).Request().PostAsync();

      Jonathan

Leave a Reply

Your email address will not be published.