Files
console_spo_utils/library_spo_utils/Services/ProjectService.cs
T
2022-09-29 08:33:32 +02:00

115 lines
5.1 KiB
C#

using console_spo_utils.Constants;
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WebParts;
namespace console_spo_utils.Services;
internal class ProjectService : IProjectService
{
private readonly ISiteOptions siteOptions;
private readonly ISharePointAuthenticationManager sharePointAuthenticationManager;
private readonly IWebpartService webpartService;
private readonly ILogger<ProjectService> logger;
public ProjectService(ISiteOptions siteOptions,
ISharePointAuthenticationManager sharePointAuthenticationManager,
IWebpartService webpartService,
ILogger<ProjectService> logger)
{
this.siteOptions = siteOptions;
this.sharePointAuthenticationManager = sharePointAuthenticationManager;
this.webpartService = webpartService;
this.logger = logger;
}
public void BuildWebParts()
{
try
{
string wpName = siteOptions.GetProjYearTenant();
var listView = GetListsId(siteOptions.GetProjListTitle(), 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() + "/Lists/" +
siteOptions.GetProjListTitle().Replace(" ", "%20") +
"/" + siteOptions.GetProjListTitle().Replace(" ", "%20") + ".aspx?viewid=";
string htmlSchema = $"<div><h1 style=\"font-weight: bold; font-size: 18pt; clear: both;\">{siteOptions.GetProjYearTenant()}</h1></div>" +
$"<div><p><a style=\"color: green\" href=\"{url}{listView[Fields.SynthticProjTitle[0]]}\">Progetti Macchina</a><br>Solo i progetti di vendita Macchine/Impianti</p></div>" +
$"<div><p><a style=\"color: green\" href=\"{url}{listView[Fields.SynthticProjTitle[1]]}\">Progetti Ricambi</a><br>Solo i progetti di vendita Ricambi</p></div>" +
$"<div><p><a style=\"color: green\" href=\"{url}{listView[Fields.SynthticProjTitle[2]]}\">Altri Progetti</a><br>Tutti gli altri progetti che non siano di vendita Macchine/Impianti o Ricambi</p></div>";
string xmlSchema = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<WebPart xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
" xmlns=\"http://schemas.microsoft.com/WebPart/v2\">" +
$"<Title>{siteOptions.GetProjYearTenant()}</Title><FrameType>None</FrameType>" +
"<Assembly>Microsoft.SharePoint, Version=13.0.0.0, Culture=neutral, " +
"PublicKeyToken=94de0004b6e3fcc5</Assembly>" +
"<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>" +
"<ContentLink xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" />" +
"<Content xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\">" +
$"<![CDATA[{htmlSchema}]]></Content>" +
"<PartStorage xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" /></WebPart>";
webpartService.AddWebPart(web, mgr, xmlSchema, wpName, "Body", 0);
var qm = new NavigationNodeCreationInformation()
{
Title = siteOptions.GetProjYearTenant(),
Url = siteOptions.GetProjectYearSite().ToString(),
AsLastNode = true
};
web.Navigation.QuickLaunch.Add(qm);
ctx.ExecuteQuery();
}
catch (Exception ex)
{
logger.LogError(ex, "WebPart Build");
}
}
private Dictionary<string, string> GetListsId(string listTitle, string[] listViewTitle)
{
Dictionary<string, string> listViewId = new Dictionary<string, string>();
try
{
foreach (var lViewTitle in listViewTitle)
{
var ctx = sharePointAuthenticationManager.GetContext(siteOptions.GetProjectYearSite());
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;
}
}