This commit is contained in:
Kalarumeth
2022-07-21 18:25:52 +02:00
parent a020c184ce
commit 59cc2299d6
+325 -37
View File
@@ -1,6 +1,6 @@
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.DocumentSet;
using Microsoft.Online.SharePoint.TenantAdministration;
using System;
using System.Collections.Concurrent;
@@ -23,15 +23,17 @@ namespace SharePointOnlineUtils
string svcUser = "svcItsSharePointAdmin@italsort.com";
SecureString svcUserPsw = new NetworkCredential("", "$O,D1XBp1O5.OdjZt86#a=").SecurePassword;
ProjectsByYearSite(siteCollection, svcUser, svcUserPsw);
//ProjectsByYearSite(siteCollection, svcUser, svcUserPsw);
//Project(siteCollection, svcUser, svcUserPsw);
SubProject(siteCollection, svcUser, svcUserPsw);
}
public static async Task ProjectsByYearSite(string siteCollection, string user, SecureString psw)
{
string year = DateTime.Now.Year.ToString();
string projectsYear = "Commesse" + year;
var listTitle = "Lista Commesse " + year;
Uri site = new Uri(siteCollection + "/Commesse/" + projectsYear);
string listTitle = "Lista Commesse " + year;
Uri site = new Uri(siteCollection + "/sites/" + projectsYear);
using (var authMgr = new AuthenticationManager())
using (var ctx = authMgr.GetContext(site, user, psw))
@@ -55,10 +57,64 @@ namespace SharePointOnlineUtils
}
}
}
public static async Task Project(string siteCollection, string user, SecureString psw)
{
string year = DateTime.Now.Year.ToString();
string projectsYear = $"Commesse{year}";
string listTitle = $"Lista Commesse {year}";
string ssProjectTitle = "CT0014";
Uri site = new Uri($"{siteCollection}/sites/{projectsYear}");
using (var authMgr = new AuthenticationManager())
using (var ctx = authMgr.GetContext(site, user, psw))
{
//if (SharePointCustomOperation.SiteExist(ctx)) //TODO: Controllo if sub site exist
SharePointCustomOperation.ProjectSubSite(ssProjectTitle, projectsYear, ctx);
SharePointCustomOperation.ProjectListEntry(ssProjectTitle, listTitle, projectsYear, ctx);
}
Uri subSite = new Uri($"{siteCollection}/sites/{projectsYear}/{ssProjectTitle}");
using (var authMgr = new AuthenticationManager())
using (var ctx = authMgr.GetContext(subSite, user, psw))
{
SharePointCustomOperation.ProjectDocLib(ssProjectTitle, ctx);
//view e field + edit short cut menu + set SottoProgetti view a homepage
}
}
public static async Task SubProject(string siteCollection, string user, SecureString psw)
{
string year = DateTime.Now.Year.ToString();
string projectsYear = $"Commesse{year}";
string listTitle = $"Lista Commesse {year}";
string ssProjectTitle = "CT0014";
string[] ssSubProjectTitle = { "CT0014-0001", "CT0014-0002", "CT0014-0003" };
Uri subSite = new Uri($"{siteCollection}/sites/{projectsYear}/{ssProjectTitle}");
Uri subProjList = new Uri($"{siteCollection}/sites/{projectsYear}/{ssProjectTitle}/SottoProgetti {ssProjectTitle}");
using (var authMgr = new AuthenticationManager())
using (var ctx = authMgr.GetContext(subSite, user, psw))
{
using (var sCtx = authMgr.GetContext(subProjList, user, psw))
{
SharePointCustomOperation.SubProjectDocSet(ssProjectTitle, ssSubProjectTitle, ctx, sCtx);
}
}
}
}
public class SharePointCustomOperation
{
#region Check If Exist
public static bool SiteExist(ClientContext ctx)
{
try
@@ -73,7 +129,7 @@ namespace SharePointOnlineUtils
return false;
}
}
public static bool ListExist(string year, ClientContext ctx)
{
try
@@ -87,7 +143,9 @@ namespace SharePointOnlineUtils
return false;
}
}
#endregion
#region Projects Years Page Set Up
public static async Task ProjectsYSite(string year, Uri site, string user, SecureString psw)
{
Console.WriteLine($"> Inizializzata la fase di creazione del sito '../Commesse{year}'.");
@@ -132,11 +190,6 @@ namespace SharePointOnlineUtils
}
}
public static async Task ProjectsYQuickMenu(ClientContext ctx)
{
}
public static async Task ProjectsYList(string listTitle, ClientContext ctx)
{
try
@@ -181,38 +234,32 @@ namespace SharePointOnlineUtils
Console.WriteLine($"> Inizializzata la fase di creazione dei campi '{listTitle}'.");
Web web = ctx.Web;
List projectsList = web.Lists.GetByTitle(listTitle);
List list = web.Lists.GetByTitle(listTitle);
List projectsList = web.Lists.GetByTitle(listTitle);
ctx.Load(list, l => l.Fields);
ctx.Load(projectsList, b => b.Id);
ctx.ExecuteQuery();
string idProjField = @"<Field Type='URL' Name='PAL_ID_Project' StaticName='ID Progetto' DisplayName='ID Progetto' Required='TRUE' />";
string idProjField = @"<Field Type='URL' Name='PAL_ID_Project' StaticName='PAL_ID_Project' DisplayName='ID Progetto' Required='TRUE' />";
Field lookupIdProjField = list.Fields.AddFieldAsXml(idProjField, true, AddFieldOptions.DefaultValue);
//lookupIdProjField.Update();
//ctx.Load(lookupIdProjField);
string clientField = @"<Field Type='Text' Name='PAL_Client' StaticName='Cliente' DisplayName='Cliente' Required='TRUE' />";
string clientField = @"<Field Type='Text' Name='PAL_Customer' StaticName='PAL_Customer' DisplayName='Cliente' Required='TRUE' />";
Field lookupClientField = list.Fields.AddFieldAsXml(clientField, true, AddFieldOptions.DefaultValue);
//lookupClientField.Update();
//ctx.Load(lookupClientField);
string dlvReasonField = @"<Field Type='Choice' Name='PAL_DlvReason' StaticName='Causale' DisplayName='Causale' Required='TRUE' Format='RadioButtons' >"
+ "<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 dlvReasonField = @"<Field Type='Choice' Name='PAL_DlvReason' StaticName='PAL_DlvReason' DisplayName='Causale' Required='TRUE' Format='RadioButtons' >"
+ "<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>"
+ "</Field>";
Field lookupDlvReasonField = list.Fields.AddFieldAsXml(dlvReasonField, true, AddFieldOptions.DefaultValue);
//lookupDlvReasonField.Update();
//ctx.Load(lookupDlvReasonField);
ctx.ExecuteQuery();
@@ -232,7 +279,6 @@ namespace SharePointOnlineUtils
List list = ctx.Web.Lists.GetByTitle(listTitle);
var views = list.Views;
//ctx.Load(list, l => l.Views);
ViewCreationInformation viewCreation = new ViewCreationInformation();
@@ -242,14 +288,14 @@ namespace SharePointOnlineUtils
viewCreation.ViewTypeKind = ViewType.None;
viewCreation.ColumnWidth = "350";
viewCreation.ViewFields = new string[3] { "ID Progetto", "Cliente", "Causale" };
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;
@@ -257,15 +303,257 @@ namespace SharePointOnlineUtils
customView.Update();
ctx.ExecuteQuery();
Console.WriteLine($"> MobileView set to true on ListView '{viewCreation.Title}'.");
Console.WriteLine($"> Abilitata la visualizzazione su mobile '{viewCreation.Title}'.");
ctx.Web.RootFolder.WelcomePage = $"Lists/{listTitle}/{listTitle}.aspx";
ctx.Web.RootFolder.Update();
ctx.ExecuteQuery();
Console.WriteLine($"> Impostata come HomePage di '{viewCreation.Title}'");
}
catch (Exception ex)
{
Console.WriteLine($"> Qualcosa è andato storto nella creazione della ListView. ( {ex.Message} )");
}
}
#endregion
#region Project Page Set Up
public static async Task ProjectSubSite(string ssProjectTitle, string projectsYear, ClientContext ctx)
{
string ssProjectDescription = "TEST AUTOMATION";
try
{
Console.WriteLine($"> Il sito {ssProjectTitle} è in fase di creazione!");
WebCreationInformation wci = new WebCreationInformation();
wci.Url = ssProjectTitle;
wci.Title = ssProjectTitle;
wci.Description = ssProjectDescription;
wci.UseSamePermissionsAsParentSite = true;
wci.WebTemplate = "STS#1";
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);
}
}
public static async Task ProjectListEntry(string ssProjectTitle, string listTitle, string projectsYear, ClientContext ctx)
{
try
{
List list = ctx.Web.Lists.GetByTitle(listTitle);
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oItem = list.AddItem(itemCreateInfo);
FieldUrlValue link = new FieldUrlValue();
link.Url = $"/sites/{projectsYear}/{ssProjectTitle}";
link.Description = ssProjectTitle;
oItem["ID_x0020_Progetto"] = link;
oItem["Cliente"] = "PAL";
oItem["Causale"] = "02 VENDITA RICAMBI";
oItem.Update();
ctx.ExecuteQuery();
Console.WriteLine($"> La Commessa {ssProjectTitle} è stata aggiunta alla lista {listTitle} con successo!");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public static async Task ProjectDocLib(string ssProjectTitle, ClientContext ctx)
{
#region DocLib
try
{
string[] docLibName = { $"Commerciale {ssProjectTitle}", $"Documenti {ssProjectTitle}", $"Media {ssProjectTitle}", $"SottoProgetti {ssProjectTitle}" };
foreach (string libName in docLibName)
{
Console.WriteLine($"> Inizializzata la fase di creazione '{libName}'.");
Web web = ctx.Web;
ListCreationInformation lci = new ListCreationInformation();
lci.Title = libName;
lci.TemplateType = (int)ListTemplateType.DocumentLibrary;
List list = web.Lists.Add(lci);
ctx.ExecuteQuery();
if (libName == $"SottoProgetti {ssProjectTitle}")
{
list.ContentTypesEnabled = true;
list.AddContentTypeToListByName("Set di documenti");
list.Update();
ctx.ExecuteQuery();
}
var fld = list.Fields.GetByInternalNameOrTitle(libName);
if (fld != null)
{
fld.Required = false;
fld.SetShowInDisplayForm(false);
fld.SetShowInEditForm(false);
fld.SetShowInNewForm(false);
list.Update();
ctx.ExecuteQuery();
}
Console.WriteLine($"> '{libName}' è stato creato con successo.");
}
}
catch (Exception ex)
{
Console.WriteLine($"> Qualcosa è andato storto... {ex.Message}");
}
#endregion
#region Dir
try
{
Dictionary<string, string> folderName = new Dictionary<string, string>
{
{ "01_Costificazioni", $"Commerciale {ssProjectTitle}" },
{ "02_Conferma_Ordine", $"Commerciale {ssProjectTitle}" },
{ "03_Amministrazione", $"Commerciale {ssProjectTitle}" },
{ "05_Documenti_di_Spedizione", $"Documenti {ssProjectTitle}" },
{ "07_Service", $"Documenti {ssProjectTitle}" },
{ "08_Specifiche_Tecniche_E_Meeting", $"Documenti {ssProjectTitle}" },
{ "10_Fornitori", $"Documenti {ssProjectTitle}" },
{ "11_Documenti_Tecnici", $"Documenti {ssProjectTitle}" }
};
foreach (KeyValuePair<string, string> kvp in folderName)
{
Console.WriteLine($"> Inizializzata la fase di {kvp.Key} in {kvp.Value}.");
List list = ctx.Web.Lists.GetByTitle(kvp.Value);
ListItemCreationInformation info = new ListItemCreationInformation();
info.UnderlyingObjectType = FileSystemObjectType.Folder;
info.LeafName = kvp.Key;
ListItem newItem = list.AddItem(info);
newItem["Title"] = kvp.Key;
newItem.Update();
ctx.ExecuteQuery();
Console.WriteLine($"> {kvp.Key} creato con successo in {kvp.Value}.");
}
}
catch (Exception ex)
{
Console.WriteLine($"> Qualcosa è andato storto... ( {ex.Message} )");
}
#endregion
}
public static async Task ProjectListField(string ssProjectTitle, ClientContext ctx) // Empty Da definire i campi che userà ITS
{
try
{
string[] listTitle = { $"Commerciale {ssProjectTitle}", $"Documenti {ssProjectTitle}", $"Media {ssProjectTitle}", $"SottoProgetti {ssProjectTitle}" };
foreach(string title in listTitle)
{
Console.WriteLine($"> Inizializzata la fase di creazione dei campi in '{title}'.");
Web web = ctx.Web;
List list = web.Lists.GetByTitle(title);
List projectsList = web.Lists.GetByTitle(title);
ctx.Load(list, l => l.Fields);
ctx.Load(projectsList, b => b.Id);
ctx.ExecuteQuery();
Field lookupField = list.Fields.AddFieldAsXml(@"<Field Type='Text' Name='PAL_Customer' StaticName='PAL_Customer' DisplayName='Cliente' Required='FALSE' />", true, AddFieldOptions.DefaultValue);
ctx.ExecuteQuery();
Console.WriteLine($"> Completata la fase di creazione dei campi in '{title}'.");
}
}
catch (Exception ex)
{
Console.WriteLine($"> Qualcosa è andato storto nella creazione delle colonne. ( {ex.Message} )");
}
}
public static async Task ProjectListView(string ssProjectTitle, ClientContext ctx) // Empty
{
}
#endregion
#region SubProject
public static async Task SubProjectDocSet(string ssProjectTitle, string[] ssSubProjectTitle, ClientContext ctx, ClientContext sCtx)
{
try
{
string[] folderName = { "09_Documenti_Tecnici", "12_Disegni_Costruttivi", "13_Manuali", "15_Documentazione_Elettrica", "16_Liste_Ricambi" };
foreach (string spt in ssSubProjectTitle)
{
Console.WriteLine($"> Inizializzata la fase di creazione per {spt}.");
var list = ctx.Web.Lists.GetByTitle($"SottoProgetti {ssProjectTitle}");
ctx.Load(list, l => l.RootFolder, l => l.ContentTypes);
ctx.ExecuteQuery();
var ctData = list.ContentTypes.Where(c => c.Name == "Set di documenti");
var contentType = ctData.FirstOrDefault();
ctx.Load(contentType);
ctx.ExecuteQuery();
DocumentSet.Create(ctx, list.RootFolder, spt, contentType.Id);
ctx.ExecuteQuery();
Console.WriteLine($"> {spt} creato con successo in SottoProgetti {ssProjectTitle}.");
/*foreach (string f in folderName) Trovare il modo di creare delle cartelle nelle docset
{
Console.WriteLine($"> La cartella {f} è in fase di creazione in {spt}");
var subList = sCtx.Web.Lists.GetByTitle(spt);
ListItemCreationInformation info = new ListItemCreationInformation();
info.UnderlyingObjectType = FileSystemObjectType.Folder;
info.LeafName = f;
ListItem newItem = subList.AddItem(info);
newItem["Title"] = f;
newItem.Update();
ctx.ExecuteQuery();
Console.WriteLine($"> La cartella {f} è stata creata in {spt}");
}*/
Console.WriteLine($"> Il Sotto Progrtto {ssSubProjectTitle} è stato creato con successo.");
}
}
catch (Exception ex)
{
Console.WriteLine($"> Qualcosa è andato storto... ( {ex.Message} )");
}
}
#endregion
}
public class AuthenticationManager : IDisposable