|
1 | | -using Microsoft.Online.SharePoint.TenantManagement; |
| 1 | +using Microsoft.Online.SharePoint.TenantAdministration; |
| 2 | +using Microsoft.Online.SharePoint.TenantManagement; |
2 | 3 | using Microsoft.SharePoint.Client; |
3 | 4 | using PnP.Framework; |
4 | 5 | using PnP.PowerShell.Commands.Base; |
5 | 6 | using System; |
6 | 7 | using System.Management.Automation; |
| 8 | +using System.Threading.Tasks; |
7 | 9 |
|
8 | 10 | namespace PnP.PowerShell.Commands |
9 | 11 | { |
@@ -59,23 +61,80 @@ protected override void ExecuteCmdlet() |
59 | 61 | Uri uri = BaseUri; |
60 | 62 | Url = $"{uri.ToString().TrimEnd('/')}/{Url.TrimStart('/')}"; |
61 | 63 | } |
62 | | - Func<TenantOperationMessage, bool> timeoutFunction = TimeoutFunction; |
63 | 64 |
|
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))) |
69 | 66 | { |
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(); |
73 | 78 |
|
74 | | - props.SharingCapability = SharingCapability.Value; |
| 79 | + props.SharingCapability = SharingCapability.Value; |
75 | 80 |
|
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); |
78 | 104 | 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 | + } |
79 | 138 | } |
80 | 139 | } |
81 | 140 |
|
|
0 commit comments