This commit is contained in:
Kalarumeth
2022-07-22 18:11:40 +02:00
parent 59cc2299d6
commit 74575bc3ff
+116 -54
View File
@@ -24,8 +24,8 @@ namespace SharePointOnlineUtils
SecureString svcUserPsw = new NetworkCredential("", "$O,D1XBp1O5.OdjZt86#a=").SecurePassword; SecureString svcUserPsw = new NetworkCredential("", "$O,D1XBp1O5.OdjZt86#a=").SecurePassword;
//ProjectsByYearSite(siteCollection, svcUser, svcUserPsw); //ProjectsByYearSite(siteCollection, svcUser, svcUserPsw);
//Project(siteCollection, svcUser, svcUserPsw); Project(siteCollection, svcUser, svcUserPsw);
SubProject(siteCollection, svcUser, svcUserPsw); //SubProject(siteCollection, svcUser, svcUserPsw);
} }
public static async Task ProjectsByYearSite(string siteCollection, string user, SecureString psw) public static async Task ProjectsByYearSite(string siteCollection, string user, SecureString psw)
@@ -66,22 +66,26 @@ namespace SharePointOnlineUtils
string ssProjectTitle = "CT0014"; string ssProjectTitle = "CT0014";
Uri site = new Uri($"{siteCollection}/sites/{projectsYear}"); Uri site = new Uri($"{siteCollection}/sites/{projectsYear}");
Uri subSite = new Uri($"{siteCollection}/sites/{projectsYear}/{ssProjectTitle}");
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)) //TODO: Controllo if sub site exist //if (SharePointCustomOperation.SiteExist(ctx)) //TODO: Controllo if sub site exist
SharePointCustomOperation.ProjectSubSite(ssProjectTitle, projectsYear, ctx); //SharePointCustomOperation.ProjectSubSite(ssProjectTitle, projectsYear, ctx);
SharePointCustomOperation.ProjectListEntry(ssProjectTitle, listTitle, projectsYear, ctx); //SharePointCustomOperation.ProjectListEntry(ssProjectTitle, listTitle, projectsYear, ctx);
} }
Uri subSite = new Uri($"{siteCollection}/sites/{projectsYear}/{ssProjectTitle}");
using (var authMgr = new AuthenticationManager()) using (var authMgr = new AuthenticationManager())
using (var ctx = authMgr.GetContext(subSite, user, psw)) using (var ctx = authMgr.GetContext(subSite, user, psw))
{ {
SharePointCustomOperation.ProjectDocLib(ssProjectTitle, ctx); //SharePointCustomOperation.ProjectDocLib(ssProjectTitle, ctx);
//view e field + edit short cut menu + set SottoProgetti view a homepage // edit short cut menu + set SottoProgetti view a homepage
//SharePointCustomOperation.ProjectSubProjListField(ssProjectTitle, ctx);
//SharePointCustomOperation.ProjectSubProjListView(ssProjectTitle, ctx);
SharePointCustomOperation.ProjectSubProjListView(ssProjectTitle, ctx);
} }
} }
@@ -91,27 +95,41 @@ namespace SharePointOnlineUtils
string projectsYear = $"Commesse{year}"; string projectsYear = $"Commesse{year}";
string listTitle = $"Lista Commesse {year}"; string listTitle = $"Lista Commesse {year}";
string ssProjectTitle = "CT0014"; string ssProjectTitle = "CT0014";
string[] ssSubProjectTitle = { "CT0014-0001", "CT0014-0002", "CT0014-0003" }; string[] ssSubProjectTitle = { "CT0015-0001", "CT0015-0002", "CT0015-0003" };
Uri subSite = new Uri($"{siteCollection}/sites/{projectsYear}/{ssProjectTitle}"); 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 authMgr = new AuthenticationManager())
using (var ctx = authMgr.GetContext(subSite, user, psw)) using (var ctx = authMgr.GetContext(subSite, user, psw))
{ {
using (var sCtx = authMgr.GetContext(subProjList, user, psw)) SharePointCustomOperation.SubProjectDocSet(ssProjectTitle, ssSubProjectTitle, ctx);
}
}
}
public class ParentProj
{ {
SharePointCustomOperation.SubProjectDocSet(ssProjectTitle, ssSubProjectTitle, ctx, sCtx); public string ProjId { get; set; }
public string Description { get; set; }
public string Customer { get; set; }
public string DeliveryReason { get; set; }
public List<SubProj> SubProjects { get; set; }
} }
} public class SubProj
} {
public string ProjId { get; set; }
public string Description { get; set; }
public string Customer { get; set; }
public string DeliveryReason { get; set; }
public string Position { get; set; }
public string ItemCode { get; set; }
public string ItemDescription { get; set; }
public string SerialNumber { get; set; }
} }
public class SharePointCustomOperation public class SharePointCustomOperation
{ {
#region Check If Exist #region Check If Exist
@@ -145,7 +163,7 @@ namespace SharePointOnlineUtils
} }
#endregion #endregion
#region Projects Years Page Set Up #region Projects by Years
public static async Task ProjectsYSite(string year, Uri site, string user, SecureString psw) public static async Task ProjectsYSite(string year, Uri site, string user, SecureString psw)
{ {
Console.WriteLine($"> Inizializzata la fase di creazione del sito '../Commesse{year}'."); Console.WriteLine($"> Inizializzata la fase di creazione del sito '../Commesse{year}'.");
@@ -319,10 +337,9 @@ namespace SharePointOnlineUtils
} }
#endregion #endregion
#region Project Page Set Up #region Project Page
public static async Task ProjectSubSite(string ssProjectTitle, string projectsYear, ClientContext ctx) public static async Task ProjectSubSite(string ssProjectTitle, string projectsYear, ClientContext ctx)
{ {
string ssProjectDescription = "TEST AUTOMATION";
try try
{ {
Console.WriteLine($"> Il sito {ssProjectTitle} è in fase di creazione!"); Console.WriteLine($"> Il sito {ssProjectTitle} è in fase di creazione!");
@@ -331,7 +348,7 @@ namespace SharePointOnlineUtils
wci.Url = ssProjectTitle; wci.Url = ssProjectTitle;
wci.Title = ssProjectTitle; wci.Title = ssProjectTitle;
wci.Description = ssProjectDescription;
wci.UseSamePermissionsAsParentSite = true; wci.UseSamePermissionsAsParentSite = true;
wci.WebTemplate = "STS#1"; wci.WebTemplate = "STS#1";
wci.Language = 1040; wci.Language = 1040;
@@ -463,30 +480,28 @@ namespace SharePointOnlineUtils
#endregion #endregion
} }
public static async Task ProjectListField(string ssProjectTitle, ClientContext ctx) // Empty Da definire i campi che userà ITS public static async Task ProjectSubProjListField(string ssProjectTitle, ClientContext ctx) // Empty Da definire i campi che userà ITS
{ {
try try
{ {
string[] listTitle = { $"Commerciale {ssProjectTitle}", $"Documenti {ssProjectTitle}", $"Media {ssProjectTitle}", $"SottoProgetti {ssProjectTitle}" }; Console.WriteLine($"> Inizializzata la fase di creazione dei campi in 'SottoProgetti {ssProjectTitle}'.");
foreach(string title in listTitle)
{
Console.WriteLine($"> Inizializzata la fase di creazione dei campi in '{title}'.");
Web web = ctx.Web; Web web = ctx.Web;
List list = web.Lists.GetByTitle(title); List list = web.Lists.GetByTitle($"SottoProgetti {ssProjectTitle}");
List projectsList = web.Lists.GetByTitle(title); List projectsList = web.Lists.GetByTitle($"SottoProgetti {ssProjectTitle}");
ctx.Load(list, l => l.Fields); ctx.Load(list, l => l.Fields);
ctx.Load(projectsList, b => b.Id); ctx.Load(projectsList, b => b.Id);
ctx.ExecuteQuery(); ctx.ExecuteQuery();
Field lookupField = list.Fields.AddFieldAsXml(@"<Field Type='Text' Name='PAL_Customer' StaticName='PAL_Customer' DisplayName='Cliente' Required='FALSE' />", true, AddFieldOptions.DefaultValue); Field lookupItemField = list.Fields.AddFieldAsXml(@"<Field Type='Text' Name='PAL_Item' StaticName='PAL_Item' DisplayName='Item' Required='FALSE' />", true, AddFieldOptions.DefaultValue);
Field lookupArticleNumberField = list.Fields.AddFieldAsXml(@"<Field Type='Text' Name='PAL_ArticleNumber' StaticName='PAL_ArticleNumber' DisplayName='Codice Articolo' Required='FALSE' />", true, AddFieldOptions.DefaultValue); // item cod
Field lookupArticleDescriptionField = list.Fields.AddFieldAsXml(@"<Field Type='Text' Name='PAL_ArticleDescription' StaticName='PAL_ArticleDescription' DisplayName='Descrizione Articolo' Required='FALSE' />", true, AddFieldOptions.DefaultValue);
Field lookupMachineSerialNumberField = list.Fields.AddFieldAsXml(@"<Field Type='Text' Name='PAL_MachineSerialNumber' StaticName='PAL_MachineSerialNumber' DisplayName='Matricola Macchina' Required='FALSE' />", true, AddFieldOptions.DefaultValue);
ctx.ExecuteQuery(); ctx.ExecuteQuery();
Console.WriteLine($"> Completata la fase di creazione dei campi in '{title}'."); Console.WriteLine($"> Completata la fase di creazione dei campi in 'SottoProgetti {ssProjectTitle}'.");
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -494,57 +509,104 @@ namespace SharePointOnlineUtils
} }
} }
public static async Task ProjectListView(string ssProjectTitle, ClientContext ctx) // Empty public static async Task ProjectSubProjListView(string ssProjectTitle, ClientContext ctx)
{ {
try
{
var listTitle = $"SottoProgetti {ssProjectTitle}";
Console.WriteLine($"> Inizializzata la fase di creazione della ListView '{listTitle}'.");
List list = ctx.Web.Lists.GetByTitle(listTitle);
var views = list.Views;
ViewCreationInformation viewCreation = new ViewCreationInformation();
viewCreation.SetAsDefaultView = true;
viewCreation.Title = listTitle;
viewCreation.ViewTypeKind = ViewType.None;
viewCreation.ColumnWidth = "350";
viewCreation.ViewFields = new string[] { "Tipo", "Nome", "Descrizione", "Item", "Codice Articolo", "Descrizione Articolo", "Matricola Macchina", "Data/ora modifica", "Modificato da", "Versione", };
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}'.");
}
catch (Exception ex)
{
Console.WriteLine($"> Qualcosa è andato storto nella creazione della ListView. ( {ex.Message} )");
}
} }
#endregion #endregion
#region SubProject #region SubProject
public static async Task SubProjectDocSet(string ssProjectTitle, string[] ssSubProjectTitle, ClientContext ctx, ClientContext sCtx) public static async Task SubProjectDocSet(string ssProjectTitle, string[] ssSubProjectTitle, ClientContext ctx)
{ {
try try
{ {
string[] folderName = { "09_Documenti_Tecnici", "12_Disegni_Costruttivi", "13_Manuali", "15_Documentazione_Elettrica", "16_Liste_Ricambi" };
foreach (string spt in ssSubProjectTitle) foreach (string spt in ssSubProjectTitle)
{ {
Console.WriteLine($"> Inizializzata la fase di creazione per {spt}."); Console.WriteLine($"> Inizializzata la fase di creazione per {spt}.");
#region List Context
var list = ctx.Web.Lists.GetByTitle($"SottoProgetti {ssProjectTitle}"); var list = ctx.Web.Lists.GetByTitle($"SottoProgetti {ssProjectTitle}");
ctx.Load(list, l => l.RootFolder, l => l.ContentTypes); ctx.Load(list, l => l.RootFolder, l => l.ContentTypes, l => l.Fields);
ctx.ExecuteQuery(); ctx.ExecuteQuery();
var ctData = list.ContentTypes.Where(c => c.Name == "Set di documenti"); var ctData = list.ContentTypes.Where(c => c.Name == "Set di documenti");
var contentType = ctData.FirstOrDefault(); var contentType = ctData.FirstOrDefault();
ctx.Load(contentType); ctx.Load(contentType);
ctx.ExecuteQuery(); ctx.ExecuteQuery();
#endregion
#region DocSet & Field value
DocumentSet.Create(ctx, list.RootFolder, spt, contentType.Id); DocumentSet.Create(ctx, list.RootFolder, spt, contentType.Id);
ctx.ExecuteQuery(); ctx.ExecuteQuery();
Console.WriteLine($"> {spt} creato con successo in SottoProgetti {ssProjectTitle}."); Console.WriteLine($"> DocumentSet {spt} creata.");
/*foreach (string f in folderName) Trovare il modo di creare delle cartelle nelle docset var dsItem = list.RootFolder.Folders.GetByUrl(spt).ListItemAllFields;
{
Console.WriteLine($"> La cartella {f} è in fase di creazione in {spt}");
dsItem["_ExtendedDescription"] = "valueDescription";
dsItem["Item"] = "valueItem";
dsItem["Codice_x0020_Articolo"] = "valueArticleNumber";
dsItem["Descrizione_x0020_Articolo"] = "valueArticleDescription";
dsItem["Matricola_x0020_Macchina"] = "valueMachineSerialNumber";
dsItem.Update();
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(); ctx.ExecuteQuery();
Console.WriteLine($"> La cartella {f} è stata creata in {spt}"); Console.WriteLine($"> Field value update.");
}*/ #endregion
Console.WriteLine($"> Il Sotto Progrtto {ssSubProjectTitle} è stato creato con successo."); #region SubProject Folder
string[] folderName = { "09_Documenti_Tecnici", "12_Disegni_Costruttivi", "13_Manuali", "15_Documentazione_Elettrica", "16_Liste_Ricambi" };
foreach (string name in folderName)
{
Console.WriteLine($"La sotto cartella {name} verrà creata in {spt}");
var rPath = ResourcePath.FromDecodedUrl($"{spt}/{name}");
list.RootFolder.AddSubFolderUsingPath(rPath);
}
ctx.ExecuteQuery();
Console.WriteLine($"Le sotto cartelle sono state create con successo in {spt}");
#endregion
Console.WriteLine($"> {spt} creato con successo in SottoProgetti {ssProjectTitle}.");
} }
} }
catch (Exception ex) catch (Exception ex)