Skip to content

Commit

Permalink
#2319 - Fix New-PnPMicrosoft365Group cmdlet (#2349)
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamdsheth authored Sep 15, 2022
1 parent fa9e8c7 commit 530c96f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added support for `-ErrorAction:Stop` to PnP PowerShell cmdlets. Notice that if you were using this in combination with the specific try/catch [System.Management.Automation.PSInvalidOperationException], it will no longer catch the exception. It will throw an `System.Management.Automation.ErrorRecord` exception instead. Remove the `-ErrorAction:Stop` parameter from your cmdlet or catch this new exception type to avoid this behavior. [#2288](https://github.com/pnp/powershell/pull/2288)
- Added ability to create shared Teams channels using `Add-PnPTeamsChannel -ChannelType Shared` [#2308](https://github.com/pnp/powershell/pull/2308)
- Added support for `IsLoopEnabled` properties in `Get-PnPTenant` and `Set-PnPTenant` cmdlets to to enable/disable loop components in the tenant. [#2307](https://github.com/pnp/powershell/pull/2307)
- Added support for `SubscribeMembersToCalendarEventsDisabled` resource behavior option in `New-PnPMicrosoft365Group` and `New-PnPTeamsTeam` cmdlet.

### Changed
- Changed to no longer require `https://` to be prefixed when using `Connect-PnPOnline -Url tenant.sharepoint.com` [#2139](https://github.com/pnp/powershell/pull/2139)
Expand All @@ -39,7 +40,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed `Get-PnPAccessToken` cmdlet to be able to work with different site collections as well as require an actual connection first. [#2270](https://github.com/pnp/powershell/pull/2270)
- Fixed `Copy-PnPList` cmdlet to be able to copy the list structure to the destination web. [#2313](https://github.com/pnp/powershell/pull/2313)
- Fixed `Add-PnPField` cmdlet , it was throwing null reference error when `-Type` was not specified and after the prompt you entered the correct type. [#2338](https://github.com/pnp/powershell/pull/2338)

- Fixed regression issue with `New-Microsoft365Group` cmdlet.
### Contributors

- Marc Studer [Studermarc]
Expand Down
7 changes: 6 additions & 1 deletion src/Commands/Enums/TeamResourceBehaviorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public enum TeamResourceBehaviorOptions
/// <summary>
/// Do not send out the out of the box welcome e-mail to members getting added to the Microsoft Teams team
/// </summary>
WelcomeEmailDisabled
WelcomeEmailDisabled,

/// <summary>
/// Members are not subscribed to the group's calendar events in Outlook.
/// </summary>
SubscribeMembersToCalendarEventsDisabled
}
}
47 changes: 22 additions & 25 deletions src/Commands/Utilities/Microsoft365GroupsUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ internal static async Task<IEnumerable<Microsoft365Group>> GetGroupsAsync(PnPCon
}
return items;
}

private static async Task<Microsoft365Group> GetGroupFilteredAsync(string filter, PnPConnection connection, string accessToken, bool includeSiteUrl, bool includeOwners)
internal static async Task<Microsoft365Group> GetGroupAsync(PnPConnection connection, Guid groupId, 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 {filter}", accessToken);
var results = await GraphHelper.GetAsync<RestResultCollection<Microsoft365Group>>(connection, $"v1.0/groups?$filter=groupTypes/any(c:c+eq+'Unified') and id eq '{groupId}'", accessToken);

if (results != null && results.Items.Any())
{
var group = results.Items.First();
Expand Down Expand Up @@ -99,33 +99,30 @@ private static async Task<Microsoft365Group> GetGroupFilteredAsync(string filter
}
return group;
}

throw new Exception();
}

internal static async Task<Microsoft365Group> GetGroupAsync(PnPConnection connection, Guid groupId, string accessToken, bool includeSiteUrl, bool includeOwners)
{
try
{
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}'");
}
return null;
}

internal static async Task<Microsoft365Group> GetGroupAsync(PnPConnection connection, string displayName, string accessToken, bool includeSiteUrl, bool includeOwners)
{

try
{
return await GetGroupFilteredAsync($"(displayName eq '{displayName}' or mailNickName eq '{displayName}')", connection, accessToken, includeSiteUrl, includeOwners);
}
catch (Exception)
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())
{
throw new Exception($"No Microsoft 365 group found with id, display name or mail nickname '{displayName}'");
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 null;
}

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

0 comments on commit 530c96f

Please sign in to comment.