Skip to content

Commit c96b19d

Browse files
committed
Adding logging and code cleanup around setting the Group Visibility to get insights into why it would fail to patch the visibility settings
1 parent eca92b1 commit c96b19d

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

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)