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!

6 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

Leave a Reply

Your email address will not be published. Required fields are marked *