{"id":710,"date":"2018-10-12T09:24:24","date_gmt":"2018-10-12T09:24:24","guid":{"rendered":"http:\/\/blog.softreeconsulting.com\/?p=710"},"modified":"2018-10-12T09:24:24","modified_gmt":"2018-10-12T09:24:24","slug":"get-members-office365-azure-ad-using-graph-api","status":"publish","type":"post","link":"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/","title":{"rendered":"Get members from Office 365 Azure Ad using Graph API"},"content":{"rendered":"<p><a href=\"http:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2018\/10\/Azure-Ad.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-719\" src=\"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2018\/10\/Azure-Ad.png\" alt=\"azure-ad\" width=\"1279\" height=\"720\" \/><\/a><\/p>\n<p>1.Create a WPF application<\/p>\n<p>2. Add the below mentioned code in the App.xaml.cs page.<\/p>\n<p>[code lang=&#8221;c&#8221;]<br \/>\nprivate static string ClientId = &#8220;0b8b0665-bc13-4fdc-bd72-e0227b9fc011&#8243;;<br \/>\nprivate static PublicClientApplication _clientApp ;<br \/>\npublic static PublicClientApplication PublicClientApp { get { return _clientApp; } }<\/p>\n<p>static App()<br \/>\n{<br \/>\n_clientApp = new PublicClientApplication(ClientId);<br \/>\n}[\/code]<\/p>\n<p>3.In mainwindow.xaml Add a button for calling the graph and a multiline textbox to show the result. I have attached the code below for mainwindow.xaml.cs<\/p>\n<p>[code lang=&#8221;c&#8221;]<br \/>\npublic partial class MainWindow : Window<br \/>\n{<br \/>\nstring graphAPIEndpoint = &#8220;https:\/\/graph.microsoft.com\/v1.0\/groups\/4fd9e6bb-c353-436c-9157-eae04c7b5182\/members&#8221;; \/\/ the guid used here should be the guid of the azure ad we wanted to get member of.<br \/>\nstring[] scopes = new string[] { &#8220;Directory.Read.All&#8221;, &#8220;User.Read.All&#8221; };<\/p>\n<p>public MainWindow()<br \/>\n{<br \/>\nInitializeComponent();<br \/>\n}<\/p>\n<p>\/\/\/ &amp;lt;summary&amp;gt;<br \/>\n\/\/\/ Call AcquireTokenAsync &#8211; to acquire a token requiring user to sign-in<br \/>\n\/\/\/ &amp;lt;\/summary&amp;gt;<br \/>\nprivate async void CallGraphButton_Click(object sender, RoutedEventArgs e)<br \/>\n{<br \/>\nAuthenticationResult authResult = null;<br \/>\nvar app = App.PublicClientApp;<br \/>\nResultText.Text = string.Empty;<\/p>\n<p>try<br \/>\n{<br \/>\nauthResult = await app.AcquireTokenSilentAsync(scopes, app.Users.FirstOrDefault());<br \/>\n}<br \/>\ncatch (MsalUiRequiredException ex)<br \/>\n{<br \/>\n\/\/ This indicates you need to call AcquireTokenAsync to acquire a token<br \/>\nSystem.Diagnostics.Debug.WriteLine($&#8221;MsalUiRequiredException: {ex.Message}&#8221;);<\/p>\n<p>try<br \/>\n{<br \/>\nauthResult = await app.AcquireTokenAsync(scopes);<br \/>\n}<br \/>\ncatch (MsalException msalex)<br \/>\n{<br \/>\nResultText.Text = $&#8221;Error Acquiring Token:{System.Environment.NewLine}{msalex}&#8221;;<br \/>\n}<br \/>\n}<br \/>\ncatch (Exception ex)<br \/>\n{<br \/>\nResultText.Text = $&#8221;Error Acquiring Token Silently:{System.Environment.NewLine}{ex}&#8221;;<br \/>\nreturn;<br \/>\n}<\/p>\n<p>if (authResult != null)<br \/>\n{<br \/>\nResultText.Text = await GetHttpContentWithToken(graphAPIEndpoint, authResult.AccessToken);<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/\/ &amp;lt;summary&amp;gt;<br \/>\n\/\/\/ Perform an HTTP GET request to a URL using an HTTP Authorization header<br \/>\n\/\/\/ &amp;lt;\/summary&amp;gt;<br \/>\n\/\/\/ &amp;lt;param name=&#8221;url&#8221;&amp;gt;The URL&amp;lt;\/param&amp;gt;<br \/>\n\/\/\/ &amp;lt;param name=&#8221;token&#8221;&amp;gt;The token&amp;lt;\/param&amp;gt;<br \/>\n\/\/\/ &amp;lt;returns&amp;gt;String containing the results of the GET operation&amp;lt;\/returns&amp;gt;<br \/>\npublic async Task&amp;lt;string&amp;gt; GetHttpContentWithToken(string url, string token)<br \/>\n{<br \/>\nvar httpClient = new System.Net.Http.HttpClient();<br \/>\nSystem.Net.Http.HttpResponseMessage response;<br \/>\ntry<br \/>\n{<br \/>\nvar request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url);<br \/>\n\/\/Add the token in Authorization header<br \/>\nrequest.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(&#8220;Bearer&#8221;, token);<br \/>\nresponse = await httpClient.SendAsync(request);<br \/>\nvar content = await response.Content.ReadAsStringAsync();<\/p>\n<p>return content;<br \/>\n}<br \/>\ncatch (Exception ex)<br \/>\n{<br \/>\nreturn ex.ToString();<br \/>\n}<br \/>\n}<br \/>\n} [\/code]<\/p>\n<p><strong>Final Result:-<\/strong><\/p>\n<p><a href=\"http:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2018\/10\/ad-Screenshot.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-702\" src=\"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2018\/10\/ad-Screenshot.png\" alt=\"Office365\" width=\"512\" height=\"437\" \/><\/a><\/p>\n<p><strong>This solution is brought to you by our SharePoint professionals.<\/strong><\/p>\n<p style=\"text-align: justify;\"><a href=\"http:\/\/www.softreeconsulting.com\/\"><strong>Softree Consulting<\/strong><\/a>\u00a0employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.<\/p>\n<p style=\"text-align: justify;\">Be it SPFx or SharePoint add-in developments,\u00a0<a href=\"http:\/\/www.softreeconsulting.com\/sharepoint-2019\/\"><strong>SharePoint 2019 developments<\/strong><\/a>, web part developments, migrating from SharePoint 2010\/2013 to SharePoint 2013\/2016\/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.Create a WPF application 2. Add the below mentioned code in the App.xaml.cs page. [code lang=&#8221;c&#8221;] private static string ClientId = &#8220;0b8b0665-bc13-4fdc-bd72-e0227b9fc011&#8243;; private static PublicClientApplication _clientApp ; public static PublicClientApplication PublicClientApp { get { return _clientApp; } } static App() { _clientApp = new PublicClientApplication(ClientId); }[\/code] 3.In mainwindow.xaml Add a button for calling the graph and a multiline textbox to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[162],"tags":[163,164,165,166,167,168],"class_list":["post-710","post","type-post","status-publish","format-standard","hentry","category-azure-ad","tag-azure-ad-graph-api-example","tag-azure-ad-graph-api-sample","tag-azure-graph-api-example","tag-azure-graph-api-tutorial","tag-how-to-use-the-azure-ad-graph-api","tag-office-365"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Get members from Office 365 Azure Ad using Graph API - Softree Technology<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Get members from Office 365 Azure Ad using Graph API - Softree Technology\" \/>\n<meta property=\"og:description\" content=\"1.Create a WPF application 2. Add the below mentioned code in the App.xaml.cs page. [code lang=&#8221;c&#8221;] private static string ClientId = &#8220;0b8b0665-bc13-4fdc-bd72-e0227b9fc011&#8243;; private static PublicClientApplication _clientApp ; public static PublicClientApplication PublicClientApp { get { return _clientApp; } } static App() { _clientApp = new PublicClientApplication(ClientId); }[\/code] 3.In mainwindow.xaml Add a button for calling the graph and a multiline textbox to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/\" \/>\n<meta property=\"og:site_name\" content=\"Softree Technology\" \/>\n<meta property=\"article:published_time\" content=\"2018-10-12T09:24:24+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Get members from Office 365 Azure Ad using Graph API - Softree Technology","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/","og_locale":"en_US","og_type":"article","og_title":"Get members from Office 365 Azure Ad using Graph API - Softree Technology","og_description":"1.Create a WPF application 2. Add the below mentioned code in the App.xaml.cs page. [code lang=&#8221;c&#8221;] private static string ClientId = &#8220;0b8b0665-bc13-4fdc-bd72-e0227b9fc011&#8243;; private static PublicClientApplication _clientApp ; public static PublicClientApplication PublicClientApp { get { return _clientApp; } } static App() { _clientApp = new PublicClientApplication(ClientId); }[\/code] 3.In mainwindow.xaml Add a button for calling the graph and a multiline textbox to [&hellip;]","og_url":"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/","og_site_name":"Softree Technology","article_published_time":"2018-10-12T09:24:24+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/#article","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/"},"author":{"name":"admin","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b"},"headline":"Get members from Office 365 Azure Ad using Graph API","datePublished":"2018-10-12T09:24:24+00:00","mainEntityOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/"},"wordCount":486,"commentCount":0,"publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2018\/10\/Azure-Ad.png","keywords":["Azure ad graph api example","Azure ad graph api sample","Azure graph api example","Azure graph api tutorial","How to use the Azure AD Graph API","office 365"],"articleSection":["Azure Ad"],"inLanguage":"en","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/","url":"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/","name":"Get members from Office 365 Azure Ad using Graph API - Softree Technology","isPartOf":{"@id":"https:\/\/softreetechnology.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/#primaryimage"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/#primaryimage"},"thumbnailUrl":"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2018\/10\/Azure-Ad.png","datePublished":"2018-10-12T09:24:24+00:00","breadcrumb":{"@id":"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/"]}]},{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/#primaryimage","url":"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2018\/10\/Azure-Ad.png","contentUrl":"https:\/\/blog.softreeconsulting.com\/wp-content\/uploads\/2018\/10\/Azure-Ad.png"},{"@type":"BreadcrumbList","@id":"https:\/\/softreetechnology.com\/blog\/azure-ad\/get-members-office365-azure-ad-using-graph-api\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/softreetechnology.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Get members from Office 365 Azure Ad using Graph API"}]},{"@type":"WebSite","@id":"https:\/\/softreetechnology.com\/blog\/#website","url":"https:\/\/softreetechnology.com\/blog\/","name":"Softree Technology","description":"Celebrating 10+ Years in SharePoint Consulting !","publisher":{"@id":"https:\/\/softreetechnology.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/softreetechnology.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en"},{"@type":"Organization","@id":"https:\/\/softreetechnology.com\/blog\/#organization","name":"Softree Technology","url":"https:\/\/softreetechnology.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","contentUrl":"https:\/\/softreetechnology.com\/blog\/wp-content\/uploads\/2023\/03\/cropped-white-logo-soft.png","width":844,"height":230,"caption":"Softree Technology"},"image":{"@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/98740297642f06debccdcee2de84086b","name":"admin","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/softreetechnology.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6fc78c8a7aa3fb0bf43c3b9a2e3962d7?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/softreeconsulting.com"],"url":"https:\/\/softreetechnology.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/710","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=710"}],"version-history":[{"count":0,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/posts\/710\/revisions"}],"wp:attachment":[{"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/softreetechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}