using library_spo_utils.Constants; using library_spo_utils.Interfaces.Services; using Microsoft.Extensions.Logging; using Microsoft.SharePoint.Client; namespace library_spo_utils.Services; internal class ProjectDocLibraryService : IProjectDocLibraryService { private readonly ILogger logger; private readonly IRightsService rightsService; public ProjectDocLibraryService( ILogger logger, IRightsService rightsService) { this.logger = logger; this.rightsService = rightsService; } public void Create(string ssProjectTitle, ClientContext ctx) { try { BuildProjectDocumentLibrary(ssProjectTitle, ctx); BuildProjectFolders(ssProjectTitle, ctx); } catch (Exception ex) { logger.LogError(ex, "Create Doc Lib"); } } private void BuildProjectFolders(string projName, ClientContext clientContext) { var projectFolders = Folders.GetProjectFolders(projName); foreach (var (folderName, folderPath) in projectFolders) { logger.LogInformation($"> Inizializzata la fase di {folderName} in {folderPath}."); var list = clientContext.Web.Lists.GetByTitle(folderPath); var info = new ListItemCreationInformation { UnderlyingObjectType = FileSystemObjectType.Folder, LeafName = folderName }; var newItem = list.AddItem(info); newItem["Title"] = folderName; newItem.Update(); clientContext.ExecuteQuery(); logger.LogInformation($"> {folderName} creato con successo in {folderPath}."); } } private void BuildProjectDocumentLibrary(string projName, ClientContext clientContext) { var docLibNames = Folders.GetProjectDocLib(projName); foreach (var (libraryName, libraryType) in docLibNames) { logger.LogInformation($"> Inizializzata la fase di creazione '{libraryName}'."); var web = clientContext.Web; clientContext.Load(web); var lci = new ListCreationInformation { Title = libraryName, TemplateType = (int)libraryType }; web.Lists.Add(lci); clientContext.ExecuteQuery(); if (libraryName.Contains("Commerciale")) { rightsService.DomainGroupRights(clientContext, string.Empty, libraryName); } logger.LogInformation($"> '{libraryName}' è stato creato con successo."); } } }