Files

37 lines
1.4 KiB
C#

using Microsoft.SharePoint.Client;
using SharepointToolbox.Web.Core.Helpers;
using SharepointToolbox.Web.Core.Models;
using SharepointToolbox.Web.Services.Session;
namespace SharepointToolbox.Web.Services;
public class LibraryDiscoveryService : ILibraryDiscoveryService
{
private readonly IElevationCoordinator _elevation;
private readonly ISessionManager _sessionManager;
public LibraryDiscoveryService(IElevationCoordinator elevation, ISessionManager sessionManager)
{
_elevation = elevation;
_sessionManager = sessionManager;
}
public async Task<IReadOnlyList<string>> ListLibrariesAsync(
TenantProfile profile, string siteUrl, CancellationToken ct = default)
{
if (string.IsNullOrWhiteSpace(siteUrl)) return Array.Empty<string>();
return await _elevation.RunAsync(async c =>
{
var ctx = await _sessionManager.GetOrCreateContextAsync(siteUrl, profile, c);
ctx.Load(ctx.Web, w => w.Lists.Include(l => l.Title, l => l.Hidden, l => l.BaseType));
await ExecuteQueryRetryHelper.ExecuteQueryRetryAsync(ctx, null, c);
return (IReadOnlyList<string>)ctx.Web.Lists
.Where(l => !l.Hidden && l.BaseType == BaseType.DocumentLibrary)
.Select(l => l.Title)
.OrderBy(t => t, StringComparer.OrdinalIgnoreCase)
.ToList();
}, ct);
}
}