Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check if group is M365 group to the Get-PnPMicrosoft365Group command #2258

Merged
merged 3 commits into from
Aug 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 57 additions & 48 deletions src/Commands/Utilities/Microsoft365GroupsUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,73 +50,82 @@ internal static async Task<IEnumerable<Microsoft365Group>> GetGroupsAsync(PnPCon
}
return items;
}
internal static async Task<Microsoft365Group> GetGroupAsync(PnPConnection connection, Guid groupId, string accessToken, bool includeSiteUrl, bool includeOwners)

private static async Task<Microsoft365Group> GetGroupFilteredAsync(string filter, PnPConnection connection, string accessToken, bool includeSiteUrl, bool includeOwners)
{
var group = await GraphHelper.GetAsync<Microsoft365Group>(connection, $"v1.0/groups/{groupId}", accessToken);
if (includeSiteUrl)
var results = await GraphHelper.GetAsync<RestResultCollection<Microsoft365Group>>(connection, $"v1.0/groups?$filter=groupTypes/any(c:c+eq+'Unified') and {filter}", accessToken);
if (results != null && results.Items.Any())
{
bool wait = true;
var iterations = 0;

while (wait)
var group = results.Items.First();
if (includeSiteUrl)
{
iterations++;
try
bool wait = true;
var iterations = 0;

while (wait)
{
var siteUrlResult = await GraphHelper.GetAsync(connection, $"v1.0/groups/{group.Id}/sites/root?$select=webUrl", accessToken);
if (!string.IsNullOrEmpty(siteUrlResult))
iterations++;
try
{
wait = false;
var resultElement = JsonSerializer.Deserialize<JsonElement>(siteUrlResult);
if (resultElement.TryGetProperty("webUrl", out JsonElement webUrlElement))
var siteUrlResult = await GraphHelper.GetAsync(connection, $"v1.0/groups/{group.Id}/sites/root?$select=webUrl", accessToken);
if (!string.IsNullOrEmpty(siteUrlResult))
{
group.SiteUrl = webUrlElement.GetString();
wait = false;
var resultElement = JsonSerializer.Deserialize<JsonElement>(siteUrlResult);
if (resultElement.TryGetProperty("webUrl", out JsonElement webUrlElement))
{
group.SiteUrl = webUrlElement.GetString();
}
break;
}
break;
}
}
catch (Exception)
{
if (iterations * 30 >= 300)
{
wait = false;
throw;
}
else
catch (Exception)
{
await Task.Delay(TimeSpan.FromSeconds(30));
if (iterations * 30 >= 300)
{
wait = false;
throw;
}
else
{
await Task.Delay(TimeSpan.FromSeconds(30));
}
}
}
}
if (includeOwners)
{
group.Owners = await GetGroupMembersAsync("owners", connection, group.Id.Value, accessToken);
}
return group;
}
if (includeOwners)

throw new Exception();
}

internal static async Task<Microsoft365Group> GetGroupAsync(PnPConnection connection, Guid groupId, string accessToken, bool includeSiteUrl, bool includeOwners)
{
try
{
group.Owners = await GetGroupMembersAsync("owners", connection, group.Id.Value, accessToken);
}
return group;
return await GetGroupFilteredAsync($"(id eq '{groupId}' or displayName eq '{groupId}' or mailNickName eq '{groupId}')", connection, accessToken, includeSiteUrl, includeOwners);
}
catch (Exception)
{
throw new Exception($"No Microsoft 365 group found with id, display name or mail nickname '{groupId}'");
}
}

internal static async Task<Microsoft365Group> GetGroupAsync(PnPConnection connection, string displayName, string accessToken, bool includeSiteUrl, bool includeOwners)
{
var results = await GraphHelper.GetAsync<RestResultCollection<Microsoft365Group>>(connection, $"v1.0/groups?$filter=groupTypes/any(c:c+eq+'Unified') and displayName eq '{displayName}' or mailNickName eq '{displayName}'", accessToken);
if (results != null && results.Items.Any())

try
{
var group = results.Items.First();
if (includeSiteUrl)
{
var siteUrlResult = await GraphHelper.GetAsync(connection, $"v1.0/groups/{group.Id}/sites/root?$select=webUrl", accessToken);
var resultElement = JsonSerializer.Deserialize<JsonElement>(siteUrlResult);
if (resultElement.TryGetProperty("webUrl", out JsonElement webUrlElement))
{
group.SiteUrl = webUrlElement.GetString();
}
}
if (includeOwners)
{
group.Owners = await GetGroupMembersAsync("owners", connection, group.Id.Value, accessToken);
}
return group;
return await GetGroupFilteredAsync($"(displayName eq '{displayName}' or mailNickName eq '{displayName}')", connection, accessToken, includeSiteUrl, includeOwners);
}
catch (Exception)
{
throw new Exception($"No Microsoft 365 group found with id, display name or mail nickname '{displayName}'");
}
return null;
}

internal static async Task<Microsoft365Group> GetDeletedGroupAsync(PnPConnection connection, Guid groupId, string accessToken)
Expand Down