d69c3290d8
Reviewed-on: #2
116 lines
4.9 KiB
Plaintext
116 lines
4.9 KiB
Plaintext
@page "/bulk-sites"
|
|
@attribute [Authorize]
|
|
@inject IUserSessionService Session
|
|
@inject IUserContextAccessor UserContext
|
|
@inject ISessionManager SessionMgr
|
|
@inject IBulkSiteService BulkSvc
|
|
@inject ICsvValidationService CsvValidation
|
|
@inject BulkResultCsvExportService ExportSvc
|
|
@inject WebExportService WebExport
|
|
@rendermode InteractiveServer
|
|
|
|
<h1 class="page-title">Bulk Site Creation</h1>
|
|
<p class="page-subtitle">Create multiple SharePoint sites from a CSV file.</p>
|
|
|
|
@if (!Session.HasProfile) { <NoProfilePrompt /> return; }
|
|
@if (UserContext.Role < UserRole.TechN1) { <WriteGuard /> return; }
|
|
|
|
<div class="card">
|
|
<div class="form-group">
|
|
<label class="form-label">Admin Center URL</label>
|
|
<input class="form-input" @bind="_adminUrl" placeholder="https://contoso-admin.sharepoint.com" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label class="form-label">CSV File (Name, Alias, Type, Template, Owners, Members)</label>
|
|
<InputFile OnChange="LoadFile" accept=".csv" class="form-input" />
|
|
</div>
|
|
|
|
@if (_rows.Count > 0)
|
|
{
|
|
<div class="alert alert-info mt-8">@_rows.Count(r => r.IsValid) valid, @_rows.Count(r => !r.IsValid) errors.</div>
|
|
<div class="data-table-wrap" style="max-height:200px;overflow-y:auto">
|
|
<table class="data-table">
|
|
<thead><tr><th>Name</th><th>Type</th><th>Alias</th><th>Status</th></tr></thead>
|
|
<tbody>
|
|
@foreach (var row in _rows.Take(50))
|
|
{
|
|
<tr class="@(row.IsValid ? "val-valid" : "val-error")">
|
|
<td>@(row.Record?.Name ?? "—")</td>
|
|
<td>@(row.Record?.Type ?? "—")</td>
|
|
<td>@(row.Record?.Alias ?? "—")</td>
|
|
<td>@(row.IsValid ? "✓" : string.Join("; ", row.Errors))</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
}
|
|
|
|
<div class="flex-row mt-8">
|
|
<button class="btn btn-primary" @onclick="RunBulk" disabled="@(_running || _rows.Count == 0 || _rows.All(r => !r.IsValid))">
|
|
@(_running ? "Creating…" : "Create Sites")
|
|
</button>
|
|
@if (_running) { <button class="btn btn-secondary" @onclick="Cancel">Cancel</button> }
|
|
</div>
|
|
<ProgressPanel IsRunning="_running" StatusMessage="@_status" Current="_current" Total="_total" />
|
|
</div>
|
|
|
|
@if (!string.IsNullOrEmpty(_error)) { <div class="alert alert-error">@_error</div> }
|
|
|
|
@if (_summary != null)
|
|
{
|
|
<div class="card">
|
|
<div class="alert @(_summary.HasFailures ? "alert-warn" : "alert-success")">
|
|
Created: @_summary.SuccessCount / @_summary.TotalCount. Failures: @_summary.FailedCount
|
|
</div>
|
|
@if (_summary.HasFailures)
|
|
{
|
|
<button class="btn btn-secondary btn-sm mt-8" @onclick="ExportErrors">Export Errors CSV</button>
|
|
}
|
|
</div>
|
|
}
|
|
|
|
@code {
|
|
private string _adminUrl = string.Empty;
|
|
private List<CsvValidationRow<BulkSiteRow>> _rows = new();
|
|
private bool _running; private string _status = string.Empty, _error = string.Empty;
|
|
private int _current, _total;
|
|
private BulkOperationSummary<BulkSiteRow>? _summary;
|
|
private CancellationTokenSource? _cts;
|
|
|
|
private async Task LoadFile(InputFileChangeEventArgs e)
|
|
{
|
|
_rows.Clear();
|
|
using var stream = e.File.OpenReadStream(maxAllowedSize: 10 * 1024 * 1024);
|
|
_rows = CsvValidation.ParseAndValidateSites(stream);
|
|
}
|
|
|
|
private async Task RunBulk()
|
|
{
|
|
_error = string.Empty; _summary = null; _running = true;
|
|
_cts = new CancellationTokenSource();
|
|
var validRows = _rows.Where(r => r.IsValid && r.Record != null).Select(r => r.Record!).ToList();
|
|
var adminUrl = string.IsNullOrWhiteSpace(_adminUrl)
|
|
? Session.CurrentProfile!.TenantUrl.Replace(".sharepoint.com", "-admin.sharepoint.com")
|
|
: _adminUrl.Trim();
|
|
var progress = new Progress<OperationProgress>(p => { _status = p.Message; _current = p.Current; _total = p.Total; InvokeAsync(StateHasChanged); });
|
|
try
|
|
{
|
|
var ctx = await SessionMgr.GetOrCreateContextAsync(adminUrl, Session.CurrentProfile!, _cts.Token);
|
|
_summary = await BulkSvc.CreateSitesAsync(ctx, validRows, progress, _cts.Token);
|
|
_status = $"Complete: {_summary.SuccessCount} created, {_summary.FailedCount} failed.";
|
|
}
|
|
catch (OperationCanceledException) { _status = "Cancelled."; }
|
|
catch (Exception ex) { _error = ex.Message; }
|
|
finally { _running = false; await InvokeAsync(StateHasChanged); }
|
|
}
|
|
|
|
private void Cancel() => _cts?.Cancel();
|
|
private async Task ExportErrors()
|
|
{
|
|
if (_summary == null) return;
|
|
var csv = ExportSvc.BuildFailedItemsCsv(_summary.Results.ToList());
|
|
await WebExport.DownloadCsvAsync(csv, $"bulk_sites_errors_{DateTime.Now:yyyyMMdd_HHmmss}.csv");
|
|
}
|
|
}
|