Files
console_spo_utils/library_spo_utils/Services/OneNoteService.cs
T
2023-06-21 10:38:57 +02:00

195 lines
6.6 KiB
C#

using System.Net;
using library_spo_utils.Constants;
using library_spo_utils.Enums;
using library_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
namespace library_spo_utils.Services;
internal class OneNoteService : IOneNoteService
{
private readonly ILogger<OneNoteService> logger;
private readonly ISiteOptions siteOptions;
private readonly ISharePointCustomOperation spc;
public OneNoteService(
ILogger<OneNoteService> logger,
ISiteOptions siteOptions,
ISharePointCustomOperation spc)
{
this.logger = logger;
this.siteOptions = siteOptions;
this.spc = spc;
}
public bool CreateFolderInsideProject(string projName, ClientContext ctx)
{
try
{
logger.LogInformation($"> Inizializzata la fase di creazione della sezione {projName} in OneNote.");
var list = ctx.Web.Lists.EnsureSiteAssetsLibrary();
ctx.Load(list, l => l.RootFolder);
ctx.ExecuteQuery();
var rPath = ResourcePath.FromDecodedUrl($"{siteOptions.GetProjYearTenant(projName)} Notebook/{projName}");
list.RootFolder.AddSubFolderUsingPath(rPath);
ctx.ExecuteQuery();
logger.LogInformation($"> Completata la fase di creazione della sezione {projName} in OneNote.");
}
catch (Exception ex)
{
logger.LogError(ex, "OnenoteSPFeature");
return false;
}
return true;
}
public bool CreateFolderInsideQuotation(string quotationName, ClientContext ctx)
{
try
{
logger.LogInformation($"> Inizializzata la fase di creazione della sezione {quotationName} in OneNote.");
var year = siteOptions.GetYear(quotationName);
var list = ctx.Web.Lists.EnsureSiteAssetsLibrary();
ctx.Load(list, l => l.RootFolder);
ctx.ExecuteQuery();
if (!spc.FolderExist(ctx, $"SiteAssets/{siteOptions.GetQuotationTenant()} Notebook/{year}"))
{
var rYearPath = ResourcePath.FromDecodedUrl($"{siteOptions.GetQuotationTenant()} Notebook/{year}");
list.RootFolder.AddSubFolderUsingPath(rYearPath);
ctx.ExecuteQuery();
}
var rPath = ResourcePath.FromDecodedUrl($"{siteOptions.GetQuotationTenant()} Notebook/{year}/{quotationName}");
list.RootFolder.AddSubFolderUsingPath(rPath);
ctx.ExecuteQuery();
logger.LogInformation($"> Completata la fase di creazione della sezione {quotationName} in OneNote.");
}
catch (Exception ex)
{
logger.LogError(ex, "OnenoteSPFeature");
return false;
}
return true;
}
public bool CreateFolderInsideNonCompliance(string nonComplianceName, ClientContext ctx)
{
try
{
logger.LogInformation($"> Inizializzata la fase di creazione della sezione {nonComplianceName} in OneNote.");
var year = siteOptions.GetYear(nonComplianceName);
var list = ctx.Web.Lists.EnsureSiteAssetsLibrary();
ctx.Load(list, l => l.RootFolder);
ctx.ExecuteQuery();
if (!spc.FolderExist(ctx, $"SiteAssets/{siteOptions.GetNonComplianceTenant()} Notebook/{year}"))
{
var rYearPath = ResourcePath.FromDecodedUrl($"{siteOptions.GetNonComplianceTenant()} Notebook/{year}");
list.RootFolder.AddSubFolderUsingPath(rYearPath);
ctx.ExecuteQuery();
}
var rPath = ResourcePath.FromDecodedUrl($"{siteOptions.GetNonComplianceTenant()} Notebook/{year}/{nonComplianceName}");
list.RootFolder.AddSubFolderUsingPath(rPath);
ctx.ExecuteQuery();
logger.LogInformation($"> Completata la fase di creazione della sezione {nonComplianceName} in OneNote.");
}
catch (Exception ex)
{
logger.LogError(ex, "OnenoteSPFeature");
return false;
}
return true;
}
public bool CreateFolderInsidePurchasing(string purchasingName, ClientContext ctx, PurchType type)
{
try
{
logger.LogInformation($"> Inizializzata la fase di creazione della sezione {purchasingName} in OneNote.");
var purchasingOneNote = "Acquisti Notebook";
var year = siteOptions.GetYear(purchasingName);
var list = ctx.Web.Lists.EnsureSiteAssetsLibrary();
ctx.Load(list, l => l.RootFolder);
ctx.ExecuteQuery();
if (!spc.FolderExist(ctx, $"SiteAssets/{purchasingOneNote}/{year}"))
{
foreach (var dir in Folders.PurchasingOneNoteDir(year))
{
var rDirPath = ResourcePath.FromDecodedUrl($"{purchasingOneNote}/{dir}");
list.RootFolder.AddSubFolderUsingPath(rDirPath);
ctx.ExecuteQuery();
}
}
switch (type)
{
case PurchType.Order:
var oPath = ResourcePath.FromDecodedUrl($"{purchasingOneNote}/{year}/Ordini di Acquisto/{purchasingName}");
list.RootFolder.AddSubFolderUsingPath(oPath);
ctx.ExecuteQuery();
break;
case PurchType.PackingSlip:
var psPath = ResourcePath.FromDecodedUrl($"{purchasingOneNote}/{year}/DDT di Acquisto/{purchasingName}");
list.RootFolder.AddSubFolderUsingPath(psPath);
ctx.ExecuteQuery();
break;
case PurchType.Request:
var rPath = ResourcePath.FromDecodedUrl($"{purchasingOneNote}/{year}/Richieste di Acquisto/{purchasingName}");
list.RootFolder.AddSubFolderUsingPath(rPath);
ctx.ExecuteQuery();
break;
}
logger.LogInformation($"> Completata la fase di creazione della sezione {purchasingName} in OneNote.");
}
catch (Exception ex)
{
logger.LogError(ex, "OnenoteSPFeature");
return false;
}
return true;
}
public void EnableFeature(ClientContext ctx)
{
var featureName = "SiteNotebook";
var featureId = new Guid("f151bb39-7c3b-414f-bb36-6bf18872052f");
#region Feature Activate
if (!spc.SiteFeaturesExist(ctx, featureName))
{
ctx.Web.ActivateFeature(featureId);
ctx.ExecuteQuery();
logger.LogInformation($"> La Feature {featureName} è ora attiva!");
}
#endregion
}
}