diff --git a/console_spo_utils/Program.cs b/console_spo_utils/Program.cs index aa86a6b..ae81ce8 100644 --- a/console_spo_utils/Program.cs +++ b/console_spo_utils/Program.cs @@ -1,8 +1,9 @@ -using Microsoft.Online.SharePoint.TenantAdministration; +using Microsoft.IdentityModel.Tokens; +using Microsoft.Online.SharePoint.TenantAdministration; using Microsoft.SharePoint.Client; using Microsoft.SharePoint.Client.DocumentSet; using Microsoft.SharePoint.Client.WebParts; -using PnP.Core.Model.SharePoint; +//using PnP.Core.Model.SharePoint; using System; //using PnP.Core.Model.SharePoint; //using PnP.Core.Services; @@ -55,9 +56,9 @@ namespace SharePointOnlineUtils #region General Variable string year = DateTime.Now.Year.ToString(); - string varProjSiteName = $"Commesse {year}"; - string projectsYear = varProjSiteName.Replace(" ", string.Empty); - string listTitle = $"Lista {varProjSiteName}"; + string tenantName = $"Commesse {year}"; + string projectsYear = tenantName.Replace(" ", string.Empty); + string listTitle = $"Lista {tenantName}"; string ssProjectTitle = "CT0016"; string[] ssSubProjectTitle = { "CT0016-0001", "CT0016-0002", "CT0016-0003" }; @@ -67,12 +68,12 @@ namespace SharePointOnlineUtils #region Project //ProjectsByYearSite(listTitle, varProjSiteName, svcUser, site, svcUserPsw); - Project(siteCollection, svcUser, svcUserPsw); + //Project(siteCollection, svcUser, svcUserPsw); //SubProject(siteCollection, svcUser, svcUserPsw); #endregion #region Offer - //Offer(siteCollection, svcUser, svcUserPsw); + Offer(siteCollection, svcUser, svcUserPsw); #endregion } @@ -85,12 +86,9 @@ namespace SharePointOnlineUtils if (spc.SiteExist(ctx) == false) { - //spc.TenantCreation(varProjSiteName, site, user, psw); - //spc.DomainGroupRights(ctx, string.Empty); - //spc.OnenoteSPFeature(ctx, string.Empty, string.Empty); - //spc.PalCustomField(ctx, spc.FieldType.Project); - //spc.ProjectsYList(listTitle, ctx); - //spc.ProjectsYListView(listTitle, ctx); + spc.TenantCreation(varProjSiteName, site, user, psw); + spc.PalCustomField(ctx, spc.FieldType.Project); + spc.ProjectsYList(listTitle, ctx); } } } @@ -98,7 +96,8 @@ namespace SharePointOnlineUtils public static async Task Project(string siteCollection, string user, SecureString psw) { string year = DateTime.Now.Year.ToString(); - string projectsYear = $"Commesse{year}"; + string tenantName = $"Commesse {year}"; + string projectsYear = tenantName.Replace(" ", string.Empty); string listTitle = $"Lista Commesse {year}"; string ssProjectTitle = "CT0016"; @@ -111,20 +110,20 @@ namespace SharePointOnlineUtils spc.AddWebPart(ctx, "/sites/Commesse2022/CT0008/SitePages/homepage.aspx"); - //if (spc.SiteExist(ctx) == true && spc.SiteExist(sCtx) == false) - //{ - // #region Site Context - // spc.ProjectSubSite(ssProjectTitle, projectsYear, ctx); - // spc.OnenoteSPFeature(ctx, year, ssProjectTitle); - // spc.ProjectListEntry(ssProjectTitle, listTitle, projectsYear, ctx); - // #endregion - // - // #region SubSite Context - // spc.ProjectDocLib(ssProjectTitle, sCtx); - // spc.ProjectSubProjList(ssProjectTitle, sCtx); - // spc.ProjectQuickMenu(ssProjectTitle, subSite.ToString(), sCtx); - // #endregion - //} + if (spc.SiteExist(ctx) == true && spc.SiteExist(sCtx) == false) + { + #region Site Context + spc.ProjectSubSite(ssProjectTitle, projectsYear, ctx); + spc.OnenoteSPFeature(ctx, tenantName, ssProjectTitle); //TODO verificare + spc.ProjectListEntry(ssProjectTitle, listTitle, projectsYear, ctx); + #endregion + + #region SubSite Context + spc.ProjectDocLib(ssProjectTitle, sCtx); + spc.ProjectSubProjList(ssProjectTitle, sCtx); + spc.ProjectQuickMenu(ssProjectTitle, subSite.ToString(), sCtx); + #endregion + } } public static async Task SubProject(string siteCollection, string user, SecureString psw) @@ -152,24 +151,28 @@ namespace SharePointOnlineUtils string year = DateTime.Now.Year.ToString(); string tenantName = "Offerte"; string docLibName = $"{tenantName} {year}"; + string quotationName = $"OF0004"; Uri site = new Uri($"{siteCollection}/sites/Offerte"); var authMgr = new AuthenticationManager(); var ctx = authMgr.GetContext(site, user, psw); + spc.OnenoteSPFeature(ctx, string.Empty, string.Empty); - //if (spc.SiteExist(ctx) == false) - //{ - // spc.TenantCreation(tenantName, site, user, psw); - // spc.PalCustomField(ctx, spc.FieldType.Quotation); - //} - - //if (spc.SiteExist(ctx) && spc.ListExist(ctx, docLibName) == false) - //{ - // spc.ProjectsYField(ctx, source); - // spc.OfferDocLib(year, ctx); - //} + if (!spc.SiteExist(ctx)) + { + spc.TenantCreation(tenantName, site, user, psw); + } + else if (spc.SiteExist(ctx) && spc.ListExist(ctx, docLibName) == false) + { + spc.PalCustomField(ctx, spc.FieldType.Quotation); + spc.OfferDocLib(docLibName, ctx); + } + else if (spc.SiteExist(ctx) && spc.ListExist(ctx, docLibName) && !spc.FolderExistsInsideList(ctx, docLibName, quotationName)) + { + spc.OfferDocSet(quotationName, docLibName, tenantName, ctx); + } } #endregion } @@ -275,52 +278,52 @@ namespace SharePointOnlineUtils } #endregion - public static async Task TenantCreation(string siteName, Uri site, string user, SecureString psw) + #region Tenant Creation + public static async Task TenantCreation(string tenantName, Uri site, string user, SecureString psw) { - Console.WriteLine($"> Inizializzata la fase di creazione del sito '../{siteName.Replace(" ", string.Empty)}'."); + Console.WriteLine($"> Inizializzata la fase di creazione del sito '../{tenantName.Replace(" ", string.Empty)}'."); string siteAdmin = "https://italsortbuttrio-admin.sharepoint.com"; Uri path = new Uri(siteAdmin); - using (var authenticationManager = new AuthenticationManager()) - using (var ctx = authenticationManager.GetContext(path, user, psw)) + var authMgr = new AuthenticationManager(); + var tenantCtx = authMgr.GetContext(path, user, psw); + var ctx = authMgr.GetContext(site, user, psw); + try { - try + var tenant = new Tenant(tenantCtx); + var scp = new SiteCreationProperties(); + + scp.Url = site.ToString(); + scp.Title = tenantName; + scp.Owner = user; + scp.Template = "SITEPAGEPUBLISHING#0"; + + SpoOperation spo = tenant.CreateSite(scp); + + tenantCtx.Load(tenant); + tenantCtx.Load(spo, i => i.IsComplete); + tenantCtx.ExecuteQuery(); + + while (!spo.IsComplete) { - var tenant = new Tenant(ctx); - var scp = new SiteCreationProperties(); - - scp.Url = site.ToString(); - scp.Title = siteName; - scp.Owner = user; - scp.Template = "SITEPAGEPUBLISHING#0"; - - SpoOperation spo = tenant.CreateSite(scp); - - ctx.Load(tenant); - ctx.Load(spo, i => i.IsComplete); - ctx.ExecuteQuery(); - - while (!spo.IsComplete) - { - Console.WriteLine($"Il sito '../{scp.Title}' è in fase di pubblicazione."); - System.Threading.Thread.Sleep(30000); - spo.RefreshLoad(); - await ctx.ExecuteQueryAsync(); - } - - Console.WriteLine($"> Il sito è stato creato con successo. ({site})"); - } - catch (Exception ex) - { - Console.WriteLine($"!> Qualcosa è andato storto nella creazione della pagina. ( {ex.Message} )"); - return; + Console.WriteLine($"Il sito '../{scp.Title}' è in fase di pubblicazione."); + System.Threading.Thread.Sleep(30000); + spo.RefreshLoad(); + await tenantCtx.ExecuteQueryAsync(); } + + Console.WriteLine($"> Il sito è stato creato con successo. ({site})"); + + DomainGroupRights(ctx, tenantName, string.Empty); + + OnenoteSPFeature(ctx, string.Empty, string.Empty); } + catch (Exception ex) { Logger.ConsoleMessage(ex); } + } - #region Projects Years public static async Task PalCustomField(ClientContext ctx, FieldType et) { Dictionary fieldList = new Dictionary(); @@ -342,7 +345,7 @@ namespace SharePointOnlineUtils case FieldType.Quotation: fieldList = new Dictionary() { - { "PAL_Offers_Reason", new string[] { "Choice", "Causale", "PAL Field", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", $"01 VENDITA MACCHINE / IMPIANTI02 VENDITA RICAMBI04 VENDITA SERVIZI FUORI COMMESSA08 VENDITA RIPARAZIONI09 ENGINEERING11 VENDITA E LAVORAZ.CONTO TERZI55 PREVENDITENC NON CONFORMITÁ" } }, + { "PAL_Quotation_Reason", new string[] { "Choice", "Causale", "PAL Field", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", $"01 VENDITA MACCHINE / IMPIANTI02 VENDITA RICAMBI04 VENDITA SERVIZI FUORI COMMESSA08 VENDITA RIPARAZIONI09 ENGINEERING11 VENDITA E LAVORAZ.CONTO TERZI55 PREVENDITENC NON CONFORMITÁ" } }, { "PAL_ID_Quotation", new string[] { "Text", "ID Offerta", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, { "PAL_Quotation_Name", new string[] { "Text", "Fornitore", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } }, { "PAL_Authors", new string[] { "Text", "Autore", "PAL Field", "FALSE", "FALSE", "TRUE", "TRUE", "TRUE", "TRUE", string.Empty } } @@ -370,14 +373,57 @@ namespace SharePointOnlineUtils ctx.ExecuteQuery(); } - catch (Exception ex) - { Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); return; } + catch (Exception ex) { Logger.ConsoleMessage(ex); } } + public static async Task OnenoteSPFeature(ClientContext ctx, string siteName, string onGroupSection) //TODO: predisposizione per offerte + { + var featureName = "SiteNotebook"; + Guid featureId = new Guid("f151bb39-7c3b-414f-bb36-6bf18872052f"); + + try + { + if (string.IsNullOrEmpty(onGroupSection)) + { + #region Feature Activate + if (!SiteFeaturesExist(ctx, featureName)) + { + ctx.Web.ActivateFeature(featureId); + ctx.ExecuteQuery(); + Console.WriteLine($"> La Feature {featureName} è ora attiva!"); + } + + #endregion + } + else + { + #region Note Group Section + + Console.WriteLine($"> Inizializzata la fase di creazione della sezione {onGroupSection} in OneNote."); + + var list = ctx.Web.Lists.EnsureSiteAssetsLibrary(); + ctx.Load(list, l => l.RootFolder); + ctx.ExecuteQuery(); + + var rPath = ResourcePath.FromDecodedUrl($"{siteName} Notebook/{onGroupSection}"); + list.RootFolder.AddSubFolderUsingPath(rPath); + ctx.ExecuteQuery(); + + Console.WriteLine($"> Completata la fase di creazione della sezione {onGroupSection} in OneNote."); + + #endregion + } + } + catch (Exception ex) { Logger.ConsoleMessage(ex); } + } + #endregion + + #region Projects Years public static async Task ProjectsYList(string listTitle, ClientContext ctx) { try { + #region New List Console.WriteLine($"> Inizializzata la fase di creazione della lista '{listTitle}'."); Web web = ctx.Web; @@ -385,13 +431,13 @@ namespace SharePointOnlineUtils creationInfo.Title = listTitle; creationInfo.TemplateType = (int)ListTemplateType.GenericList; - List list = web.Lists.Add(creationInfo); + List oList = web.Lists.Add(creationInfo); ctx.ExecuteQuery(); - ctx.Load(list, l => l.Fields); + ctx.Load(oList, l => l.Fields); - var fld = list.Fields.GetByInternalNameOrTitle("Title"); + var fld = oList.Fields.GetByInternalNameOrTitle("Title"); if (fld != null) { fld.Required = false; @@ -399,23 +445,13 @@ namespace SharePointOnlineUtils fld.SetShowInEditForm(false); fld.SetShowInNewForm(false); - list.Update(); + oList.Update(); ctx.ExecuteQuery(); } Console.WriteLine($"> La lista '{listTitle}' è stato creato con successo."); - } - catch (Exception ex) - { - Console.WriteLine($"!> Qualcosa è andato storto nella creazione della lista. ( {ex.Message} )"); - return; - } - } + #endregion - public static async Task ProjectsYListView(string listTitle, ClientContext ctx) - { - try - { #region Field Console.WriteLine($"> Inizializzata la fase di importazione dei campi '{listTitle}'."); @@ -476,13 +512,8 @@ namespace SharePointOnlineUtils Console.WriteLine($"> Impostata come HomePage di '{viewCreation.Title}'"); #endregion - - } - catch (Exception ex) - { - Console.WriteLine($"!> Qualcosa è andato storto nella creazione della ListView. ( {ex.Message} )"); - return; } + catch (Exception ex) { Logger.ConsoleMessage(ex); } } #endregion @@ -499,18 +530,13 @@ namespace SharePointOnlineUtils wci.Title = ssProjectTitle; wci.UseSamePermissionsAsParentSite = true; wci.WebTemplate = "SITEPAGEPUBLISHING#0"; - //wci.Language = 1040; Web web = ctx.Site.RootWeb.Webs.Add(wci); ctx.ExecuteQuery(); Console.WriteLine($"> Il sito {ssProjectTitle} è stato creato con successo!"); } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - return; - } + catch (Exception ex) { Logger.ConsoleMessage(ex); } } public static async Task ProjectListEntry(string ssProjectTitle, string listTitle, string projectsYear, ClientContext ctx) //TODO: PP var @@ -535,11 +561,7 @@ namespace SharePointOnlineUtils Console.WriteLine($"> La Commessa {ssProjectTitle} è stata aggiunta alla lista {listTitle} con successo!"); } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - return; - } + catch (Exception ex) { Logger.ConsoleMessage(ex); } } public static async Task ProjectDocLib(string ssProjectTitle, ClientContext ctx) //TODO: Da testare @@ -569,7 +591,7 @@ namespace SharePointOnlineUtils ctx.ExecuteQuery(); - if (kvp.Key.Contains("Commerciale")) DomainGroupRights(ctx, kvp.Key); + if (kvp.Key.Contains("Commerciale")) DomainGroupRights(ctx, string.Empty, kvp.Key); Console.WriteLine($"> '{kvp.Key}' è stato creato con successo."); } @@ -604,11 +626,7 @@ namespace SharePointOnlineUtils Console.WriteLine($"> {kvp.Key} creato con successo in {kvp.Value}."); } } - catch (Exception ex) - { - Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); - return; - } + catch (Exception ex) { Logger.ConsoleMessage(ex); } #endregion } @@ -671,11 +689,7 @@ namespace SharePointOnlineUtils Console.WriteLine($"> Abilitata la visualizzazione su mobile '{viewCreation.Title}'."); #endregion } - catch (Exception ex) - { - Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); - return; - } + catch (Exception ex) { Logger.ConsoleMessage(ex); } } public static async Task ProjectQuickMenu(string ssProjectTitle, string subSiteUrl, ClientContext ctx) @@ -716,8 +730,7 @@ namespace SharePointOnlineUtils } } } - catch (Exception ex) - { Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); return; } + catch (Exception ex) { Logger.ConsoleMessage(ex); } } #endregion @@ -804,21 +817,15 @@ namespace SharePointOnlineUtils } } } - catch (Exception ex) - { - Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); - return; - } + catch (Exception ex) { Logger.ConsoleMessage(ex); } } #endregion #region Offer - public static async Task OfferDocLib(string year, ClientContext ctx) + public static async Task OfferDocLib(string docLibName, ClientContext ctx) // TODO: enable one note function { try { - string docLibName = $"Offerte {year}"; - #region New DocLib Console.WriteLine($"> Inizializzata la fase di creazione '{docLibName}'."); @@ -828,20 +835,71 @@ namespace SharePointOnlineUtils ListCreationInformation lci = new ListCreationInformation(); lci.Title = docLibName; lci.TemplateType = (int)ListTemplateType.DocumentLibrary; - List list = web.Lists.Add(lci); + web.Lists.Add(lci); ctx.ExecuteQuery(); Console.WriteLine($"> Completata la fase di creazione '{docLibName}'."); #endregion - DomainGroupRights(ctx, docLibName); + #region Field + Console.WriteLine($"> Inizializzata la fase di importazione dei campi '{docLibName}'."); + + List list = ctx.Web.Lists.GetByTitle(docLibName); + var siteField = ctx.Site.RootWeb; + ctx.Load(list, l => l.Fields); + ctx.Load(siteField, s => s.Fields); + + ctx.ExecuteQuery(); + + string[] fieldName = { "ID Offerta", "Fornitore", "Causale", "Autore" }; + + foreach (var fn in fieldName) + { + var Fields = siteField.Fields.GetByTitle(fn); + list.Fields.Add(Fields); + } + + ctx.ExecuteQuery(); + + Console.WriteLine($"> Completata la fase di importazione dei campi '{docLibName}'."); + #endregion + + #region View + Console.WriteLine($"> Inizializzata la fase di creazione della ListView '{docLibName}'."); + + var views = list.Views; + + ViewCreationInformation viewCreation = new ViewCreationInformation(); + + viewCreation.SetAsDefaultView = true; + viewCreation.Title = docLibName; + viewCreation.ViewTypeKind = ViewType.None; + viewCreation.ColumnWidth = "350"; + viewCreation.ViewFields = new string[] { "Type", "Name", "ID Offerta", "Fornitore", "Causale", "Autore" }; + + var view = views.Add(viewCreation); + + ctx.Load(view); + ctx.ExecuteQuery(); + + Console.WriteLine($"> Completata la fase di creazione della ListView '{viewCreation.Title}'."); + + var customView = views.GetByTitle(viewCreation.Title); + customView.MobileView = true; + customView.MobileDefaultView = true; + + customView.Update(); + ctx.ExecuteQuery(); + + Console.WriteLine($"> Abilitata la visualizzazione su mobile '{viewCreation.Title}'."); + #endregion #region Shortcut on Quickmenu NavigationNodeCollection spNavNodeColl = web.Navigation.QuickLaunch; NavigationNodeCreationInformation newNavNode = new NavigationNodeCreationInformation(); newNavNode.Title = $"{docLibName}"; - newNavNode.Url = $"{web.Url}/Offerte%20{year}"; + newNavNode.Url = $"{web.Url}/{docLibName.Replace(" ", "%20")}"; newNavNode.AsLastNode = true; spNavNodeColl.Add(newNavNode); @@ -850,24 +908,23 @@ namespace SharePointOnlineUtils Console.WriteLine($"> Il menu rapido è stato aggiornato, il collegamemento a {docLibName} è ora disponibile."); #endregion } - catch (Exception ex) - { Console.WriteLine($"!> Qualcosa è andato storto... {ex.Message}"); return; } + catch (Exception ex) { Logger.ConsoleMessage(ex); } } - public static async Task OfferDocSet(string year, ClientContext ctx) + public static async Task OfferDocSet(string quotationName, string docLibName, string tenantName, ClientContext ctx) //TODO: add onenote on creation of { try { - var listTitle = $"Offerte {year}"; + #region Context var web = ctx.Web; ctx.Load(web, w => w.Url); - var list = web.Lists.GetByTitle(listTitle); + List list = web.Lists.GetByTitle(docLibName); ctx.Load(list, l => l.RootFolder, l => l.ContentTypes, l => l.Fields, l => l.ContentTypesEnabled); ctx.ExecuteQuery(); list.ContentTypesEnabled = true; list.Update(); - if (ListContentTypeExist(ctx, listTitle, "Set di documenti") == false) + if (ListContentTypeExist(ctx, docLibName, "Set di documenti") == false) { var documentCT = ctx.Site.RootWeb.AvailableContentTypes.GetById("0x0120D5"); ctx.Load(documentCT); @@ -888,58 +945,56 @@ namespace SharePointOnlineUtils var contentType = ctData.FirstOrDefault(); ctx.Load(contentType); ctx.ExecuteQuery(); - - } - catch (Exception ex) { Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); return; } - } - #endregion - - #region SPFeature - public static async Task OnenoteSPFeature(ClientContext ctx, string year, string ssProjectTitle) //TODO: predisposizione per offerte - { - var featureName = "SiteNotebook"; - Guid featureId = new Guid("f151bb39-7c3b-414f-bb36-6bf18872052f"); - - if (string.IsNullOrEmpty(ssProjectTitle)) - { - #region Feature Activate - try - { - if (!SiteFeaturesExist(ctx, featureName)) - { - ctx.Web.ActivateFeature(featureId); - ctx.ExecuteQuery(); - Console.WriteLine($"> La Feature {featureName} è ora attiva!"); - } - } - catch (Exception ex) { Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); return; } #endregion - } - else - { - #region Note Section - try + + + Console.WriteLine($"> Inizializzata la fase di creazione per {quotationName}."); + + #region DocSet Field Entry + DocumentSet.Create(ctx, list.RootFolder, quotationName, contentType.Id); + ctx.ExecuteQuery(); + + Console.WriteLine($"> DocumentSet {quotationName} creata."); + + var dsItem = list.RootFolder.Folders.GetByUrl(quotationName).ListItemAllFields; + + dsItem["PAL_ID_Quotation"] = "valueItemCode"; + dsItem["PAL_Quotation_Name"] = "valueItemDescription"; + dsItem["PAL_Quotation_Reason"] = "NC NON CONFORMITÁ"; + dsItem["PAL_Authors"] = "Test"; + + dsItem.Update(); + ctx.ExecuteQuery(); + + Console.WriteLine($"> Field value update."); + #endregion + + #region Quotation Folder + string rev = "Rev. 1"; + string[] folderName = { rev, $"{rev}/01_Costificazione", $"{rev}/02_Offerta", $"{rev}/03_Meeting", $"{rev}/04_Corrispondenza", $"{rev}/08_Specifiche_Tecniche", $"{rev}/10_Fornitori", $"{rev}/11_Documenti_Tecnici", $"{rev}/14_Foto" }; + + foreach (string name in folderName) { - Console.WriteLine($"> Inizializzata la fase di creazione della sezione {ssProjectTitle} in OneNote."); - - var list = ctx.Web.Lists.EnsureSiteAssetsLibrary(); - ctx.Load(list, l => l.RootFolder); - ctx.ExecuteQuery(); - - var rPath = ResourcePath.FromDecodedUrl($"Commesse {year} Notebook/{ssProjectTitle}"); + Console.WriteLine($"La sotto cartella {name} verrà creata in {quotationName}"); + var rPath = ResourcePath.FromDecodedUrl($"{quotationName}/{name}"); list.RootFolder.AddSubFolderUsingPath(rPath); - ctx.ExecuteQuery(); - - Console.WriteLine($"> Completata la fase di creazione della sezione {ssProjectTitle} in OneNote."); } - catch (Exception ex) { Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); return; } + + ctx.ExecuteQuery(); + + OnenoteSPFeature(ctx, tenantName, quotationName); + + Console.WriteLine($"Le sotto cartelle sono state create con successo in {quotationName}"); #endregion + + Console.WriteLine($"> {quotationName} creato con successo in Offerte/{docLibName}."); } + catch (Exception ex) { Logger.ConsoleMessage(ex); } } #endregion #region Rights - public static async Task DomainGroupRights(ClientContext ctx, string docLibName) + public static async Task DomainGroupRights(ClientContext ctx, string tenantName, string docLibName) { #region Groups & Privileges Dictionary itsADGruopRole = new Dictionary() @@ -963,9 +1018,9 @@ namespace SharePointOnlineUtils try { - if (string.IsNullOrEmpty(docLibName)) + if (tenantName.Contains("Commesse")) { - #region Site Permission + #region Site Permission Project Web web = ctx.Web; foreach (KeyValuePair role in itsADGruopRole) @@ -985,7 +1040,29 @@ namespace SharePointOnlineUtils Console.WriteLine("> Completata la fase di assegnazione dei ruoli."); #endregion } - else + else if (tenantName.Contains("Offerte")) + { + #region Site Permission Quotation + Web web = ctx.Web; + + 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); + + web.RoleAssignments.Add(adGroup, roleDB); + web.Update(); + } + + ctx.ExecuteQuery(); + Console.WriteLine("> Completata la fase di assegnazione dei ruoli."); + #endregion + } + else if (!string.IsNullOrEmpty(docLibName)) { #region DocLib Permission Web web = ctx.Web; @@ -1013,7 +1090,7 @@ namespace SharePointOnlineUtils } } - catch (Exception ex) { Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} {ex.StackTrace} )"); return; } + catch (Exception ex) { Logger.ConsoleMessage(ex); } } #endregion @@ -1061,7 +1138,7 @@ namespace SharePointOnlineUtils limitedWebPartManager.AddWebPart(oWebPartDefinition.WebPart, "Left", 1); ctx.ExecuteQuery(); } - catch (Exception ex) { Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )\n[\n{ex.StackTrace}\n]"); return; } + catch (Exception ex) { Logger.ConsoleMessage(ex); } } public static void GetWebPart(ClientContext ctx) @@ -1073,6 +1150,11 @@ namespace SharePointOnlineUtils #endregion } + public class Logger + { + public static void ConsoleMessage(Exception ex) { Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )\n[\n{ex.StackTrace}\n]"); return; } + } + public class AuthenticationManager : IDisposable {