|
| 1 | +using PnP.Framework.Diagnostics; |
1 | 2 | using PnP.PowerShell.Commands.Model;
|
2 | 3 | using PnP.PowerShell.Commands.Utilities.REST;
|
3 | 4 | using System;
|
@@ -699,33 +700,42 @@ internal static Microsoft365Group Update(ApiRequestHelper requestHelper, Microso
|
699 | 700 | return requestHelper.Patch($"v1.0/groups/{group.Id}", group);
|
700 | 701 | }
|
701 | 702 |
|
| 703 | + /// <summary> |
| 704 | + /// Allows to set the visibility of a group to hideFromAddressLists and hideFromOutlookClients. |
| 705 | + /// </summary> |
702 | 706 | internal static void SetVisibility(ApiRequestHelper requestHelper, Guid groupId, bool? hideFromAddressLists, bool? hideFromOutlookClients)
|
703 | 707 | {
|
704 |
| - var patchData = new |
705 |
| - { |
706 |
| - hideFromAddressLists = hideFromAddressLists, |
707 |
| - hideFromOutlookClients = hideFromOutlookClients |
708 |
| - }; |
| 708 | + var attempt = 1; |
| 709 | + var maxRetries = 10; |
| 710 | + var retryAfterSeconds = 5; |
709 | 711 |
|
710 |
| - var retry = true; |
711 |
| - var iteration = 0; |
712 |
| - while (retry) |
| 712 | + while (true) |
713 | 713 | {
|
714 | 714 | try
|
715 | 715 | {
|
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 | + }); |
719 | 721 |
|
720 |
| - catch (Exception) |
721 |
| - { |
722 |
| - Thread.Sleep(5000); |
723 |
| - iteration++; |
| 722 | + // Request successful, exit the loop |
| 723 | + break; |
724 | 724 | }
|
725 |
| - |
726 |
| - if (iteration > 10) // don't try more than 10 times |
| 725 | + catch (Exception e) |
727 | 726 | {
|
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++; |
729 | 739 | }
|
730 | 740 | }
|
731 | 741 | }
|
|
0 commit comments