108 lines
3.9 KiB
C#
108 lines
3.9 KiB
C#
using console_spo_utils.Constants;
|
|
using console_spo_utils.Interfaces.Repositories;
|
|
using console_spo_utils.Interfaces.Services;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.SharePoint.Client;
|
|
using Microsoft.SharePoint.Client.DocumentSet;
|
|
|
|
namespace console_spo_utils.Services;
|
|
|
|
internal class NonComplianceDocSetService : INonComplianceDocSetService
|
|
{
|
|
private readonly ISharePointCustomOperation cpt;
|
|
private readonly IOneNoteService oneNoteService;
|
|
private readonly INonComplianceRepository nonComplianceRepository;
|
|
|
|
public NonComplianceDocSetService(
|
|
ISharePointCustomOperation cpt,
|
|
IOneNoteService oneNoteService,
|
|
INonComplianceRepository nonComplianceRepository)
|
|
{
|
|
this.cpt = cpt;
|
|
this.oneNoteService = oneNoteService;
|
|
this.nonComplianceRepository = nonComplianceRepository;
|
|
}
|
|
|
|
public void Create(string nonComplianceName, string docLibraryName, string tenantName, ClientContext ctx)
|
|
{
|
|
#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 {nonComplianceName}.");
|
|
|
|
#region DocSet Field Entry
|
|
|
|
DocumentSet.Create(ctx, list.RootFolder, nonComplianceName, contentType.Id);
|
|
ctx.ExecuteQuery();
|
|
|
|
Console.WriteLine($"> DocumentSet {nonComplianceName} creata.");
|
|
|
|
var dsItem = list.RootFolder.Folders.GetByUrl(nonComplianceName).ListItemAllFields;
|
|
|
|
dsItem["PAL_NC_Source"] = nonComplianceRepository.DefaultNonComplianceSource();
|
|
dsItem["PAL_NC_Reference"] = nonComplianceRepository.DefaultNonComplianceReference();
|
|
dsItem["PAL_NC_Nominative"] = nonComplianceRepository.DefaultNonComplianceNominative();
|
|
dsItem["PAL_NC_DateOfDetection"] = nonComplianceRepository.DefaultNonComplianceDateOfDetection();
|
|
dsItem["PAL_NC_Project"] = nonComplianceRepository.DefaultNonComplianceProject();
|
|
dsItem["PAL_NC_ItemCode"] = nonComplianceRepository.DefaultNonComplianceItemCode();
|
|
dsItem["PAL_NC_PortalUrl"] = nonComplianceRepository.DefaultNonCompliancePortalUrl();
|
|
|
|
dsItem.Update();
|
|
ctx.ExecuteQuery();
|
|
|
|
Console.WriteLine($"> Field value update.");
|
|
|
|
#endregion
|
|
|
|
#region Quotation Folder
|
|
|
|
foreach (var name in Folders.OfferDocSet)
|
|
{
|
|
Console.WriteLine($"La sotto cartella {name} verrà creata in {nonComplianceName}");
|
|
var rPath = ResourcePath.FromDecodedUrl($"{nonComplianceName}/{name}");
|
|
list.RootFolder.AddSubFolderUsingPath(rPath);
|
|
}
|
|
|
|
ctx.ExecuteQuery();
|
|
|
|
oneNoteService.CreateFolderInsideNonCompliance(nonComplianceName, ctx);
|
|
|
|
Console.WriteLine($"Le sotto cartelle sono state create con successo in {nonComplianceName}");
|
|
|
|
#endregion
|
|
|
|
Console.WriteLine($"> {nonComplianceName} creato con successo in Offerte/{docLibraryName}.");
|
|
}
|
|
} |