131 lines
4.4 KiB
C#
131 lines
4.4 KiB
C#
using console_spo_utils.Interfaces.Services;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.SharePoint.Client;
|
|
|
|
namespace console_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()} 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 list = ctx.Web.Lists.EnsureSiteAssetsLibrary();
|
|
ctx.Load(list, l => l.RootFolder);
|
|
ctx.ExecuteQuery();
|
|
|
|
if (!spc.FolderExistsInsideList(ctx, $"{siteOptions.GetQuotationTenant()} Notebook", $"{DateTime.Today.Year}"))
|
|
{
|
|
var rYearPath = ResourcePath.FromDecodedUrl($"{siteOptions.GetQuotationTenant()} Notebook/{DateTime.Today.Year}");
|
|
list.RootFolder.AddSubFolderUsingPath(rYearPath);
|
|
ctx.ExecuteQuery();
|
|
}
|
|
|
|
var rPath = ResourcePath.FromDecodedUrl($"{siteOptions.GetQuotationTenant()} Notebook/{DateTime.Today.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 quotationName, ClientContext ctx)
|
|
{
|
|
try
|
|
{
|
|
|
|
logger.LogInformation($"> Inizializzata la fase di creazione della sezione {quotationName} in OneNote.");
|
|
|
|
var list = ctx.Web.Lists.EnsureSiteAssetsLibrary();
|
|
ctx.Load(list, l => l.RootFolder);
|
|
ctx.ExecuteQuery();
|
|
|
|
if (!spc.FolderExistsInsideList(ctx, $"{siteOptions.GetNonComplianceTenant()} Notebook", $"{DateTime.Today.Year}"))
|
|
{
|
|
var rYearPath = ResourcePath.FromDecodedUrl($"{siteOptions.GetNonComplianceTenant()} Notebook/{DateTime.Today.Year}");
|
|
list.RootFolder.AddSubFolderUsingPath(rYearPath);
|
|
ctx.ExecuteQuery();
|
|
}
|
|
|
|
var rPath = ResourcePath.FromDecodedUrl($"{siteOptions.GetNonComplianceTenant()} Notebook/{DateTime.Today.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 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
|
|
}
|
|
} |