示例#1
0
        internal static async Task <IEnumerable <Microsoft365Group> > GetGroupsAsync(HttpClient httpClient, string accessToken, bool includeSiteUrl, bool includeOwners)
        {
            var items  = new List <Microsoft365Group>();
            var result = await GraphHelper.GetAsync <RestResultCollection <Microsoft365Group> >(httpClient, "v1.0/groups", accessToken);

            if (result != null && result.Items.Any())
            {
                items.AddRange(result.Items);
                while (!string.IsNullOrEmpty(result.NextLink))
                {
                    result = await GraphHelper.GetAsync <RestResultCollection <Microsoft365Group> >(httpClient, result.NextLink, accessToken);

                    if (result != null && result.Items.Any())
                    {
                        items.AddRange(result.Items);
                    }
                }
            }
            if (includeSiteUrl || includeOwners)
            {
                var chunks = BatchUtility.Chunk(items.Select(g => g.Id.ToString()), 20);
                if (includeOwners)
                {
                    foreach (var chunk in chunks)
                    {
                        var ownerResults = await BatchUtility.GetObjectCollectionBatchedAsync <Microsoft365User>(httpClient, accessToken, chunk.ToArray(), "/groups/{0}/owners");

                        foreach (var ownerResult in ownerResults)
                        {
                            items.First(i => i.Id.ToString() == ownerResult.Key).Owners = ownerResult.Value;
                        }
                    }
                }

                if (includeSiteUrl)
                {
                    foreach (var chunk in chunks)
                    {
                        var results = await BatchUtility.GetPropertyBatchedAsync(httpClient, accessToken, chunk.ToArray(), "/groups/{0}/sites/root", "webUrl");

                        //var results = await GetSiteUrlBatchedAsync(httpClient, accessToken, chunk.ToArray());
                        foreach (var batchResult in results)
                        {
                            items.First(i => i.Id.ToString() == batchResult.Key).SiteUrl = batchResult.Value;
                        }
                    }
                }
            }
            return(items);
        }