From 0cc1b8f0ee06f7dd490b16b89a38bf9dacbc2b4f Mon Sep 17 00:00:00 2001 From: Kalarumeth <70245579+Kalarumeth@users.noreply.github.com> Date: Fri, 29 Jul 2022 16:20:42 +0200 Subject: [PATCH] Defined group role and setted rights on creation --- console_spo_utils/Program.cs | 178 +++++++++++++++++++++++++++-------- 1 file changed, 137 insertions(+), 41 deletions(-) diff --git a/console_spo_utils/Program.cs b/console_spo_utils/Program.cs index 00501b1..55fedf5 100644 --- a/console_spo_utils/Program.cs +++ b/console_spo_utils/Program.cs @@ -16,7 +16,7 @@ using System.Web; namespace SharePointOnlineUtils { - + #region External Variable public class ParentProj { public string ProjId { get; set; } @@ -37,6 +37,7 @@ namespace SharePointOnlineUtils public string ItemDescription { get; set; } public string SerialNumber { get; set; } } + #endregion public class Program { @@ -59,10 +60,9 @@ namespace SharePointOnlineUtils Uri subSite = new Uri($"{siteCollection}/sites/{projectsYear}/{ssProjectTitle}"); #endregion - - ProjectsByYearSite(listTitle, year, svcUser, site, svcUserPsw); + //ProjectsByYearSite(listTitle, year, svcUser, site, svcUserPsw); Project(siteCollection, svcUser, svcUserPsw); - SubProject(siteCollection, svcUser, svcUserPsw); + //SubProject(siteCollection, svcUser, svcUserPsw); } public static async Task ProjectsByYearSite(string listTitle, string year, string user, Uri site, SecureString psw) @@ -70,9 +70,11 @@ namespace SharePointOnlineUtils using (var authMgr = new AuthenticationManager()) using (var ctx = authMgr.GetContext(site, user, psw)) { + if (SharePointCustomOperation.SiteExist(ctx) == false) { SharePointCustomOperation.ProjectsYSite(year, site, user, psw); + SharePointCustomOperation.DomainGroupRights(ctx, ""); SharePointCustomOperation.OnenoteSPFeature(ctx, "", ""); SharePointCustomOperation.ProjectsYField(ctx); SharePointCustomOperation.ProjectsYList(listTitle, ctx); @@ -86,7 +88,7 @@ namespace SharePointOnlineUtils string year = DateTime.Now.Year.ToString(); string projectsYear = $"Commesse{year}"; string listTitle = $"Lista Commesse {year}"; - string ssProjectTitle = "CT0002"; + string ssProjectTitle = "CT0011"; Uri site = new Uri($"{siteCollection}/sites/{projectsYear}"); Uri subSite = new Uri($"{siteCollection}/sites/{projectsYear}/{ssProjectTitle}"); @@ -163,7 +165,7 @@ namespace SharePointOnlineUtils } } - private static bool FolderExistsInsideList(ClientContext context, string listTitle ,string folderName) + public static bool FolderExistsInsideList(ClientContext context, string listTitle ,string folderName) { var folderExists = context.Web.Lists.GetByTitle(listTitle).RootFolder; context.Load(folderExists, f => f.Folders); @@ -177,11 +179,6 @@ namespace SharePointOnlineUtils } return false; } - catch (ServerUnauthorizedAccessException uae) - { - Console.WriteLine("You are not allowed to access this folder"); - throw; - } catch (Exception ex) { Console.WriteLine("Could not find folder."); @@ -189,7 +186,7 @@ namespace SharePointOnlineUtils } } - private static bool ListContentTypeExist(ClientContext ctx, string listTitle, string contentTypeName) + public static bool ListContentTypeExist(ClientContext ctx, string listTitle, string contentTypeName) { ContentTypeCollection cntCollection = ctx.Web.Lists.GetByTitle(listTitle).ContentTypes; @@ -271,32 +268,33 @@ namespace SharePointOnlineUtils } catch (Exception ex) { - Console.WriteLine($"?> Qualcosa è andato storto nella creazione della pagina. ( {ex.Message} )"); + Console.WriteLine($"!> Qualcosa è andato storto nella creazione della pagina. ( {ex.Message} )"); + return; } } } public static async Task ProjectsYField(ClientContext ctx) { + string xmlChoice = "01 VENDITA MACCHINE / IMPIANTI02 VENDITA RICAMBI04 VENDITA SERVIZI FUORI COMMESSA08 VENDITA RIPARAZIONI09 ENGINEERING11 VENDITA E LAVORAZ.CONTO TERZI55 PREVENDITENC NON CONFORMITÁ"; + + Dictionary fieldList = new Dictionary() + { + { "PAL_ID_Project", new string[] { "URL", "ID Progetto", "PAL Field", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, + { "PAL_DlvReason", new string[] { "Choice", "Causale", "PAL Field", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", xmlChoice } }, + { "PAL_Customer", new string[] { "Text", "Cliente", "PAL Field", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, + { "PAL_Item", new string[] { "Text", "Item", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, + { "PAL_ItemCode", new string[] { "Text", "Codice Articolo", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, + { "PAL_ItemDescription", new string[] { "Text", "Descrizione Articolo", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, + { "PAL_SerialNumber", new string[] { "Text", "Matricola Macchina", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } } + }; + try { var site = ctx.Site; ctx.Load(site, w => w.RootWeb, w => w.RootWeb.Fields); ctx.ExecuteQuery(); - string xmlChoice = "01 VENDITA MACCHINE / IMPIANTI02 VENDITA RICAMBI04 VENDITA SERVIZI FUORI COMMESSA08 VENDITA RIPARAZIONI09 ENGINEERING11 VENDITA E LAVORAZ.CONTO TERZI55 PREVENDITENC NON CONFORMITÁ"; - - Dictionary fieldList = new Dictionary() - { - { "PAL_ID_Project", new string[] { "URL", "ID Progetto", "PAL Field", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, - { "PAL_DlvReason", new string[] { "Choice", "Causale", "PAL Field", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", xmlChoice } }, - { "PAL_Customer", new string[] { "Text", "Cliente", "PAL Field", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, - { "PAL_Item", new string[] { "Text", "Item", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, - { "PAL_ItemCode", new string[] { "Text", "Codice Articolo", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, - { "PAL_ItemDescription", new string[] { "Text", "Descrizione Articolo", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, - { "PAL_SerialNumber", new string[] { "Text", "Matricola Macchina", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } } - }; - foreach (KeyValuePair f in fieldList) { var fieldXml = $""; @@ -308,7 +306,7 @@ namespace SharePointOnlineUtils ctx.ExecuteQuery(); } catch (Exception ex) - { Console.WriteLine($"?> Qualcosa è andato storto... ( {ex.Message} )"); } + { Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); return; } } public static async Task ProjectsYList(string listTitle, ClientContext ctx) @@ -344,7 +342,8 @@ namespace SharePointOnlineUtils } catch (Exception ex) { - Console.WriteLine($"?> Qualcosa è andato storto nella creazione della lista. ( {ex.Message} )"); + Console.WriteLine($"!> Qualcosa è andato storto nella creazione della lista. ( {ex.Message} )"); + return; } } @@ -416,7 +415,8 @@ namespace SharePointOnlineUtils } catch (Exception ex) { - Console.WriteLine($"?> Qualcosa è andato storto nella creazione della ListView. ( {ex.Message} )"); + Console.WriteLine($"!> Qualcosa è andato storto nella creazione della ListView. ( {ex.Message} )"); + return; } } #endregion @@ -434,7 +434,7 @@ namespace SharePointOnlineUtils wci.Title = ssProjectTitle; wci.UseSamePermissionsAsParentSite = true; wci.WebTemplate = "SITEPAGEPUBLISHING#0"; - wci.Language = 1040; + //wci.Language = 1040; Web web = ctx.Site.RootWeb.Webs.Add(wci); ctx.ExecuteQuery(); @@ -444,6 +444,7 @@ namespace SharePointOnlineUtils catch (Exception ex) { Console.WriteLine(ex.Message); + return; } } @@ -472,6 +473,7 @@ namespace SharePointOnlineUtils catch (Exception ex) { Console.WriteLine(ex.Message); + return; } } @@ -480,27 +482,36 @@ namespace SharePointOnlineUtils #region DocLib try { - string[] docLibName = { $"Commerciale {ssProjectTitle}", $"Documenti {ssProjectTitle}", $"Media {ssProjectTitle}", $"SottoCommesse {ssProjectTitle}" }; + string[] docLibName = { $"Documenti {ssProjectTitle}", $"Media {ssProjectTitle}", $"SottoCommesse {ssProjectTitle}", $"Commerciale {ssProjectTitle}" }; foreach (string libName in docLibName) { Console.WriteLine($"> Inizializzata la fase di creazione '{libName}'."); Web web = ctx.Web; - ListCreationInformation lci = new ListCreationInformation(); + ctx.Load(web); + //if (libName.Contains("Commerciale")) + //{ + // web.BreakRoleInheritance(false, true); + // ctx.ExecuteQuery(); + //} + + ListCreationInformation lci = new ListCreationInformation(); lci.Title = libName; lci.TemplateType = (int)ListTemplateType.DocumentLibrary; List list = web.Lists.Add(lci); ctx.ExecuteQuery(); + if (libName.Contains("Commerciale")) DomainGroupRights(ctx, libName); Console.WriteLine($"> '{libName}' è stato creato con successo."); } } catch (Exception ex) { - Console.WriteLine($"?> Qualcosa è andato storto... {ex.Message}"); + Console.WriteLine($"!> Qualcosa è andato storto... {ex.Message}"); + return; } #endregion @@ -537,7 +548,8 @@ namespace SharePointOnlineUtils } catch (Exception ex) { - Console.WriteLine($"?> Qualcosa è andato storto... ( {ex.Message} )"); + Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); + return; } #endregion } @@ -603,7 +615,8 @@ namespace SharePointOnlineUtils } catch (Exception ex) { - Console.WriteLine($"?> Qualcosa è andato storto... ( {ex.Message} )"); + Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); + return; } } @@ -636,13 +649,13 @@ namespace SharePointOnlineUtils Console.WriteLine($"> Il menu rapido è stato aggiornato in {ssProjectTitle}"); } catch (Exception ex) - { Console.WriteLine($"?> Qualcosa è andato storto... ( {ex.Message} )"); } + { Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); return; } } } #endregion #region SubProject - public static async Task SubProjectDocSet(string ssProjectTitle, string[] ssSubProjectTitle, ClientContext ctx) + public static async Task SubProjectDocSet(string ssProjectTitle, string[] ssSubProjectTitle, ClientContext ctx) //TODO: da impostare le variabili generali { try { @@ -727,7 +740,8 @@ namespace SharePointOnlineUtils } catch (Exception ex) { - Console.WriteLine($"?> Qualcosa è andato storto... ( {ex.Message} )"); + Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); + return; } } #endregion @@ -738,6 +752,7 @@ namespace SharePointOnlineUtils var featureName = "SiteNotebook"; Guid featureId = new Guid("f151bb39-7c3b-414f-bb36-6bf18872052f"); + #region Feature Activate if (string.IsNullOrEmpty(ssProjectTitle)) { try @@ -749,9 +764,12 @@ namespace SharePointOnlineUtils Console.WriteLine($"> La Feature {featureName} è ora attiva!"); } } - catch (Exception ex) { Console.WriteLine($"?> Qualcosa è andato storto... ( {ex.Message} )"); } + catch (Exception ex) { Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); return; } } - if (!string.IsNullOrEmpty(ssProjectTitle)) + #endregion + + #region Note Section + else { try { @@ -767,7 +785,85 @@ namespace SharePointOnlineUtils Console.WriteLine($"> Completata la fase di creazione della sezione {ssProjectTitle} in OneNote."); } - catch (Exception ex) { Console.WriteLine($"?> Qualcosa è andato storto... ( {ex.Message} )"); } + catch (Exception ex) { Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); return; } + } + #endregion + } + #endregion + + #region Rights + public static async Task DomainGroupRights(ClientContext ctx, string docLibName) + { + Dictionary itsADGruopRole = new Dictionary() + { + { "ITS-SPO-PROJ-OWNER", "Full Control" }, + { "ITS-SPO-PROJ-DESIGN", "Design" }, + { "ITS-SPO-PROJ-MODIFY", "Edit" }, + { "ITS-SPO-PROJ-COLLABORATE", "Contribute" }, + { "ITS-SPO-PROJ-READ", "Read" } + }; + + Dictionary itsADGruopSalesRole = new Dictionary() + { + { "ITS-SPO-PROJ-OWNER", "Full Control" }, + { "ITS-SPO-PROJ-SALES-MODIFY", "Edit" }, + { "ITS-SPO-PROJ-SALES-READ", "Read" } + }; + + Console.WriteLine("> Inizializzata la fase di assegnazione dei ruoli."); + + if (string.IsNullOrEmpty(docLibName)) + { + try + { + Web web = ctx.Web; + + foreach (KeyValuePair role in itsADGruopRole) + { + var adGroup = web.EnsureUser(role.Key); + ctx.Load(adGroup); + + var roleD = web.RoleDefinitions.GetByName(role.Value); + RoleDefinitionBindingCollection roleDB = new RoleDefinitionBindingCollection(ctx); + roleDB.Add(roleD); + + web.RoleAssignments.Add(adGroup, roleDB); + web.Update(); + } + + ctx.ExecuteQuery(); + Console.WriteLine("> Completata la fase di assegnazione dei ruoli."); + } + catch (Exception ex) { Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); return; } + } + else + { + try + { + Web web = ctx.Web; + List list = web.Lists.GetByTitle(docLibName); + ctx.Load(list); + + list.BreakRoleInheritance(false, true); + ctx.ExecuteQuery(); + + foreach (KeyValuePair role in itsADGruopSalesRole) + { + + var adGroup = web.EnsureUser(role.Key); + ctx.Load(adGroup); + + var roleD = web.RoleDefinitions.GetByName(role.Value); + RoleDefinitionBindingCollection roleDB = new RoleDefinitionBindingCollection(ctx); + roleDB.Add(roleD); + + list.RoleAssignments.Add(adGroup, roleDB); + list.Update(); + } + ctx.ExecuteQuery(); + Console.WriteLine($"> Completata la fase di assegnazione dei ruoli in {docLibName}."); + } + catch (Exception ex) { Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} {ex.StackTrace} )"); return; } } } #endregion