From b840d8a98440c3bb2e9b42ba64f0f9fb61108a53 Mon Sep 17 00:00:00 2001 From: Kalarumeth <70245579+Kalarumeth@users.noreply.github.com> Date: Tue, 27 Sep 2022 17:04:46 +0200 Subject: [PATCH] NC implementation --- console_spo_utils/Constants/Fields.cs | 17 +++++++++++ console_spo_utils/Enums/FieldType.cs | 3 +- .../Services/ISharePointStructureBuilder.cs | 1 + .../Interfaces/Services/ISiteOptions.cs | 4 ++- .../Interfaces/Services/ISiteService.cs | 1 + .../Interfaces/Services/ITenantService.cs | 1 + console_spo_utils/Services/OneNoteService.cs | 28 ++++++++++++++++++- .../Services/ProjectQuickMenuService.cs | 25 +++++++++++++++++ console_spo_utils/Services/ProjectService.cs | 3 +- console_spo_utils/Services/RightsService.cs | 28 +++++++++++++++++++ .../Services/SharePointCustomOperation.cs | 7 ++--- .../SharePointStructureBuilderService.cs | 13 +++++++++ console_spo_utils/Services/SiteOptions.cs | 19 +++++++++++-- console_spo_utils/Services/SiteService.cs | 21 ++++++++++++++ console_spo_utils/Services/SubSiteService.cs | 8 +++--- console_spo_utils/Services/TenantService.cs | 11 +++++++- console_spo_utils/Workers/MainWorker.cs | 8 ++++++ 17 files changed, 182 insertions(+), 16 deletions(-) diff --git a/console_spo_utils/Constants/Fields.cs b/console_spo_utils/Constants/Fields.cs index 0cc1b9b..eae59d3 100644 --- a/console_spo_utils/Constants/Fields.cs +++ b/console_spo_utils/Constants/Fields.cs @@ -124,4 +124,21 @@ public static class Fields }; } } + + public static Dictionary NonComplianceCustomFields + { + get + { + return new Dictionary() + { + { "PAL_NC_Source", new string[] {"Choice", "Sorgente", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", "InternoClienteFornitore" } }, + { "PAL_NC_Reference", new string[] { "Text", "Rifermento", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, + { "PAL_NC_Nominative", new string[] { "Text", "Nominativo", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, + { "PAL_NC_DateOfDetection", new string[] { "Text", "Data Rilevazione", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, + { "PAL_NC_Project", new string[] { "Text", "Commessa", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, + { "PAL_NC_ItemCode", new string[] { "Text", "Parte", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, + { "PAL_NC_Portal", new string[] {"URL", "Portal", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, + }; + } + } } \ No newline at end of file diff --git a/console_spo_utils/Enums/FieldType.cs b/console_spo_utils/Enums/FieldType.cs index 2ce2db5..ca4ce54 100644 --- a/console_spo_utils/Enums/FieldType.cs +++ b/console_spo_utils/Enums/FieldType.cs @@ -3,6 +3,7 @@ public enum PalFieldType { Project, - Quotation + Quotation, + NonCompliance } } diff --git a/console_spo_utils/Interfaces/Services/ISharePointStructureBuilder.cs b/console_spo_utils/Interfaces/Services/ISharePointStructureBuilder.cs index 9825b0c..465af41 100644 --- a/console_spo_utils/Interfaces/Services/ISharePointStructureBuilder.cs +++ b/console_spo_utils/Interfaces/Services/ISharePointStructureBuilder.cs @@ -4,4 +4,5 @@ public interface ISharePointStructureBuilder { public bool BuildProject(string projName); public bool BuildQuotation(string quotationName); + public bool BuildNonCompliance(string nonComplianceName); } \ No newline at end of file diff --git a/console_spo_utils/Interfaces/Services/ISiteOptions.cs b/console_spo_utils/Interfaces/Services/ISiteOptions.cs index da9a107..b396ff9 100644 --- a/console_spo_utils/Interfaces/Services/ISiteOptions.cs +++ b/console_spo_utils/Interfaces/Services/ISiteOptions.cs @@ -17,5 +17,7 @@ public interface ISiteOptions string GetSubProjList(string projName); public string GetQuotationLibrary(); public Uri GetProjectSite(); - + public string GetNonComplianceTenant(); + public string GetNonComplianceLibrary(); + public Uri GetNonComplianceSite(); } \ No newline at end of file diff --git a/console_spo_utils/Interfaces/Services/ISiteService.cs b/console_spo_utils/Interfaces/Services/ISiteService.cs index a5e54e9..672f9ab 100644 --- a/console_spo_utils/Interfaces/Services/ISiteService.cs +++ b/console_spo_utils/Interfaces/Services/ISiteService.cs @@ -5,4 +5,5 @@ public interface ISiteService public bool CreateProjectSiteIfNotExists(); public bool CreateSubSiteIfNotExists(string projName); public bool CreateQuotationSiteIfNotExists(string quotationName); + public bool CreateNonComplianceSiteIfNotExists(string nonComplianceName); } \ No newline at end of file diff --git a/console_spo_utils/Interfaces/Services/ITenantService.cs b/console_spo_utils/Interfaces/Services/ITenantService.cs index 4e1d4fa..453153c 100644 --- a/console_spo_utils/Interfaces/Services/ITenantService.cs +++ b/console_spo_utils/Interfaces/Services/ITenantService.cs @@ -7,4 +7,5 @@ public interface ITenantService { public void CreateForProject(); public void CreateForQuotation(); + public void CreateForNonCompliance(); } \ No newline at end of file diff --git a/console_spo_utils/Services/OneNoteService.cs b/console_spo_utils/Services/OneNoteService.cs index 0e3d7a8..cf6344d 100644 --- a/console_spo_utils/Services/OneNoteService.cs +++ b/console_spo_utils/Services/OneNoteService.cs @@ -73,6 +73,33 @@ public class OneNoteService : IOneNoteService 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(); + //TODO: verificare se worka + 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"; @@ -85,7 +112,6 @@ public class OneNoteService : IOneNoteService ctx.ExecuteQuery(); logger.LogInformation($"> La Feature {featureName} è ora attiva!"); } - #endregion } } \ No newline at end of file diff --git a/console_spo_utils/Services/ProjectQuickMenuService.cs b/console_spo_utils/Services/ProjectQuickMenuService.cs index 7b40bdf..80786f2 100644 --- a/console_spo_utils/Services/ProjectQuickMenuService.cs +++ b/console_spo_utils/Services/ProjectQuickMenuService.cs @@ -2,6 +2,7 @@ using console_spo_utils.Interfaces.Services; using Microsoft.Extensions.Logging; using Microsoft.SharePoint.Client; +using Microsoft.SharePoint.Navigation; namespace console_spo_utils.Services; @@ -22,6 +23,8 @@ public class ProjectQuickMenuService : IProjectQuickMenuService { try { + ClearQuickMenu(ctx); + var subSiteUrl = siteOptions.GetSubProjSite(projName).ToString(); var itemQuickMenu = Folders.GetProjectQuickMenu(projName, subSiteUrl); @@ -58,4 +61,26 @@ public class ProjectQuickMenuService : IProjectQuickMenuService logger.LogError(ex, "CreateForProject"); } } + + private void ClearQuickMenu(ClientContext ctx) + { + try + { + NavigationNodeCollection navBar = ctx.Web.Navigation.QuickLaunch; + ctx.Load(navBar); + ctx.ExecuteQueryRetry(); + + int[] element = new[] { 1, 1, 1 }; + + foreach (var e in element) + { + navBar[e].DeleteObject(); + ctx.ExecuteQuery(); + } + } + catch (Exception ex) + { + logger.LogError(ex, "ClearQuickMenu"); + } + } } \ No newline at end of file diff --git a/console_spo_utils/Services/ProjectService.cs b/console_spo_utils/Services/ProjectService.cs index 31a6a11..83b14c5 100644 --- a/console_spo_utils/Services/ProjectService.cs +++ b/console_spo_utils/Services/ProjectService.cs @@ -16,8 +16,7 @@ public class ProjectService : IProjectService public ProjectService(ISiteOptions siteOptions, ISharePointAuthenticationManager sharePointAuthenticationManager, IWebpartService webpartService, - ILogger logger, - IProjectQuickMenuService projectQuickMenuService) + ILogger logger) { this.siteOptions = siteOptions; this.sharePointAuthenticationManager = sharePointAuthenticationManager; diff --git a/console_spo_utils/Services/RightsService.cs b/console_spo_utils/Services/RightsService.cs index 1348392..d9bbff9 100644 --- a/console_spo_utils/Services/RightsService.cs +++ b/console_spo_utils/Services/RightsService.cs @@ -33,6 +33,13 @@ namespace console_spo_utils.Services { "ITS-SPO-PROJ-SALES-MODIFY", "Edit" }, { "ITS-SPO-PROJ-SALES-READ", "Read" } }; + + var itsAdGruopNCRole = new Dictionary() + { + { "ITS-SPO-NC-OWNER", "Full Control" }, + { "ITS-SPO-NC-MODIFY", "Edit" }, + { "ITS-SPO-NC-READ", "Read" } + }; #endregion logger.LogInformation("> Inizializzata la fase di assegnazione dei ruoli."); @@ -81,6 +88,27 @@ namespace console_spo_utils.Services logger.LogInformation("> Completata la fase di assegnazione dei ruoli."); #endregion } + else if (tenantName.Contains("Conformità")) + { + #region Site Permission Quotation + var web = ctx.Web; + + foreach (var role in itsAdGruopNCRole) + { + var adGroup = web.EnsureUser(role.Key); + ctx.Load(adGroup); + + var roleD = web.RoleDefinitions.GetByName(role.Value); + var roleDb = new RoleDefinitionBindingCollection(ctx) { roleD }; + + web.RoleAssignments.Add(adGroup, roleDb); + web.Update(); + } + + ctx.ExecuteQuery(); + logger.LogInformation("> Completata la fase di assegnazione dei ruoli."); + #endregion + } else if (!string.IsNullOrEmpty(docLibName)) { #region DocLib Permission diff --git a/console_spo_utils/Services/SharePointCustomOperation.cs b/console_spo_utils/Services/SharePointCustomOperation.cs index e2fdea7..cece9dd 100644 --- a/console_spo_utils/Services/SharePointCustomOperation.cs +++ b/console_spo_utils/Services/SharePointCustomOperation.cs @@ -25,8 +25,6 @@ namespace console_spo_utils.Services this.authMgr = authMgr; } - - #region Check If Exist public bool SiteExist(ClientContext ctx) { @@ -103,7 +101,7 @@ namespace console_spo_utils.Services return false; } - #endregion + public bool ListContentTypeExist(ClientContext ctx, string listTitle, string contentTypeName) { @@ -123,7 +121,7 @@ namespace console_spo_utils.Services return true; } } - + #endregion public void PalCustomField(ClientContext ctx, PalFieldType et) { @@ -131,6 +129,7 @@ namespace console_spo_utils.Services { PalFieldType.Project => Fields.ProjectCustomFields, PalFieldType.Quotation => Fields.QuotationCustomFields, + PalFieldType.NonCompliance => Fields.NonComplianceCustomFields, _ => throw new NotImplementedException("Entity type not found") }; diff --git a/console_spo_utils/Services/SharePointStructureBuilderService.cs b/console_spo_utils/Services/SharePointStructureBuilderService.cs index 9faefff..3a32e73 100644 --- a/console_spo_utils/Services/SharePointStructureBuilderService.cs +++ b/console_spo_utils/Services/SharePointStructureBuilderService.cs @@ -63,4 +63,17 @@ public class SharePointStructureBuilderService : ISharePointStructureBuilder return true; } + + public bool BuildNonCompliance(string nonComplianceName) + { + var siteBuildResult = siteService.CreateNonComplianceSiteIfNotExists(nonComplianceName); + if (!siteBuildResult) + { + throw new Exception($"Impossibile to build site"); + } + + quotationBuildService.CreateIfNotExists(nonComplianceName); + + return true; + } } \ No newline at end of file diff --git a/console_spo_utils/Services/SiteOptions.cs b/console_spo_utils/Services/SiteOptions.cs index c02b3ac..9a35a5a 100644 --- a/console_spo_utils/Services/SiteOptions.cs +++ b/console_spo_utils/Services/SiteOptions.cs @@ -53,12 +53,12 @@ public class SiteOptions : ISiteOptions public string GetQuotationLibrary() { - return $"Offerte {GetYear()}"; + return $"{GetQuotationTenant()} {GetYear()}"; } public Uri GetQuotationSite() { - return new Uri($"{SiteCollection}/sites/Offerte"); + return new Uri($"{SiteCollection}/sites/{GetQuotationTenant()}"); } public string GetSubProjList(string projName) @@ -75,4 +75,19 @@ public class SiteOptions : ISiteOptions { return new Uri($"{SiteCollection}/sites/{GetProjectTenant()}"); } + + public string GetNonComplianceTenant() + { + return "Non Conformità"; + } + + public string GetNonComplianceLibrary() + { + return $"{GetNonComplianceTenant()} {GetYear()}"; + } + + public Uri GetNonComplianceSite() + { + return new Uri($"{SiteCollection}/sites/{GetNonComplianceTenant().Replace(" ", String.Empty)}"); + } } \ No newline at end of file diff --git a/console_spo_utils/Services/SiteService.cs b/console_spo_utils/Services/SiteService.cs index fbf7452..168a44b 100644 --- a/console_spo_utils/Services/SiteService.cs +++ b/console_spo_utils/Services/SiteService.cs @@ -129,6 +129,27 @@ public class SiteService : ISiteService return true; } + public bool CreateNonComplianceSiteIfNotExists(string nonComplianceName) + { + try + { + var site = siteOptions.GetNonComplianceSite(); + using var ctx = authMgr.GetContext(site); + if (spc.SiteExist(ctx)) + { + return true; + } + tenantService.CreateForNonCompliance(); + } + catch (Exception e) + { + logger.LogError(e, "Site Service"); + return false; + } + + return true; + } + private void CreateProjectListEntry(string projName, ClientContext ctx) { diff --git a/console_spo_utils/Services/SubSiteService.cs b/console_spo_utils/Services/SubSiteService.cs index d1e4047..b950066 100644 --- a/console_spo_utils/Services/SubSiteService.cs +++ b/console_spo_utils/Services/SubSiteService.cs @@ -8,10 +8,13 @@ namespace console_spo_utils.Services; public class SubSiteService : ISubSiteService { private readonly ILogger logger; + private readonly IProjectQuickMenuService projectQuickMenuService; - public SubSiteService(ILogger logger) + public SubSiteService(ILogger logger, + IProjectQuickMenuService projectQuickMenuService) { this.logger = logger; + this.projectQuickMenuService = projectQuickMenuService; } @@ -19,7 +22,6 @@ public class SubSiteService : ISubSiteService { try { - logger.LogInformation($"> Il sito {projName} è in fase di creazione!"); var wci = new WebCreationInformation @@ -61,8 +63,6 @@ public class SubSiteService : ISubSiteService ctx.ExecuteQuery(); AddFieldsToSubProj(siteField, list, ctx); - - logger.LogInformation($"> Completata la fase di importazione dei campi in 'SottoCommesse {ssProjectTitle}'."); #endregion diff --git a/console_spo_utils/Services/TenantService.cs b/console_spo_utils/Services/TenantService.cs index 7a90848..e8a0eab 100644 --- a/console_spo_utils/Services/TenantService.cs +++ b/console_spo_utils/Services/TenantService.cs @@ -49,7 +49,16 @@ public class TenantService : ITenantService ); } - public void TenantCreation(string tenantName, Uri site,PalFieldType fieldType) + public void CreateForNonCompliance() + { + TenantCreation( + siteOptions.GetNonComplianceTenant(), + siteOptions.GetNonComplianceSite(), + PalFieldType.NonCompliance + ); + } + + public void TenantCreation(string tenantName, Uri site, PalFieldType fieldType) { Console.WriteLine($"> Inizializzata la fase di creazione del sito '../{tenantName.Replace(" ", string.Empty)}'."); diff --git a/console_spo_utils/Workers/MainWorker.cs b/console_spo_utils/Workers/MainWorker.cs index d6ff267..f32d0b3 100644 --- a/console_spo_utils/Workers/MainWorker.cs +++ b/console_spo_utils/Workers/MainWorker.cs @@ -35,6 +35,14 @@ namespace console_spo_utils.Workers logger.LogError("Impossible to create structure"); return; } + + var buildNonComplianceResult = structureBuilder.BuildNonCompliance("NC0001"); + + if (!buildNonComplianceResult) + { + logger.LogError("Impossible to create structure"); + return; + } } } } \ No newline at end of file