Files
console_spo_utils/console_spo_utils/Services/ProjectQuickMenuService.cs
T
2022-09-15 18:26:06 +02:00

61 lines
1.8 KiB
C#

using console_spo_utils.Constants;
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
namespace console_spo_utils.Services;
public class ProjectQuickMenuService : IProjectQuickMenuService
{
private readonly ILogger<ProjectQuickMenuService> logger;
private readonly ISiteOptions siteOptions;
public ProjectQuickMenuService(ILogger<ProjectQuickMenuService> logger
, ISiteOptions siteOptions)
{
this.logger = logger;
this.siteOptions = siteOptions;
}
public void CreateForProject(string projName, ClientContext ctx)
{
try
{
var subSiteUrl = siteOptions.GetSubProjSite(projName).ToString();
var itemQuickMenu = Folders.GetProjectQuickMenu(projName, subSiteUrl);
var spNavNodeColl = ctx.Web.Navigation.QuickLaunch;
var newNavNode = new NavigationNodeCreationInformation();
ctx.Load(ctx.Web, w => w.RootFolder.WelcomePage);
foreach (var (name, path) in itemQuickMenu)
{
newNavNode.Title = name;
newNavNode.Url = path;
newNavNode.AsLastNode = true;
spNavNodeColl.Add(newNavNode);
ctx.ExecuteQuery();
logger.LogInformation($"> Il menu rapido è stato aggiornato in {projName}");
if (name != $"SottoCommesse {projName}")
{
continue;
}
ctx.Web.RootFolder.WelcomePage = $"SottoCommesse%20{projName}/Forms/SottoCommesse%20{projName}.aspx";
ctx.Web.RootFolder.Update();
ctx.ExecuteQuery();
}
}
catch (Exception ex)
{
logger.LogError(ex, "CreateForProject");
}
}
}