Skip to content

Commit f9c5dad

Browse files
authored
Refactor site creation logic to improve timeout handling and ensure proper site properties update (#5185)
* Refactor site creation logic to improve timeout handling and ensure proper site properties update * Refactor sharing capability handling to improve timeout management and encapsulate logic in a dedicated method * Fix `New-PnPTenantSite` cmdlet to improve timeout handling during site creation operations.
1 parent 1653f5c commit f9c5dad

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
5353
- Fixed issue with `Set-PnPSite -HidePeoplePreviewingFiles $true` and `Set-PnPSite -HidePeopleWhoHaveListsOpen $true` added missing properties [#5003](https://github.com/pnp/powershell/issues/5003)
5454
- Fixed issue with `New-PnpAzureCertificate -OutPfx pnp.pfx -OutCert pnp.cer` made it language agnostic [#5137](https://github.com/pnp/powershell/issues/5137)
5555
- Fix `Register-PnPEntraIdApp` Allow to use CER file instead of PFX [#5129](https://github.com/pnp/powershell/issues/5129)
56+
- Fix `New-PnPTenantSite` cmdlet to properly handle timeout issues for classic sites. [#5185](https://github.com/pnp/powershell/pull/5185)
5657

5758
### Removed
5859
- Removed `-RemoveExisting` parameter from `Add-PnPAzureADGroupMember`, `Add-PnPAzureADGroupOwner`, `Add-PnPMicrosoft365GroupMember` and `Add-PnPMicrosoft365GroupOwner` cmdlets. It was never really implemented and without function. [#5153](https://github.com/pnp/powershell/pull/5153)

src/Commands/Admin/NewTenantSite.cs

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.Online.SharePoint.TenantManagement;
1+
using Microsoft.Online.SharePoint.TenantAdministration;
2+
using Microsoft.Online.SharePoint.TenantManagement;
23
using Microsoft.SharePoint.Client;
34
using PnP.Framework;
45
using PnP.PowerShell.Commands.Base;
@@ -59,26 +60,64 @@ protected override void ExecuteCmdlet()
5960
Uri uri = BaseUri;
6061
Url = $"{uri.ToString().TrimEnd('/')}/{Url.TrimStart('/')}";
6162
}
62-
Func<TenantOperationMessage, bool> timeoutFunction = TimeoutFunction;
6363

64-
Guid newSiteId = Tenant.CreateSiteCollection(Url, Title, Owner, Template, (int)StorageQuota,
65-
(int)StorageQuotaWarningLevel, TimeZone, (int)ResourceQuota, (int)ResourceQuotaWarningLevel, Lcid,
66-
RemoveDeletedSite, Wait, Wait == true ? timeoutFunction : null);
67-
68-
if (newSiteId != Guid.Empty && Wait && SharingCapability.HasValue)
64+
if (ParameterSpecified(nameof(RemoveDeletedSite)))
6965
{
70-
var props = Tenant.GetSitePropertiesByUrl(Url, true);
71-
Tenant.Context.Load(props);
72-
Tenant.Context.ExecuteQueryRetry();
66+
Func<TenantOperationMessage, bool> timeoutFunction = TimeoutFunction;
7367

74-
props.SharingCapability = SharingCapability.Value;
68+
Guid newSiteId = Tenant.CreateSiteCollection(Url, Title, Owner, Template, (int)StorageQuota,
69+
(int)StorageQuotaWarningLevel, TimeZone, (int)ResourceQuota, (int)ResourceQuotaWarningLevel, Lcid,
70+
RemoveDeletedSite, Wait, Wait == true ? timeoutFunction : null);
7571

76-
var op = props.Update();
77-
AdminContext.Load(op, i => i.IsComplete, i => i.PollingInterval);
72+
if (newSiteId != Guid.Empty && Wait && SharingCapability.HasValue)
73+
{
74+
SetSharingCapability(Url, SharingCapability.Value);
75+
}
76+
}
77+
else
78+
{
79+
SiteCreationProperties siteCreationProperties = new SiteCreationProperties
80+
{
81+
Url = Url,
82+
Owner = Owner,
83+
Title = Title,
84+
Template = Template,
85+
StorageMaximumLevel = StorageQuota,
86+
StorageWarningLevel = StorageQuotaWarningLevel,
87+
TimeZoneId = TimeZone,
88+
UserCodeMaximumLevel = ResourceQuota,
89+
UserCodeWarningLevel = ResourceQuotaWarningLevel,
90+
Lcid = Lcid
91+
};
92+
93+
SpoOperation spoOperation = Tenant.CreateSite(siteCreationProperties);
94+
AdminContext.Load(spoOperation, s => s.IsComplete, s => s.PollingInterval, s => s.HasTimedout);
7895
AdminContext.ExecuteQueryRetry();
96+
97+
if (Wait)
98+
{
99+
PollOperation(spoOperation);
100+
}
101+
102+
if (Wait && SharingCapability.HasValue)
103+
{
104+
SetSharingCapability(Url, SharingCapability.Value);
105+
}
79106
}
80107
}
81108

109+
private void SetSharingCapability(string url, SharingCapabilities sharingCapability)
110+
{
111+
var props = Tenant.GetSitePropertiesByUrl(url, true);
112+
Tenant.Context.Load(props);
113+
Tenant.Context.ExecuteQueryRetry();
114+
115+
props.SharingCapability = sharingCapability;
116+
var op = props.Update();
117+
AdminContext.Load(op, i => i.IsComplete, i => i.PollingInterval);
118+
AdminContext.ExecuteQueryRetry();
119+
}
120+
82121
private bool TimeoutFunction(TenantOperationMessage message)
83122
{
84123
if (message == TenantOperationMessage.CreatingSiteCollection)

src/Commands/Base/PnPSharePointCmdlet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ protected void PollOperation(SpoOperation spoOperation)
186186
}
187187

188188
LogDebug("Checking for operation status");
189-
ClientContext.Load(spoOperation);
190-
ClientContext.ExecuteQueryRetry();
189+
spoOperation.Context.Load(spoOperation, s => s.IsComplete, s => s.HasTimedout, s => s.PollingInterval);
190+
spoOperation.Context.ExecuteQueryRetry();
191191
}
192192
LogWarning("SharePoint Operation Wait Interrupted");
193193
}

0 commit comments

Comments
 (0)