title: Azure AD Graph API Group Search
body: <p>We have an office365 account with active directory on azure.
  I am making a search feature that queries our Azure AD with the given search term. Since the Graph API only supports .StartsWith(string) LINQ queries, I have to pull in all groups then query that collection with my search term.</p>
  
  <p>I am using the 'Get Group members' function from <a href="https://github.com/AzureADSamples/ConsoleApp-GraphAPI-DotNet/blob/master/GraphConsoleAppV3/Program.cs" rel="nofollow">this demo</a> as a guide to make my search function.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public List&lt;myModel&gt; SearchGroups(string term)
      {
          List&lt;myModel&gt; returnList = new List&lt;myModel&gt;();
  
         //my service root uri
          Uri serviceRoot = new Uri(serviceRootURL);
  
          //create the client and get authorization
          ActiveDirectoryClient adClient = new ActiveDirectoryClient(serviceRoot, async () =&gt; await GetAppTokenAsync());
  
          //get collection of IGroup
          IPagedCollection&lt;IGroup&gt; groups = adClient.Groups.ExecuteAsync().Result;
  
          //do while loop because groups are returned in paged list...
          do
          {
              List&lt;IGroup&gt; directoryObjects = groups.CurrentPage.ToList();
  
              //get groups that contain the search term
              foreach (IGroup item in directoryObjects.Where(x=&gt;x.DisplayName.ToLower().Contains(term.ToLower())))
              {
                  returnList.Add(new myModel(item as Microsoft.Azure.ActiveDirectory.GraphClient.Group));          
              }
              //get next page of results
              groups = groups.GetNextPageAsync().Result;
  
          } while (groups.MorePagesAvailable); //also tried while(groups != null) same issue
  
          return returnList;
      }
  </code></pre>
  
  <p>If I let it run, the code gets hung up and never returns anything and if I pause it, it is usually stuck on this line</p>
  
  <pre><code>groups = groups.GetNextPageAsync().Result;
  </code></pre>
  
  <p>If I put a break point and step through the code, it works perfectly fine so I am thinking it is an issue with the async methods. I just have no experience with async methods and the graph api docs are not that great in my opinion so I am stuck.</p>
  
  <p>Using: ASP.NET MVC, C#, azure active directory Graph API, Web API</p>