Skip to content

Commit 034ee33

Browse files
authored
Merge pull request #4791 from KoenZomers/LogGroupVisibilityInNewGroup
Removed `New-PnPMicrosoft365Group` setting the group visibility twice and adding logging around trying to set the group visibility
2 parents 49808ba + 0bbc3a1 commit 034ee33

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
9090
- `Get-PnPCustomAction` now supports a completer for `-Identity` and uses the PnP Core SDK to return custom actions.
9191
- `Set-PnPPropertyBagValue` and `Remove-PnPPropertyBagValue` now toggle the NoScript status of the site to allow setting/removing property bag values, but only if the tenant wide `AllowWebPropertyBagUpdateWhenDenyAddAndCustomizePagesIsEnabled` is not enabled [#4680](https://github.com/pnp/powershell/pull/4680)
9292
- `Get-PnPTenant` now uses nullable types for the properties that can return null if the property is not set or could not be retrieved. Beware that the property `PublicCdnOrigins` has been renamed to `PublicCdnOriginParsed `. All other property names will remain the same. [#4722](https://github.com/pnp/powershell/pull/4722)
93+
- Removed `New-PnPMicrosoft365Group` setting the group visibility options twice when providing `-HideFromAddressLists` and/or `-HideFromOutlookClients` and adding logging around trying to set the group visibility [#4791](https://github.com/pnp/powershell/pull/4791)
9394

9495
### Fixed
9596

src/Commands/Microsoft365Groups/NewMicrosoft365Group.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,6 @@ protected override void ExecuteCmdlet()
175175

176176
var group = Microsoft365GroupsUtility.Create(GraphRequestHelper, newGroup, CreateTeam, LogoPath, Owners, Members, HideFromAddressLists, HideFromOutlookClients, Labels);
177177

178-
if (ParameterSpecified(nameof(HideFromAddressLists)) || ParameterSpecified(nameof(HideFromOutlookClients)))
179-
{
180-
Microsoft365GroupsUtility.SetVisibility(GraphRequestHelper, group.Id.Value, HideFromAddressLists, HideFromOutlookClients);
181-
}
182-
183178
var updatedGroup = Microsoft365GroupsUtility.GetGroup(GraphRequestHelper, group.Id.Value, true, false, false, true);
184179

185180
WriteObject(updatedGroup);

src/Commands/Utilities/Microsoft365GroupsUtility.cs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using PnP.Framework.Diagnostics;
12
using PnP.PowerShell.Commands.Model;
23
using PnP.PowerShell.Commands.Utilities.REST;
34
using System;
@@ -699,33 +700,42 @@ internal static Microsoft365Group Update(ApiRequestHelper requestHelper, Microso
699700
return requestHelper.Patch($"v1.0/groups/{group.Id}", group);
700701
}
701702

703+
/// <summary>
704+
/// Allows to set the visibility of a group to hideFromAddressLists and hideFromOutlookClients.
705+
/// </summary>
702706
internal static void SetVisibility(ApiRequestHelper requestHelper, Guid groupId, bool? hideFromAddressLists, bool? hideFromOutlookClients)
703707
{
704-
var patchData = new
705-
{
706-
hideFromAddressLists = hideFromAddressLists,
707-
hideFromOutlookClients = hideFromOutlookClients
708-
};
708+
var attempt = 1;
709+
var maxRetries = 10;
710+
var retryAfterSeconds = 5;
709711

710-
var retry = true;
711-
var iteration = 0;
712-
while (retry)
712+
while (true)
713713
{
714714
try
715715
{
716-
requestHelper.Patch<dynamic>($"v1.0/groups/{groupId}", patchData);
717-
retry = false;
718-
}
716+
requestHelper.Patch<dynamic>($"v1.0/groups/{groupId}", new
717+
{
718+
hideFromAddressLists,
719+
hideFromOutlookClients
720+
});
719721

720-
catch (Exception)
721-
{
722-
Thread.Sleep(5000);
723-
iteration++;
722+
// Request successful, exit the loop
723+
break;
724724
}
725-
726-
if (iteration > 10) // don't try more than 10 times
725+
catch (Exception e)
727726
{
728-
retry = false;
727+
if (attempt == maxRetries)
728+
{
729+
Log.Warning("Microsoft365GroupsUtility.SetVisibility", $"Failed to set the visibility of the group {groupId} to hideFromAddressLists: {hideFromAddressLists} and hideFromOutlookClients: {hideFromOutlookClients}. Exception: {e.Message}. Giving up after {maxRetries} attempts.");
730+
break;
731+
}
732+
else
733+
{
734+
Log.Debug("Microsoft365GroupsUtility.SetVisibility", $"Failed to set the visibility of the group {groupId} to hideFromAddressLists: {hideFromAddressLists} and hideFromOutlookClients: {hideFromOutlookClients}. Exception: {e.Message}. Retrying in {retryAfterSeconds} seconds. Attempt {attempt} out of {maxRetries}.");
735+
}
736+
737+
Thread.Sleep(TimeSpan.FromSeconds(retryAfterSeconds));
738+
attempt++;
729739
}
730740
}
731741
}

0 commit comments

Comments
 (0)