Skip to content

Commit 96b62de

Browse files
committed
Refactor site creation logic to improve timeout handling and ensure proper site properties update
1 parent 1653f5c commit 96b62de

File tree

1 file changed

+72
-13
lines changed

1 file changed

+72
-13
lines changed

src/Commands/Admin/NewTenantSite.cs

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
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;
56
using System;
67
using System.Management.Automation;
8+
using System.Threading.Tasks;
79

810
namespace PnP.PowerShell.Commands
911
{
@@ -59,23 +61,80 @@ protected override void ExecuteCmdlet()
5961
Uri uri = BaseUri;
6062
Url = $"{uri.ToString().TrimEnd('/')}/{Url.TrimStart('/')}";
6163
}
62-
Func<TenantOperationMessage, bool> timeoutFunction = TimeoutFunction;
6364

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)
65+
if (ParameterSpecified(nameof(RemoveDeletedSite)))
6966
{
70-
var props = Tenant.GetSitePropertiesByUrl(Url, true);
71-
Tenant.Context.Load(props);
72-
Tenant.Context.ExecuteQueryRetry();
67+
Func<TenantOperationMessage, bool> timeoutFunction = TimeoutFunction;
68+
69+
Guid newSiteId = Tenant.CreateSiteCollection(Url, Title, Owner, Template, (int)StorageQuota,
70+
(int)StorageQuotaWarningLevel, TimeZone, (int)ResourceQuota, (int)ResourceQuotaWarningLevel, Lcid,
71+
RemoveDeletedSite, Wait, Wait == true ? timeoutFunction : null);
72+
73+
if (newSiteId != Guid.Empty && Wait && SharingCapability.HasValue)
74+
{
75+
var props = Tenant.GetSitePropertiesByUrl(Url, true);
76+
Tenant.Context.Load(props);
77+
Tenant.Context.ExecuteQueryRetry();
7378

74-
props.SharingCapability = SharingCapability.Value;
79+
props.SharingCapability = SharingCapability.Value;
7580

76-
var op = props.Update();
77-
AdminContext.Load(op, i => i.IsComplete, i => i.PollingInterval);
81+
var op = props.Update();
82+
AdminContext.Load(op, i => i.IsComplete, i => i.PollingInterval);
83+
AdminContext.ExecuteQueryRetry();
84+
}
85+
}
86+
else
87+
{
88+
SiteCreationProperties siteCreationProperties = new SiteCreationProperties
89+
{
90+
Url = Url,
91+
Owner = Owner,
92+
Title = Title,
93+
Template = Template,
94+
StorageMaximumLevel = StorageQuota,
95+
StorageWarningLevel = StorageQuotaWarningLevel,
96+
TimeZoneId = TimeZone,
97+
UserCodeMaximumLevel = ResourceQuota,
98+
UserCodeWarningLevel = ResourceQuotaWarningLevel,
99+
Lcid = Lcid
100+
};
101+
102+
SpoOperation spoOperation = Tenant.CreateSite(siteCreationProperties);
103+
AdminContext.Load(spoOperation, s => s.IsComplete, s => s.PollingInterval, s => s.HasTimedout);
78104
AdminContext.ExecuteQueryRetry();
105+
106+
if (Wait)
107+
{
108+
while (!spoOperation.IsComplete)
109+
{
110+
if (spoOperation.HasTimedout)
111+
{
112+
throw new TimeoutException("Wait for site creation operation to complete has timed out.");
113+
}
114+
Task.Delay(TimeSpan.FromMilliseconds(spoOperation.PollingInterval)).GetAwaiter().GetResult();
115+
spoOperation.RefreshLoad();
116+
if (((Cmdlet)this).Stopping)
117+
{
118+
((Cmdlet)this).WriteWarning("Cmdlet execution interrupted by user, stopping wait for site creation operation to complete.");
119+
break;
120+
}
121+
AdminContext.Load(spoOperation, s => s.IsComplete, s => s.PollingInterval, s => s.HasTimedout);
122+
AdminContext.ExecuteQueryRetry();
123+
}
124+
}
125+
126+
if (SharingCapability.HasValue)
127+
{
128+
var props = Tenant.GetSitePropertiesByUrl(Url, true);
129+
Tenant.Context.Load(props);
130+
Tenant.Context.ExecuteQueryRetry();
131+
132+
props.SharingCapability = SharingCapability.Value;
133+
134+
var op = props.Update();
135+
AdminContext.Load(op, i => i.IsComplete, i => i.PollingInterval);
136+
AdminContext.ExecuteQueryRetry();
137+
}
79138
}
80139
}
81140

0 commit comments

Comments
 (0)