Defined group role and setted rights on creation

This commit is contained in:
Kalarumeth
2022-07-29 16:20:42 +02:00
parent f279977558
commit 0cc1b8f0ee
+130 -34
View File
@@ -16,7 +16,7 @@ using System.Web;
namespace SharePointOnlineUtils namespace SharePointOnlineUtils
{ {
#region External Variable
public class ParentProj public class ParentProj
{ {
public string ProjId { get; set; } public string ProjId { get; set; }
@@ -37,6 +37,7 @@ namespace SharePointOnlineUtils
public string ItemDescription { get; set; } public string ItemDescription { get; set; }
public string SerialNumber { get; set; } public string SerialNumber { get; set; }
} }
#endregion
public class Program public class Program
{ {
@@ -59,10 +60,9 @@ namespace SharePointOnlineUtils
Uri subSite = new Uri($"{siteCollection}/sites/{projectsYear}/{ssProjectTitle}"); Uri subSite = new Uri($"{siteCollection}/sites/{projectsYear}/{ssProjectTitle}");
#endregion #endregion
//ProjectsByYearSite(listTitle, year, svcUser, site, svcUserPsw);
ProjectsByYearSite(listTitle, year, svcUser, site, svcUserPsw);
Project(siteCollection, svcUser, 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) 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 authMgr = new AuthenticationManager())
using (var ctx = authMgr.GetContext(site, user, psw)) using (var ctx = authMgr.GetContext(site, user, psw))
{ {
if (SharePointCustomOperation.SiteExist(ctx) == false) if (SharePointCustomOperation.SiteExist(ctx) == false)
{ {
SharePointCustomOperation.ProjectsYSite(year, site, user, psw); SharePointCustomOperation.ProjectsYSite(year, site, user, psw);
SharePointCustomOperation.DomainGroupRights(ctx, "");
SharePointCustomOperation.OnenoteSPFeature(ctx, "", ""); SharePointCustomOperation.OnenoteSPFeature(ctx, "", "");
SharePointCustomOperation.ProjectsYField(ctx); SharePointCustomOperation.ProjectsYField(ctx);
SharePointCustomOperation.ProjectsYList(listTitle, ctx); SharePointCustomOperation.ProjectsYList(listTitle, ctx);
@@ -86,7 +88,7 @@ namespace SharePointOnlineUtils
string year = DateTime.Now.Year.ToString(); string year = DateTime.Now.Year.ToString();
string projectsYear = $"Commesse{year}"; string projectsYear = $"Commesse{year}";
string listTitle = $"Lista Commesse {year}"; string listTitle = $"Lista Commesse {year}";
string ssProjectTitle = "CT0002"; string ssProjectTitle = "CT0011";
Uri site = new Uri($"{siteCollection}/sites/{projectsYear}"); Uri site = new Uri($"{siteCollection}/sites/{projectsYear}");
Uri subSite = new Uri($"{siteCollection}/sites/{projectsYear}/{ssProjectTitle}"); 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; var folderExists = context.Web.Lists.GetByTitle(listTitle).RootFolder;
context.Load(folderExists, f => f.Folders); context.Load(folderExists, f => f.Folders);
@@ -177,11 +179,6 @@ namespace SharePointOnlineUtils
} }
return false; return false;
} }
catch (ServerUnauthorizedAccessException uae)
{
Console.WriteLine("You are not allowed to access this folder");
throw;
}
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine("Could not find folder."); 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; ContentTypeCollection cntCollection = ctx.Web.Lists.GetByTitle(listTitle).ContentTypes;
@@ -271,19 +268,14 @@ namespace SharePointOnlineUtils
} }
catch (Exception ex) 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) public static async Task ProjectsYField(ClientContext ctx)
{ {
try
{
var site = ctx.Site;
ctx.Load(site, w => w.RootWeb, w => w.RootWeb.Fields);
ctx.ExecuteQuery();
string xmlChoice = "<CHOICES><CHOICE>01 VENDITA MACCHINE / IMPIANTI</CHOICE><CHOICE>02 VENDITA RICAMBI</CHOICE><CHOICE>04 VENDITA SERVIZI FUORI COMMESSA</CHOICE><CHOICE>08 VENDITA RIPARAZIONI</CHOICE><CHOICE>09 ENGINEERING</CHOICE><CHOICE>11 VENDITA E LAVORAZ.CONTO TERZI</CHOICE><CHOICE>55 PREVENDITE</CHOICE><CHOICE>NC NON CONFORMITÁ</CHOICE></CHOICES>"; string xmlChoice = "<CHOICES><CHOICE>01 VENDITA MACCHINE / IMPIANTI</CHOICE><CHOICE>02 VENDITA RICAMBI</CHOICE><CHOICE>04 VENDITA SERVIZI FUORI COMMESSA</CHOICE><CHOICE>08 VENDITA RIPARAZIONI</CHOICE><CHOICE>09 ENGINEERING</CHOICE><CHOICE>11 VENDITA E LAVORAZ.CONTO TERZI</CHOICE><CHOICE>55 PREVENDITE</CHOICE><CHOICE>NC NON CONFORMITÁ</CHOICE></CHOICES>";
Dictionary<string, string[]> fieldList = new Dictionary<string, string[]>() Dictionary<string, string[]> fieldList = new Dictionary<string, string[]>()
@@ -297,6 +289,12 @@ namespace SharePointOnlineUtils
{ "PAL_SerialNumber", new string[] { "Text", "Matricola Macchina", "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();
foreach (KeyValuePair<string, string[]> f in fieldList) foreach (KeyValuePair<string, string[]> f in fieldList)
{ {
var fieldXml = $"<Field Name='{f.Key}' Type='{f.Value[0]}' Description='' DisplayName='{f.Value[1]}' StaticName='{f.Key}' Group='{f.Value[2]}' Hidden='{f.Value[3]}' Required='{f.Value[4]}' Sealed='{f.Value[5]}' ShowInDisplayForm='{f.Value[6]}' ShowInEditForm='{f.Value[7]}' ShowInNewForm='{f.Value[8]}'>{f.Value[9]}</Field>"; var fieldXml = $"<Field Name='{f.Key}' Type='{f.Value[0]}' Description='' DisplayName='{f.Value[1]}' StaticName='{f.Key}' Group='{f.Value[2]}' Hidden='{f.Value[3]}' Required='{f.Value[4]}' Sealed='{f.Value[5]}' ShowInDisplayForm='{f.Value[6]}' ShowInEditForm='{f.Value[7]}' ShowInNewForm='{f.Value[8]}'>{f.Value[9]}</Field>";
@@ -308,7 +306,7 @@ namespace SharePointOnlineUtils
ctx.ExecuteQuery(); ctx.ExecuteQuery();
} }
catch (Exception ex) 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) public static async Task ProjectsYList(string listTitle, ClientContext ctx)
@@ -344,7 +342,8 @@ namespace SharePointOnlineUtils
} }
catch (Exception ex) 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) 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 #endregion
@@ -434,7 +434,7 @@ namespace SharePointOnlineUtils
wci.Title = ssProjectTitle; wci.Title = ssProjectTitle;
wci.UseSamePermissionsAsParentSite = true; wci.UseSamePermissionsAsParentSite = true;
wci.WebTemplate = "SITEPAGEPUBLISHING#0"; wci.WebTemplate = "SITEPAGEPUBLISHING#0";
wci.Language = 1040; //wci.Language = 1040;
Web web = ctx.Site.RootWeb.Webs.Add(wci); Web web = ctx.Site.RootWeb.Webs.Add(wci);
ctx.ExecuteQuery(); ctx.ExecuteQuery();
@@ -444,6 +444,7 @@ namespace SharePointOnlineUtils
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(ex.Message); Console.WriteLine(ex.Message);
return;
} }
} }
@@ -472,6 +473,7 @@ namespace SharePointOnlineUtils
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine(ex.Message); Console.WriteLine(ex.Message);
return;
} }
} }
@@ -480,27 +482,36 @@ namespace SharePointOnlineUtils
#region DocLib #region DocLib
try 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) foreach (string libName in docLibName)
{ {
Console.WriteLine($"> Inizializzata la fase di creazione '{libName}'."); Console.WriteLine($"> Inizializzata la fase di creazione '{libName}'.");
Web web = ctx.Web; 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.Title = libName;
lci.TemplateType = (int)ListTemplateType.DocumentLibrary; lci.TemplateType = (int)ListTemplateType.DocumentLibrary;
List list = web.Lists.Add(lci); List list = web.Lists.Add(lci);
ctx.ExecuteQuery(); ctx.ExecuteQuery();
if (libName.Contains("Commerciale")) DomainGroupRights(ctx, libName);
Console.WriteLine($"> '{libName}' è stato creato con successo."); Console.WriteLine($"> '{libName}' è stato creato con successo.");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"?> Qualcosa è andato storto... {ex.Message}"); Console.WriteLine($"!> Qualcosa è andato storto... {ex.Message}");
return;
} }
#endregion #endregion
@@ -537,7 +548,8 @@ namespace SharePointOnlineUtils
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"?> Qualcosa è andato storto... ( {ex.Message} )"); Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )");
return;
} }
#endregion #endregion
} }
@@ -603,7 +615,8 @@ namespace SharePointOnlineUtils
} }
catch (Exception ex) 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}"); Console.WriteLine($"> Il menu rapido è stato aggiornato in {ssProjectTitle}");
} }
catch (Exception ex) catch (Exception ex)
{ Console.WriteLine($"?> Qualcosa è andato storto... ( {ex.Message} )"); } { Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )"); return; }
} }
} }
#endregion #endregion
#region SubProject #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 try
{ {
@@ -727,7 +740,8 @@ namespace SharePointOnlineUtils
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"?> Qualcosa è andato storto... ( {ex.Message} )"); Console.WriteLine($"!> Qualcosa è andato storto... ( {ex.Message} )");
return;
} }
} }
#endregion #endregion
@@ -738,6 +752,7 @@ namespace SharePointOnlineUtils
var featureName = "SiteNotebook"; var featureName = "SiteNotebook";
Guid featureId = new Guid("f151bb39-7c3b-414f-bb36-6bf18872052f"); Guid featureId = new Guid("f151bb39-7c3b-414f-bb36-6bf18872052f");
#region Feature Activate
if (string.IsNullOrEmpty(ssProjectTitle)) if (string.IsNullOrEmpty(ssProjectTitle))
{ {
try try
@@ -749,9 +764,12 @@ namespace SharePointOnlineUtils
Console.WriteLine($"> La Feature {featureName} è ora attiva!"); 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 try
{ {
@@ -767,7 +785,85 @@ namespace SharePointOnlineUtils
Console.WriteLine($"> Completata la fase di creazione della sezione {ssProjectTitle} in OneNote."); 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<string, string> itsADGruopRole = new Dictionary<string, string>()
{
{ "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<string, string> itsADGruopSalesRole = new Dictionary<string, string>()
{
{ "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<string, string> 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<string, string> 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 #endregion