113 lines
4.2 KiB
C#
113 lines
4.2 KiB
C#
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 PurchasingPackingSlipDocSetService : IPurchasingPackingSlipDocSetService
|
|
{
|
|
private readonly IPurchasingPackingSlipRepository _purchasingPackingSlipRepository;
|
|
private readonly ISharePointCustomOperation _cpt;
|
|
private readonly IOneNoteService _oneNoteService;
|
|
private readonly ISiteOptions _siteOptions;
|
|
private readonly ISharePointAuthenticationManager _sharePointAuthenticationManager;
|
|
private readonly ILogger<PurchasingPackingSlipDocSetService> _logger;
|
|
|
|
public PurchasingPackingSlipDocSetService(
|
|
IPurchasingPackingSlipRepository purchasingPackingSlipRepository,
|
|
ISharePointCustomOperation cpt,
|
|
IOneNoteService oneNoteService,
|
|
ISiteOptions siteOptions,
|
|
ISharePointAuthenticationManager sharePointAuthenticationManager,
|
|
ILogger<PurchasingPackingSlipDocSetService> logger)
|
|
{
|
|
_purchasingPackingSlipRepository = purchasingPackingSlipRepository;
|
|
_cpt = cpt;
|
|
_oneNoteService = oneNoteService;
|
|
_siteOptions = siteOptions;
|
|
_sharePointAuthenticationManager = sharePointAuthenticationManager;
|
|
_logger = logger;
|
|
}
|
|
public void Create(string purchasingPackingSlipName, 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 {purchasingPackingSlipName}.");
|
|
|
|
#region DocSet Field Entry
|
|
|
|
DocumentSet.Create(ctx, list.RootFolder, purchasingPackingSlipName, contentType.Id);
|
|
ctx.ExecuteQuery();
|
|
|
|
Console.WriteLine($"> DocumentSet {purchasingPackingSlipName} creata.");
|
|
var dsItem = list.RootFolder.Folders.GetByUrl(purchasingPackingSlipName).ListItemAllFields;
|
|
|
|
dsItem["PAL_PO_Supplier"] = _purchasingPackingSlipRepository.DefaultPurchasingPackingSlipSupplier(purchasingPackingSlipName);
|
|
dsItem["PAL_Status"] = _purchasingPackingSlipRepository.DefaultPurchasingPackingSlipState(purchasingPackingSlipName);
|
|
|
|
dsItem.Update();
|
|
ctx.ExecuteQuery();
|
|
|
|
Console.WriteLine($"> Field value update.");
|
|
|
|
#endregion
|
|
|
|
#region Folder
|
|
|
|
//foreach (var name in Folders.PurchasingOrder)
|
|
//{
|
|
// Console.WriteLine($"La sotto cartella {name} verrà creata in {nonComplianceName}");
|
|
// var rPath = ResourcePath.FromDecodedUrl($"{nonComplianceName}/{name}");
|
|
// list.RootFolder.AddSubFolderUsingPath(rPath);
|
|
//}
|
|
|
|
//ctx.ExecuteQuery();
|
|
|
|
var purchCtx = _sharePointAuthenticationManager.GetContext(_siteOptions.GetPurchasingSite());
|
|
|
|
|
|
_oneNoteService.CreateFolderInsidePurchasing(purchasingPackingSlipName, purchCtx, PurchType.PackingSlip);
|
|
|
|
Console.WriteLine($"Le sotto cartelle sono state create con successo in {purchasingPackingSlipName}");
|
|
|
|
#endregion
|
|
|
|
Console.WriteLine($"> {purchasingPackingSlipName} creato con successo.");
|
|
}
|
|
} |