Azure AD Graph API – Get User (or DirectoryObject) Extended Properties (C#)

Ok, this blog post will be covering an API that doesn’t have any enhancements planned, but I’m hoping this may prove to be useful to others.

From Microsoft themselves:

We strongly recommend that you use Microsoft Graph instead of Azure AD Graph API to access Azure Active Directory resources. Our development efforts are now concentrated on Microsoft Graph and no further enhancements are planned for Azure AD Graph API. There are a very limited number of scenarios for which Azure AD Graph API might still be appropriate; for more information, see the Microsoft Graph or the Azure AD Graph blog post in the Office Dev Center.

First things first, our clients’ Environment contained Extension Properties that were on-premise. To get these available via Azure AD we had to run the Azure AD Connect Sync to sync Directory extensions made by Microsoft Exchange.

Now on to the code. The trick was to cast my IDirectoryObject to the proper class Microsoft.Azure.ActiveDirectory.GraphClient.User. This enabled me to call the “GetExtendedProperties” method.

This site was helpful as well https://www.simple-talk.com/cloud/security-and-compliance/azure-active-directory-part-6-schema-extensions/

From the code below I was able to extract the Key’s required by our application. And that, my friends, is that.


List<IUser> users = … // Get users from Azure AD Graph API in C# code
foreach (IUser u in users)
{
// IMPORTANT – Casting from IUser to Microsoft.Azure.ActiveDirectory.GraphClient.User
// gives us the ability to call .GetExtendedProperties();
Microsoft.Azure.ActiveDirectory.GraphClient.User usr = u as Microsoft.Azure.ActiveDirectory.GraphClient.User;
var extProps = usr.GetExtendedProperties();
foreach (KeyValuePair<string, object> pair in extProps) {
// pair.Key is the name of the extension property.
// log this to your console to see!
}
}

One thought on “Azure AD Graph API – Get User (or DirectoryObject) Extended Properties (C#)

  1. Thank you for pointing out that IUser had to be casted to User so I can call GetExtendedProperties!!! It literally took me days to find your highly useful post so I could work through this issue!!!
    While I was able to get the extended properties via other methods, your method is a much more useful and cleaner approach.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.