using library_spo_utils.Constants; using library_spo_utils.Interfaces.Services; using Microsoft.Extensions.Logging; using Microsoft.SharePoint.Client; using Microsoft.SharePoint.Client.WebParts; namespace library_spo_utils.Services; internal class ProjectService : IProjectService { private readonly ISiteOptions siteOptions; private readonly ISharePointAuthenticationManager sharePointAuthenticationManager; private readonly IWebpartService webpartService; private readonly ILogger logger; public ProjectService(ISiteOptions siteOptions, ISharePointAuthenticationManager sharePointAuthenticationManager, IWebpartService webpartService, ILogger logger) { this.siteOptions = siteOptions; this.sharePointAuthenticationManager = sharePointAuthenticationManager; this.webpartService = webpartService; this.logger = logger; } public void BuildWebParts(string projName) { try { string wpName = siteOptions.GetProjYearTenant(projName); var listView = GetListsId(projName, siteOptions.GetProjListTitle(projName), Fields.SynthticProjTitle); var ctx = sharePointAuthenticationManager.GetContext(siteOptions.GetProjectSite()); Web web = ctx.Web; var file = web.GetFileByServerRelativeUrl("/sites/Commesse/SitePages/Home.aspx"); ctx.Load(web); ctx.ExecuteQuery(); LimitedWebPartManager mgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared); string url = siteOptions.GetProjectYearSite(projName) + "/Lists/" + siteOptions.GetProjListTitle(projName).Replace(" ", "%20") + "/" + siteOptions.GetProjListTitle(projName).Replace(" ", "%20") + ".aspx?viewid="; string htmlSchema = $"

{siteOptions.GetProjYearTenant(projName)}

" + $"

Progetti Macchina
Solo i progetti di vendita Macchine/Impianti

" + $"

Progetti Ricambi
Solo i progetti di vendita Ricambi

" + $"

Altri Progetti
Tutti gli altri progetti che non siano di vendita Macchine/Impianti o Ricambi

"; string xmlSchema = "" + "" + $"{siteOptions.GetProjYearTenant(projName)}None" + "Microsoft.SharePoint, Version=13.0.0.0, Culture=neutral, " + "PublicKeyToken=94de0004b6e3fcc5" + "Microsoft.SharePoint.WebPartPages.ContentEditorWebPart" + "" + "" + $"" + ""; webpartService.AddWebPart(web, mgr, xmlSchema, wpName, "Body", 0); var qm = new NavigationNodeCreationInformation() { Title = siteOptions.GetProjYearTenant(projName), Url = siteOptions.GetProjectYearSite(projName).ToString(), AsLastNode = true }; web.Navigation.QuickLaunch.Add(qm); ctx.ExecuteQuery(); } catch (Exception ex) { logger.LogError(ex, "WebPart Build"); } } private Dictionary GetListsId(string projName, string listTitle, string[] listViewTitle) { Dictionary listViewId = new Dictionary(); try { foreach (var lViewTitle in listViewTitle) { var ctx = sharePointAuthenticationManager.GetContext(siteOptions.GetProjectYearSite(projName)); var list = ctx.Web.Lists.GetByTitle(listTitle); ctx.Load(list.Views); var listView = list.Views.GetByTitle(lViewTitle); ctx.Load(listView, l => l.Id); ctx.ExecuteQuery(); var id = listView.Id; listViewId.Add(lViewTitle, id.ToString()); } } catch (Exception ex) { logger.LogError(ex, "Get List Id for Web Part"); } return listViewId; } }