using library_spo_utils.Constants; using library_spo_utils.Enums; using library_spo_utils.Interfaces.Repositories; using library_spo_utils.Interfaces.Services; using Microsoft.Extensions.Logging; using Microsoft.SharePoint.Client; using Microsoft.SharePoint.Client.DocumentSet; namespace library_spo_utils.Services; internal class QuotationDocSetService : IQuotationDocSetService { private readonly ISharePointCustomOperation cpt; private readonly ILogger logger; private readonly IOneNoteService oneNoteService; private readonly IQuotationRepository quotationRepository; private readonly IFieldEntryDataUpdate _fieldEntryDataUpdate; public QuotationDocSetService(ISharePointCustomOperation cpt, ILogger logger, IOneNoteService oneNoteService, IQuotationRepository quotationRepository, IFieldEntryDataUpdate fieldEntryDataUpdate) { this.cpt = cpt; this.logger = logger; this.oneNoteService = oneNoteService; this.quotationRepository = quotationRepository; _fieldEntryDataUpdate = fieldEntryDataUpdate; } public void Create(string quotationName, string docLibraryName, string tenantName, ClientContext ctx) { try { #region Context var web = ctx.Web; ctx.Load(web, w => w.Url); List list = web.Lists.GetByTitle(docLibraryName); ctx.Load(list, l => l.RootFolder, l => l.ContentTypes, l => l.Fields, l => l.ContentTypesEnabled); ctx.ExecuteQuery(); list.ContentTypesEnabled = true; list.Update(); if (!cpt.ListContentTypeExist(ctx, docLibraryName, "Set di documenti")) { var documentCT = ctx.Site.RootWeb.AvailableContentTypes.GetById("0x0120D5"); ctx.Load(documentCT); ctx.ExecuteQuery(); var ctDocSet = new ContentTypeCreationInformation() { Name = "Set di documenti", ParentContentType = documentCT }; list.ContentTypes.Add(ctDocSet); list.Update(); ctx.ExecuteQuery(); } var ctData = list.ContentTypes.Where(c => c.Name == "Set di documenti"); var contentType = ctData.FirstOrDefault(); ctx.Load(contentType); ctx.ExecuteQuery(); #endregion Console.WriteLine($"> Inizializzata la fase di creazione per {quotationName}."); #region DocSet Field Entry DocumentSet.Create(ctx, list.RootFolder, quotationName, contentType.Id); ctx.ExecuteQuery(); Console.WriteLine($"> DocumentSet {quotationName} creata."); //var dsItem = list.RootFolder.Folders.GetByUrl(quotationName).ListItemAllFields; //dsItem["PAL_ID_Quotation"] = quotationRepository.DefaultIdQuotation(quotationName); //dsItem["PAL_Quotation_Name"] = quotationRepository.DefaultQuotationName(quotationName); //dsItem["PAL_Quotation_Reason"] = quotationRepository.DefaultQuotationReason(quotationName); //dsItem["PAL_Authors"] = quotationRepository.DefaultAuthors(quotationName); //dsItem["PAL_Status"] = quotationRepository.DefaultQuotationState(quotationName); //dsItem.Update(); //ctx.ExecuteQuery(); _fieldEntryDataUpdate.FieldUpdate(ctx, docLibraryName, quotationName, FieldUpdateType.Quotation); Console.WriteLine($"> Field value update."); #endregion #region Quotation Folder var revision = "Rev.00"; foreach (var name in Folders.OfferDocSet(revision)) { Console.WriteLine($"La sotto cartella {name} verrà creata in {quotationName}"); var rPath = ResourcePath.FromDecodedUrl($"{quotationName}/{name}"); list.RootFolder.AddSubFolderUsingPath(rPath); } ctx.ExecuteQuery(); oneNoteService.CreateFolderInsideQuotation(quotationName, ctx); Console.WriteLine($"Le sotto cartelle sono state create con successo in {quotationName}"); #endregion Console.WriteLine($"> {quotationName} creato con successo in Offerte/{docLibraryName}."); } catch (Exception ex) { logger.LogError(ex, ""); } } }