115 lines
5.2 KiB
C#
115 lines
5.2 KiB
C#
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<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(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 = $"<div><h1 style=\"font-weight: bold; font-size: 18pt; clear: both;\">{siteOptions.GetProjYearTenant(projName)}</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(projName)}</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(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<string, string> GetListsId(string projName, string listTitle, string[] listViewTitle)
|
|
{
|
|
Dictionary<string, string> listViewId = new Dictionary<string, string>();
|
|
|
|
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;
|
|
}
|
|
} |