update directory name and access visibility

This commit is contained in:
Kalarumeth
2022-09-29 08:33:32 +02:00
parent 3ab3afe25e
commit f258a284d6
86 changed files with 253 additions and 361 deletions
@@ -0,0 +1,204 @@
using console_spo_utils.Model;
using Microsoft.SharePoint.Client;
using System.Collections.Concurrent;
using System.Text;
using System.Text.Json;
using System.Web;
using Microsoft.Extensions.Logging;
using console_spo_utils.Interfaces.Services;
namespace console_spo_utils.Services
{
internal sealed class SharePointAuthenticationManager : IDisposable, ISharePointAuthenticationManager
{
private readonly ILogger<SharePointAuthenticationManager> logger;
private readonly ISiteOptions siteOptions;
public SharePointAuthenticationManager(
ILogger<SharePointAuthenticationManager> logger,
ISiteOptions siteOptions)
{
this.logger = logger;
this.siteOptions = siteOptions;
}
private static readonly HttpClient HttpClient = new();
private static readonly SemaphoreSlim SemaphoreSlimTokens = new(1);
private AutoResetEvent tokenResetEvent = null;
private readonly ConcurrentDictionary<string, string> tokenCache = new();
private bool disposedValue;
#region CSOM
public ClientContext GetContext(Uri web)
{
var context = new ClientContext(web);
context.ExecutingWebRequest += (sender, e) =>
{
var accessToken = EnsureAccessTokenAsync(new Uri($"{web.Scheme}://{web.DnsSafeHost}"),
siteOptions.GetUser(),
new System.Net.NetworkCredential(string.Empty, siteOptions.GetPassword()).Password)
.GetAwaiter()
.GetResult();
e.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + accessToken;
};
return context;
}
public async Task<string> EnsureAccessTokenAsync(Uri resourceUri, string userPrincipalName, string userPassword)
{
var accessTokenFromCache = TokenFromCache(resourceUri, tokenCache);
if (accessTokenFromCache == null)
{
await SemaphoreSlimTokens.WaitAsync();
try
{
// No async methods are allowed in a lock section
var accessToken = await AcquireTokenAsync(resourceUri, userPrincipalName, userPassword);
logger.LogInformation($"Successfully requested new access token resource {resourceUri.DnsSafeHost} for user {userPrincipalName}");
AddTokenToCache(resourceUri, tokenCache, accessToken);
// Register a thread to invalidate the access token once's it's expired
tokenResetEvent = new AutoResetEvent(false);
var wi = new TokenWaitInfo();
wi.Handle = ThreadPool.RegisterWaitForSingleObject(
tokenResetEvent,
(state, timedOut) =>
{
if (!timedOut)
{
var internalWaitToken = (TokenWaitInfo)state;
if (internalWaitToken?.Handle is not null)
{
internalWaitToken.Handle.Unregister(null);
}
}
else
{
try
{
// Take a lock to ensure no other threads are updating the SharePoint Access token at this time
SemaphoreSlimTokens.Wait();
RemoveTokenFromCache(resourceUri, tokenCache);
logger.LogInformation($"Cached token for resource {resourceUri.DnsSafeHost} and user {userPrincipalName} expired");
}
catch (Exception ex)
{
logger.LogInformation($"Something went wrong during cache token invalidation: {ex.Message}");
RemoveTokenFromCache(resourceUri, tokenCache);
}
finally
{
SemaphoreSlimTokens.Release();
}
}
},
wi,
(uint)CalculateThreadSleep(accessToken).TotalMilliseconds,
true
);
return accessToken;
}
finally
{
SemaphoreSlimTokens.Release();
}
}
else
{
logger.LogInformation("OK - Execution Query");
return accessTokenFromCache;
}
}
private async Task<string> AcquireTokenAsync(Uri resourceUri, string username, string password)
{
var resource = $"{resourceUri.Scheme}://{resourceUri.DnsSafeHost}";
var clientId = siteOptions.DefaultAadAppId;
var body = $"resource={resource}&client_id={clientId}&grant_type=password&username={HttpUtility.UrlEncode(username)}&password={HttpUtility.UrlEncode(password)}";
using var stringContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded");
var result = await HttpClient.PostAsync(siteOptions.TokenEndpoint, stringContent).ContinueWith((response) =>
{
return response.Result.Content.ReadAsStringAsync().Result;
}).ConfigureAwait(false);
var tokenResult = JsonSerializer.Deserialize<JsonElement>(result);
var token = tokenResult.GetProperty("access_token").GetString();
return token;
}
private static string TokenFromCache(Uri web, ConcurrentDictionary<string, string> tokenCache)
{
if (tokenCache.TryGetValue(web.DnsSafeHost, out var accessToken))
{
return accessToken;
}
return null;
}
private static void AddTokenToCache(Uri web, ConcurrentDictionary<string, string> tokenCache, string newAccessToken)
{
if (tokenCache.TryGetValue(web.DnsSafeHost, out var currentAccessToken))
{
tokenCache.TryUpdate(web.DnsSafeHost, newAccessToken, currentAccessToken);
}
else
{
tokenCache.TryAdd(web.DnsSafeHost, newAccessToken);
}
}
private static void RemoveTokenFromCache(Uri web, ConcurrentDictionary<string, string> tokenCache)
{
tokenCache.TryRemove(web.DnsSafeHost, out _);
}
private static TimeSpan CalculateThreadSleep(string accessToken)
{
var token = new System.IdentityModel.Tokens.Jwt.JwtSecurityToken(accessToken);
var lease = GetAccessTokenLease(token.ValidTo);
lease = TimeSpan.FromSeconds(lease.TotalSeconds - TimeSpan.FromMinutes(5).TotalSeconds > 0 ? lease.TotalSeconds - TimeSpan.FromMinutes(5).TotalSeconds : lease.TotalSeconds);
return lease;
}
private static TimeSpan GetAccessTokenLease(DateTime expiresOn)
{
var now = DateTime.UtcNow;
var expires = expiresOn.Kind == DateTimeKind.Utc ? expiresOn : TimeZoneInfo.ConvertTimeToUtc(expiresOn);
var lease = expires - now;
return lease;
}
private void Dispose(bool disposing)
{
if (disposedValue)
{
return;
}
if (disposing)
{
if (tokenResetEvent is not null)
{
tokenResetEvent.Set();
tokenResetEvent.Dispose();
}
}
disposedValue = true;
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
#endregion
}
}
@@ -0,0 +1,58 @@
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
namespace console_spo_utils.Services;
internal class NonComplianceBuildService : INonComplianceBuildService
{
private readonly ISiteOptions siteOptions;
private readonly ISharePointAuthenticationManager sharePointAuthenticationManager;
private readonly ISharePointCustomOperation spc;
private readonly ITenantService tenantService;
private readonly INonComplianceDocLibraryService nonComplianceDocLibraryService;
private readonly INonComplianceDocSetService nonComplianceDocSetService;
private readonly ILogger<NonComplianceBuildService> logger;
public NonComplianceBuildService(
ISiteOptions siteOptions,
ISharePointAuthenticationManager sharePointAuthenticationManager,
ISharePointCustomOperation spc,
ITenantService tenantService,
INonComplianceDocLibraryService nonComplianceDocLibraryService,
INonComplianceDocSetService nonComplianceDocSetService,
ILogger<NonComplianceBuildService> logger)
{
this.siteOptions = siteOptions;
this.sharePointAuthenticationManager = sharePointAuthenticationManager;
this.spc = spc;
this.tenantService = tenantService;
this.nonComplianceDocLibraryService = nonComplianceDocLibraryService;
this.nonComplianceDocSetService = nonComplianceDocSetService;
this.logger = logger;
}
public void CreateIfNotExists(string nonComplianceName)
{
var site = siteOptions.GetNonComplianceSite();
var ctx = sharePointAuthenticationManager.GetContext(site);
var list = siteOptions.GetNonComplianceLibrary();
var tenant = siteOptions.GetNonComplianceTenant();
if (!spc.SiteExist(ctx))
{
tenantService.CreateForNonCompliance();
}
else if (!spc.ListExist(ctx, list))
{
nonComplianceDocLibraryService.Create(list, ctx);
}
else if (!spc.FolderExistsInsideList(ctx, list, nonComplianceName))
{
nonComplianceDocSetService.Create(nonComplianceName, list, tenant, ctx);
}
else
{
logger.LogInformation($"Quotation with {nonComplianceName} already exist");
}
}
}
@@ -0,0 +1,115 @@
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
namespace console_spo_utils.Services;
internal class NonComplianceDocLibraryService : INonComplianceDocLibraryService
{
private readonly ILogger<NonComplianceDocLibraryService> logger;
public NonComplianceDocLibraryService(ILogger<NonComplianceDocLibraryService> logger)
{
this.logger = logger;
}
public void Create(string libName, ClientContext ctx)
{
try
{
#region New DocLib
Console.WriteLine($"> Inizializzata la fase di creazione '{libName}'.");
Web web = ctx.Web;
ctx.Load(web, w => w.RootFolder.WelcomePage, w => w.Url);
ListCreationInformation lci = new ListCreationInformation();
lci.Title = libName;
lci.TemplateType = (int)ListTemplateType.DocumentLibrary;
web.Lists.Add(lci);
ctx.ExecuteQuery();
Console.WriteLine($"> Completata la fase di creazione '{libName}'.");
#endregion
#region Field
Console.WriteLine($"> Inizializzata la fase di importazione dei campi '{libName}'.");
List list = ctx.Web.Lists.GetByTitle(libName);
var siteField = ctx.Site.RootWeb;
ctx.Load(list, l => l.Fields);
ctx.Load(siteField, s => s.Fields);
ctx.ExecuteQuery();
string[] fieldName = { "Sorgente", "Rifermento", "Nominativo", "Data Rilevazione", "Commessa", "Parte", "Portal" };
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 '{libName}'.");
#endregion
#region View
Console.WriteLine($"> Inizializzata la fase di creazione della ListView '{libName}'.");
var views = list.Views;
ViewCreationInformation viewCreation = new ViewCreationInformation();
viewCreation.SetAsDefaultView = true;
viewCreation.Title = libName;
viewCreation.ViewTypeKind = ViewType.None;
viewCreation.ColumnWidth = "350";
viewCreation.ViewFields = new string[] { "Type", "Name", "Sorgente", "Rifermento", "Nominativo", "Data Rilevazione", "Commessa", "Parte", "Portal" };
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 = $"{libName}";
newNavNode.Url = $"{web.Url}/{libName.Replace(" ", "%20").Replace("à", "")}";
newNavNode.AsLastNode = true;
spNavNodeColl.Add(newNavNode);
ctx.ExecuteQuery();
Console.WriteLine($"> Il menu rapido è stato aggiornato, il collegamento a {libName} è ora disponibile.");
#endregion
}
catch (Exception ex)
{
logger.LogError(ex, "NonComplianceDocLibraryService");
}
}
}
@@ -0,0 +1,108 @@
using console_spo_utils.Constants;
using console_spo_utils.Interfaces.Repositories;
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.DocumentSet;
namespace console_spo_utils.Services;
internal class NonComplianceDocSetService : INonComplianceDocSetService
{
private readonly ISharePointCustomOperation cpt;
private readonly IOneNoteService oneNoteService;
private readonly INonComplianceRepository nonComplianceRepository;
public NonComplianceDocSetService(
ISharePointCustomOperation cpt,
IOneNoteService oneNoteService,
INonComplianceRepository nonComplianceRepository)
{
this.cpt = cpt;
this.oneNoteService = oneNoteService;
this.nonComplianceRepository = nonComplianceRepository;
}
public void Create(string nonComplianceName, string docLibraryName, string tenantName, ClientContext ctx)
{
#region Context
var web = ctx.Web;
ctx.Load(web, w => w.Url);
List list = web.Lists.GetByTitle(docLibraryName);
ctx.Load(list, l => l.RootFolder, l => l.ContentTypes, l => l.Fields, l => l.ContentTypesEnabled);
ctx.ExecuteQuery();
list.ContentTypesEnabled = true;
list.Update();
if (!cpt.ListContentTypeExist(ctx, docLibraryName, "Set di documenti"))
{
var documentCT = ctx.Site.RootWeb.AvailableContentTypes.GetById("0x0120D5");
ctx.Load(documentCT);
ctx.ExecuteQuery();
var ctDocSet = new ContentTypeCreationInformation()
{
Name = "Set di documenti",
ParentContentType = documentCT
};
list.ContentTypes.Add(ctDocSet);
list.Update();
ctx.ExecuteQuery();
}
var ctData = list.ContentTypes.Where(c => c.Name == "Set di documenti");
var contentType = ctData.FirstOrDefault();
ctx.Load(contentType);
ctx.ExecuteQuery();
#endregion
Console.WriteLine($"> Inizializzata la fase di creazione per {nonComplianceName}.");
#region DocSet Field Entry
DocumentSet.Create(ctx, list.RootFolder, nonComplianceName, contentType.Id);
ctx.ExecuteQuery();
Console.WriteLine($"> DocumentSet {nonComplianceName} creata.");
var dsItem = list.RootFolder.Folders.GetByUrl(nonComplianceName).ListItemAllFields;
dsItem["PAL_NC_Source"] = nonComplianceRepository.DefaultNonComplianceSource();
dsItem["PAL_NC_Reference"] = nonComplianceRepository.DefaultNonComplianceReference();
dsItem["PAL_NC_Nominative"] = nonComplianceRepository.DefaultNonComplianceNominative();
dsItem["PAL_NC_DateOfDetection"] = nonComplianceRepository.DefaultNonComplianceDateOfDetection();
dsItem["PAL_NC_Project"] = nonComplianceRepository.DefaultNonComplianceProject();
dsItem["PAL_NC_ItemCode"] = nonComplianceRepository.DefaultNonComplianceItemCode();
dsItem["PAL_NC_PortalUrl"] = nonComplianceRepository.DefaultNonCompliancePortalUrl();
dsItem.Update();
ctx.ExecuteQuery();
Console.WriteLine($"> Field value update.");
#endregion
#region Quotation Folder
foreach (var name in Folders.OfferDocSet)
{
Console.WriteLine($"La sotto cartella {name} verrà creata in {nonComplianceName}");
var rPath = ResourcePath.FromDecodedUrl($"{nonComplianceName}/{name}");
list.RootFolder.AddSubFolderUsingPath(rPath);
}
ctx.ExecuteQuery();
oneNoteService.CreateFolderInsideNonCompliance(nonComplianceName, ctx);
Console.WriteLine($"Le sotto cartelle sono state create con successo in {nonComplianceName}");
#endregion
Console.WriteLine($"> {nonComplianceName} creato con successo in Offerte/{docLibraryName}.");
}
}
@@ -0,0 +1,131 @@
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
namespace console_spo_utils.Services;
internal class OneNoteService : IOneNoteService
{
private readonly ILogger<OneNoteService> logger;
private readonly ISiteOptions siteOptions;
private readonly ISharePointCustomOperation spc;
public OneNoteService(
ILogger<OneNoteService> logger,
ISiteOptions siteOptions,
ISharePointCustomOperation spc)
{
this.logger = logger;
this.siteOptions = siteOptions;
this.spc = spc;
}
public bool CreateFolderInsideProject(string projName, ClientContext ctx)
{
try
{
logger.LogInformation($"> Inizializzata la fase di creazione della sezione {projName} in OneNote.");
var list = ctx.Web.Lists.EnsureSiteAssetsLibrary();
ctx.Load(list, l => l.RootFolder);
ctx.ExecuteQuery();
var rPath = ResourcePath.FromDecodedUrl($"{siteOptions.GetProjYearTenant()} Notebook/{projName}");
list.RootFolder.AddSubFolderUsingPath(rPath);
ctx.ExecuteQuery();
logger.LogInformation($"> Completata la fase di creazione della sezione {projName} in OneNote.");
}
catch (Exception ex)
{
logger.LogError(ex, "OnenoteSPFeature");
return false;
}
return true;
}
public bool CreateFolderInsideQuotation(string quotationName, ClientContext ctx)
{
try
{
logger.LogInformation($"> Inizializzata la fase di creazione della sezione {quotationName} in OneNote.");
var list = ctx.Web.Lists.EnsureSiteAssetsLibrary();
ctx.Load(list, l => l.RootFolder);
ctx.ExecuteQuery();
if (!spc.FolderExistsInsideList(ctx, $"{siteOptions.GetQuotationTenant()} Notebook", $"{DateTime.Today.Year}"))
{
var rYearPath = ResourcePath.FromDecodedUrl($"{siteOptions.GetQuotationTenant()} Notebook/{DateTime.Today.Year}");
list.RootFolder.AddSubFolderUsingPath(rYearPath);
ctx.ExecuteQuery();
}
var rPath = ResourcePath.FromDecodedUrl($"{siteOptions.GetQuotationTenant()} Notebook/{DateTime.Today.Year}/{quotationName}");
list.RootFolder.AddSubFolderUsingPath(rPath);
ctx.ExecuteQuery();
logger.LogInformation($"> Completata la fase di creazione della sezione {quotationName} in OneNote.");
}
catch (Exception ex)
{
logger.LogError(ex, "OnenoteSPFeature");
return false;
}
return true;
}
public bool CreateFolderInsideNonCompliance(string quotationName, ClientContext ctx)
{
try
{
logger.LogInformation($"> Inizializzata la fase di creazione della sezione {quotationName} in OneNote.");
var list = ctx.Web.Lists.EnsureSiteAssetsLibrary();
ctx.Load(list, l => l.RootFolder);
ctx.ExecuteQuery();
if (!spc.FolderExistsInsideList(ctx, $"{siteOptions.GetNonComplianceTenant()} Notebook", $"{DateTime.Today.Year}"))
{
var rYearPath = ResourcePath.FromDecodedUrl($"{siteOptions.GetNonComplianceTenant()} Notebook/{DateTime.Today.Year}");
list.RootFolder.AddSubFolderUsingPath(rYearPath);
ctx.ExecuteQuery();
}
var rPath = ResourcePath.FromDecodedUrl($"{siteOptions.GetNonComplianceTenant()} Notebook/{DateTime.Today.Year}/{quotationName}");
list.RootFolder.AddSubFolderUsingPath(rPath);
ctx.ExecuteQuery();
logger.LogInformation($"> Completata la fase di creazione della sezione {quotationName} in OneNote.");
}
catch (Exception ex)
{
logger.LogError(ex, "OnenoteSPFeature");
return false;
}
return true;
}
public void EnableFeature(ClientContext ctx)
{
var featureName = "SiteNotebook";
var featureId = new Guid("f151bb39-7c3b-414f-bb36-6bf18872052f");
#region Feature Activate
if (!spc.SiteFeaturesExist(ctx, featureName))
{
ctx.Web.ActivateFeature(featureId);
ctx.ExecuteQuery();
logger.LogInformation($"> La Feature {featureName} è ora attiva!");
}
#endregion
}
}
@@ -0,0 +1,86 @@
using console_spo_utils.Constants;
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
namespace console_spo_utils.Services;
internal class ProjectDocLibraryService : IProjectDocLibraryService
{
private readonly ILogger<ProjectDocLibraryService> logger;
private readonly IRightsService rightsService;
public ProjectDocLibraryService(
ILogger<ProjectDocLibraryService> logger,
IRightsService rightsService)
{
this.logger = logger;
this.rightsService = rightsService;
}
public void Create(string ssProjectTitle, ClientContext ctx)
{
try
{
BuildProjectDocumentLibrary(ssProjectTitle, ctx);
BuildProjectFolders(ssProjectTitle, ctx);
}
catch (Exception ex)
{
logger.LogError(ex, "Create Doc Lib");
}
}
private void BuildProjectFolders(string projName, ClientContext clientContext)
{
var projectFolders = Folders.GetProjectFolders(projName);
foreach (var (folderName, folderPath) in projectFolders)
{
logger.LogInformation($"> Inizializzata la fase di {folderName} in {folderPath}.");
var list = clientContext.Web.Lists.GetByTitle(folderPath);
var info = new ListItemCreationInformation
{
UnderlyingObjectType = FileSystemObjectType.Folder,
LeafName = folderName
};
var newItem = list.AddItem(info);
newItem["Title"] = folderName;
newItem.Update();
clientContext.ExecuteQuery();
logger.LogInformation($"> {folderName} creato con successo in {folderPath}.");
}
}
private void BuildProjectDocumentLibrary(string projName, ClientContext clientContext)
{
var docLibNames = Folders.GetProjectDocLib(projName);
foreach (var (libraryName, libraryType) in docLibNames)
{
logger.LogInformation($"> Inizializzata la fase di creazione '{libraryName}'.");
var web = clientContext.Web;
clientContext.Load(web);
var lci = new ListCreationInformation
{
Title = libraryName,
TemplateType = (int)libraryType
};
web.Lists.Add(lci);
clientContext.ExecuteQuery();
if (libraryName.Contains("Commerciale"))
{
rightsService.DomainGroupRights(clientContext, string.Empty, libraryName);
}
logger.LogInformation($"> '{libraryName}' è stato creato con successo.");
}
}
}
@@ -0,0 +1,86 @@
using console_spo_utils.Constants;
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Navigation;
namespace console_spo_utils.Services;
internal class ProjectQuickMenuService : IProjectQuickMenuService
{
private readonly ILogger<ProjectQuickMenuService> logger;
private readonly ISiteOptions siteOptions;
public ProjectQuickMenuService(ILogger<ProjectQuickMenuService> logger
, ISiteOptions siteOptions)
{
this.logger = logger;
this.siteOptions = siteOptions;
}
public void CreateForProject(string projName, ClientContext ctx)
{
try
{
ClearQuickMenu(ctx);
var subSiteUrl = siteOptions.GetSubProjSite(projName).ToString();
var itemQuickMenu = Folders.GetProjectQuickMenu(projName, subSiteUrl);
var spNavNodeColl = ctx.Web.Navigation.QuickLaunch;
var newNavNode = new NavigationNodeCreationInformation();
ctx.Load(ctx.Web, w => w.RootFolder.WelcomePage);
foreach (var (name, path) in itemQuickMenu)
{
newNavNode.Title = name;
newNavNode.Url = path;
newNavNode.AsLastNode = true;
spNavNodeColl.Add(newNavNode);
ctx.ExecuteQuery();
logger.LogInformation($"> Il menu rapido è stato aggiornato in {projName}");
if (name != $"SottoCommesse {projName}")
{
continue;
}
ctx.Web.RootFolder.WelcomePage = $"SottoCommesse%20{projName}/Forms/SottoCommesse%20{projName}.aspx";
ctx.Web.RootFolder.Update();
ctx.ExecuteQuery();
}
}
catch (Exception ex)
{
logger.LogError(ex, "CreateForProject");
}
}
private void ClearQuickMenu(ClientContext ctx)
{
try
{
NavigationNodeCollection navBar = ctx.Web.Navigation.QuickLaunch;
ctx.Load(navBar);
ctx.ExecuteQueryRetry();
int[] element = new[] { 1, 1, 1 };
foreach (var e in element)
{
navBar[e].DeleteObject();
ctx.ExecuteQuery();
}
}
catch (Exception ex)
{
logger.LogError(ex, "ClearQuickMenu");
}
}
}
@@ -0,0 +1,115 @@
using console_spo_utils.Constants;
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WebParts;
namespace console_spo_utils.Services;
internal class ProjectService : IProjectService
{
private readonly ISiteOptions siteOptions;
private readonly ISharePointAuthenticationManager sharePointAuthenticationManager;
private readonly IWebpartService webpartService;
private readonly ILogger<ProjectService> logger;
public ProjectService(ISiteOptions siteOptions,
ISharePointAuthenticationManager sharePointAuthenticationManager,
IWebpartService webpartService,
ILogger<ProjectService> logger)
{
this.siteOptions = siteOptions;
this.sharePointAuthenticationManager = sharePointAuthenticationManager;
this.webpartService = webpartService;
this.logger = logger;
}
public void BuildWebParts()
{
try
{
string wpName = siteOptions.GetProjYearTenant();
var listView = GetListsId(siteOptions.GetProjListTitle(), Fields.SynthticProjTitle);
var ctx = sharePointAuthenticationManager.GetContext(siteOptions.GetProjectSite());
Web web = ctx.Web;
var file = web.GetFileByServerRelativeUrl("/sites/Commesse/SitePages/Home.aspx");
ctx.Load(web);
ctx.ExecuteQuery();
LimitedWebPartManager mgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
string url = siteOptions.GetProjectYearSite() + "/Lists/" +
siteOptions.GetProjListTitle().Replace(" ", "%20") +
"/" + siteOptions.GetProjListTitle().Replace(" ", "%20") + ".aspx?viewid=";
string htmlSchema = $"<div><h1 style=\"font-weight: bold; font-size: 18pt; clear: both;\">{siteOptions.GetProjYearTenant()}</h1></div>" +
$"<div><p><a style=\"color: green\" href=\"{url}{listView[Fields.SynthticProjTitle[0]]}\">Progetti Macchina</a><br>Solo i progetti di vendita Macchine/Impianti</p></div>" +
$"<div><p><a style=\"color: green\" href=\"{url}{listView[Fields.SynthticProjTitle[1]]}\">Progetti Ricambi</a><br>Solo i progetti di vendita Ricambi</p></div>" +
$"<div><p><a style=\"color: green\" href=\"{url}{listView[Fields.SynthticProjTitle[2]]}\">Altri Progetti</a><br>Tutti gli altri progetti che non siano di vendita Macchine/Impianti o Ricambi</p></div>";
string xmlSchema = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<WebPart xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
" xmlns=\"http://schemas.microsoft.com/WebPart/v2\">" +
$"<Title>{siteOptions.GetProjYearTenant()}</Title><FrameType>None</FrameType>" +
"<Assembly>Microsoft.SharePoint, Version=13.0.0.0, Culture=neutral, " +
"PublicKeyToken=94de0004b6e3fcc5</Assembly>" +
"<TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>" +
"<ContentLink xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" />" +
"<Content xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\">" +
$"<![CDATA[{htmlSchema}]]></Content>" +
"<PartStorage xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" /></WebPart>";
webpartService.AddWebPart(web, mgr, xmlSchema, wpName, "Body", 0);
var qm = new NavigationNodeCreationInformation()
{
Title = siteOptions.GetProjYearTenant(),
Url = siteOptions.GetProjectYearSite().ToString(),
AsLastNode = true
};
web.Navigation.QuickLaunch.Add(qm);
ctx.ExecuteQuery();
}
catch (Exception ex)
{
logger.LogError(ex, "WebPart Build");
}
}
private Dictionary<string, string> GetListsId(string listTitle, string[] listViewTitle)
{
Dictionary<string, string> listViewId = new Dictionary<string, string>();
try
{
foreach (var lViewTitle in listViewTitle)
{
var ctx = sharePointAuthenticationManager.GetContext(siteOptions.GetProjectYearSite());
var list = ctx.Web.Lists.GetByTitle(listTitle);
ctx.Load(list.Views);
var listView = list.Views.GetByTitle(lViewTitle);
ctx.Load(listView, l => l.Id);
ctx.ExecuteQuery();
var id = listView.Id;
listViewId.Add(lViewTitle, id.ToString());
}
}
catch (Exception ex)
{
logger.LogError(ex, "Get List Id for Web Part");
}
return listViewId;
}
}
@@ -0,0 +1,169 @@
using console_spo_utils.Constants;
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
namespace console_spo_utils.Services;
internal class ProjectYearService : IProjectYearService
{
private readonly ILogger<ProjectYearService> logger;
private readonly IProjectService projectService;
public ProjectYearService(
ILogger<ProjectYearService> logger,
IProjectService projectService)
{
this.logger = logger;
this.projectService = projectService;
}
public void CreateList(string listTitle, ClientContext ctx)
{
try
{
#region New List
Console.WriteLine($"> Inizializzata la fase di creazione della lista '{listTitle}'.");
Web web = ctx.Web;
ListCreationInformation creationInfo = new ListCreationInformation();
creationInfo.Title = listTitle;
creationInfo.TemplateType = (int)ListTemplateType.GenericList;
List oList = web.Lists.Add(creationInfo);
ctx.ExecuteQuery();
ctx.Load(oList, l => l.Fields);
var fld = oList.Fields.GetByInternalNameOrTitle("Title");
if (fld != null)
{
fld.Required = false;
fld.SetShowInDisplayForm(false);
fld.SetShowInEditForm(false);
fld.SetShowInNewForm(false);
oList.Update();
ctx.ExecuteQuery();
}
Console.WriteLine($"> La lista '{listTitle}' è stato creato con successo.");
#endregion
#region Field
Console.WriteLine($"> Inizializzata la fase di importazione dei campi '{listTitle}'.");
List list = ctx.Web.Lists.GetByTitle(listTitle);
var siteField = ctx.Site.RootWeb;
ctx.Load(list, l => l.Fields);
ctx.Load(siteField, s => s.Fields);
ctx.ExecuteQuery();
foreach (var fn in Fields.SiteFields)
{
var Fields = siteField.Fields.GetByTitle(fn);
list.Fields.Add(Fields);
}
ctx.ExecuteQuery();
Console.WriteLine($"> Completata la fase di importazione dei campi '{listTitle}'.");
#endregion
#region View
Console.WriteLine($"> Inizializzata la fase di creazione della ListView '{listTitle}'.");
var views = list.Views;
ViewCreationInformation viewCreation = new ViewCreationInformation();
viewCreation.Title = listTitle;
viewCreation.ViewTypeKind = ViewType.Html;
viewCreation.ColumnWidth = "350";
viewCreation.ViewFields = Fields.SiteFields;
viewCreation.SetAsDefaultView = true;
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}'.");
ctx.Web.RootFolder.WelcomePage = $"Lists/{listTitle}/{listTitle}.aspx";
ctx.Web.RootFolder.Update();
ctx.ExecuteQuery();
CreateViewList(list, ctx);
projectService.BuildWebParts();
Console.WriteLine($"> Impostata come HomePage di '{viewCreation.Title}'");
#endregion
}
catch (Exception ex)
{
logger.LogError(ex, "Project Years");
}
}
public void CreateViewList(List list, ClientContext ctx)
{
try
{
foreach (KeyValuePair<string, string> listTitle in Fields.SyntheticProjList)
{
Console.WriteLine($"> Inizializzata la fase di creazione della ListView '{listTitle}'.");
var views = list.Views;
ViewCreationInformation viewCreation = new ViewCreationInformation();
viewCreation.Title = listTitle.Key;
viewCreation.ViewTypeKind = ViewType.Html;
viewCreation.ColumnWidth = "350";
viewCreation.ViewFields = Fields.SiteFields;
viewCreation.Query = $"<Where>{listTitle.Value}</Where>";
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.Update();
ctx.ExecuteQuery();
Console.WriteLine($"> Abilitata la visualizzazione su mobile '{viewCreation.Title}'.");
}
}
catch (Exception ex)
{
logger.LogError(ex, "Project Years");
}
}
}
@@ -0,0 +1,58 @@
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
namespace console_spo_utils.Services;
internal class QuotationBuildService : IQuotationBuildService
{
private readonly ISiteOptions siteOptions;
private readonly ISharePointAuthenticationManager sharePointAuthenticationManager;
private readonly ISharePointCustomOperation spc;
private readonly ITenantService tenantService;
private readonly IQuotationDocLibraryService quotationDocLibraryService;
private readonly IQuotationDocSetService quotationDocSetService;
private readonly ILogger<QuotationBuildService> logger;
public QuotationBuildService(
ISiteOptions siteOptions,
ISharePointAuthenticationManager sharePointAuthenticationManager,
ISharePointCustomOperation spc,
ITenantService tenantService,
IQuotationDocLibraryService quotationDocLibraryService,
IQuotationDocSetService quotationDocSetService,
ILogger<QuotationBuildService> logger)
{
this.siteOptions = siteOptions;
this.sharePointAuthenticationManager = sharePointAuthenticationManager;
this.spc = spc;
this.tenantService = tenantService;
this.quotationDocLibraryService = quotationDocLibraryService;
this.quotationDocSetService = quotationDocSetService;
this.logger = logger;
}
public void CreateIfNotExists(string quotationName)
{
var site = siteOptions.GetQuotationSite();
var ctx = sharePointAuthenticationManager.GetContext(site);
var list = siteOptions.GetQuotationLibrary();
var tenant = siteOptions.GetQuotationTenant();
if (!spc.SiteExist(ctx))
{
tenantService.CreateForQuotation();
}
else if (!spc.ListExist(ctx, list))
{
quotationDocLibraryService.Create(list, ctx);
}
else if (!spc.FolderExistsInsideList(ctx, list, quotationName))
{
quotationDocSetService.Create(quotationName, list, tenant, ctx);
}
else
{
logger.LogInformation($"Quotation with {quotationName} already exist");
}
}
}
@@ -0,0 +1,115 @@
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
namespace console_spo_utils.Services;
internal class QuotationDocLibraryService : IQuotationDocLibraryService
{
private readonly ILogger<QuotationDocLibraryService> logger;
public QuotationDocLibraryService(
ILogger<QuotationDocLibraryService> logger)
{
this.logger = logger;
}
public void Create(string libName, ClientContext ctx)
{
try
{
#region New DocLib
Console.WriteLine($"> Inizializzata la fase di creazione '{libName}'.");
Web web = ctx.Web;
ctx.Load(web, w => w.RootFolder.WelcomePage, w => w.Url);
ListCreationInformation lci = new ListCreationInformation();
lci.Title = libName;
lci.TemplateType = (int)ListTemplateType.DocumentLibrary;
web.Lists.Add(lci);
ctx.ExecuteQuery();
Console.WriteLine($"> Completata la fase di creazione '{libName}'.");
#endregion
#region Field
Console.WriteLine($"> Inizializzata la fase di importazione dei campi '{libName}'.");
List list = ctx.Web.Lists.GetByTitle(libName);
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 '{libName}'.");
#endregion
#region View
Console.WriteLine($"> Inizializzata la fase di creazione della ListView '{libName}'.");
var views = list.Views;
ViewCreationInformation viewCreation = new ViewCreationInformation();
viewCreation.SetAsDefaultView = true;
viewCreation.Title = libName;
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 = $"{libName}";
newNavNode.Url = $"{web.Url}/{libName.Replace(" ", "%20")}";
newNavNode.AsLastNode = true;
spNavNodeColl.Add(newNavNode);
ctx.ExecuteQuery();
Console.WriteLine($"> Il menu rapido è stato aggiornato, il collegamento a {libName} è ora disponibile.");
#endregion
}
catch (Exception ex)
{
logger.LogError(ex, "QuotationDocLibraryService");
}
}
}
@@ -0,0 +1,114 @@
using console_spo_utils.Constants;
using console_spo_utils.Interfaces.Repositories;
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.DocumentSet;
namespace console_spo_utils.Services;
internal class QuotationDocSetService : IQuotationDocSetService
{
private readonly ISharePointCustomOperation cpt;
private readonly ILogger<QuotationDocSetService> logger;
private readonly IOneNoteService oneNoteService;
private readonly IQuotationRepository quotationRepository;
public QuotationDocSetService(ISharePointCustomOperation cpt,
ILogger<QuotationDocSetService> logger,
IOneNoteService oneNoteService,
IQuotationRepository quotationRepository)
{
this.cpt = cpt;
this.logger = logger;
this.oneNoteService = oneNoteService;
this.quotationRepository = quotationRepository;
}
public void Create(string quotationName, string docLibraryName, string tenantName, ClientContext ctx)
{
try
{
#region Context
var web = ctx.Web;
ctx.Load(web, w => w.Url);
List list = web.Lists.GetByTitle(docLibraryName);
ctx.Load(list, l => l.RootFolder, l => l.ContentTypes, l => l.Fields, l => l.ContentTypesEnabled);
ctx.ExecuteQuery();
list.ContentTypesEnabled = true;
list.Update();
if (!cpt.ListContentTypeExist(ctx, docLibraryName, "Set di documenti"))
{
var documentCT = ctx.Site.RootWeb.AvailableContentTypes.GetById("0x0120D5");
ctx.Load(documentCT);
ctx.ExecuteQuery();
var ctDocSet = new ContentTypeCreationInformation()
{
Name = "Set di documenti",
ParentContentType = documentCT
};
list.ContentTypes.Add(ctDocSet);
list.Update();
ctx.ExecuteQuery();
}
var ctData = list.ContentTypes.Where(c => c.Name == "Set di documenti");
var contentType = ctData.FirstOrDefault();
ctx.Load(contentType);
ctx.ExecuteQuery();
#endregion
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"] = quotationRepository.DefaultIdQuotation(quotationName);
dsItem["PAL_Quotation_Name"] = quotationRepository.DefaultQuotationName(quotationName);
dsItem["PAL_Quotation_Reason"] = quotationRepository.DefaultQuotationReason(quotationName);
dsItem["PAL_Authors"] = quotationRepository.DefaultAuthors(quotationName);
dsItem.Update();
ctx.ExecuteQuery();
Console.WriteLine($"> Field value update.");
#endregion
#region Quotation Folder
foreach (var name in Folders.OfferDocSet)
{
Console.WriteLine($"La sotto cartella {name} verrà creata in {quotationName}");
var rPath = ResourcePath.FromDecodedUrl($"{quotationName}/{name}");
list.RootFolder.AddSubFolderUsingPath(rPath);
}
ctx.ExecuteQuery();
oneNoteService.CreateFolderInsideQuotation(quotationName, ctx);
Console.WriteLine($"Le sotto cartelle sono state create con successo in {quotationName}");
#endregion
Console.WriteLine($"> {quotationName} creato con successo in Offerte/{docLibraryName}.");
}
catch (Exception ex)
{
logger.LogError(ex, "");
}
}
}
+146
View File
@@ -0,0 +1,146 @@
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
namespace console_spo_utils.Services
{
internal class RightsService : IRightsService
{
private readonly ILogger<RightsService> logger;
public RightsService(ILogger<RightsService> logger)
{
this.logger = logger;
}
#region Rights
public void DomainGroupRights(ClientContext ctx, string tenantName, string docLibName = "")
{
#region Groups & Privileges
var itsAdGroupDictionary = 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" }
};
var itsAdGruopSalesRole = new Dictionary<string, string>()
{
{ "ITS-SPO-PROJ-OWNER", "Full Control" },
{ "ITS-SPO-PROJ-SALES-MODIFY", "Edit" },
{ "ITS-SPO-PROJ-SALES-READ", "Read" }
};
var itsAdGruopNCRole = new Dictionary<string, string>()
{
{ "ITS-SPO-NC-OWNER", "Full Control" },
{ "ITS-SPO-NC-MODIFY", "Edit" },
{ "ITS-SPO-NC-READ", "Read" }
};
#endregion
logger.LogInformation("> Inizializzata la fase di assegnazione dei ruoli.");
try
{
if (tenantName.Contains("Commesse"))
{
#region Site Permission Create
var web = ctx.Web;
foreach (var role in itsAdGroupDictionary)
{
var adGroup = web.EnsureUser(role.Key);
ctx.Load(adGroup);
var roleD = web.RoleDefinitions.GetByName(role.Value);
var roleDb = new RoleDefinitionBindingCollection(ctx) { roleD };
web.RoleAssignments.Add(adGroup, roleDb);
web.Update();
}
ctx.ExecuteQuery();
logger.LogInformation("> Completata la fase di assegnazione dei ruoli.");
#endregion
}
else if (tenantName.Contains("Offerte"))
{
#region Site Permission Quotation
var web = ctx.Web;
foreach (var role in itsAdGruopSalesRole)
{
var adGroup = web.EnsureUser(role.Key);
ctx.Load(adGroup);
var roleD = web.RoleDefinitions.GetByName(role.Value);
var roleDb = new RoleDefinitionBindingCollection(ctx) { roleD };
web.RoleAssignments.Add(adGroup, roleDb);
web.Update();
}
ctx.ExecuteQuery();
logger.LogInformation("> Completata la fase di assegnazione dei ruoli.");
#endregion
}
else if (tenantName.Contains("Conformità"))
{
#region Site Permission Quotation
var web = ctx.Web;
foreach (var role in itsAdGruopNCRole)
{
var adGroup = web.EnsureUser(role.Key);
ctx.Load(adGroup);
var roleD = web.RoleDefinitions.GetByName(role.Value);
var roleDb = new RoleDefinitionBindingCollection(ctx) { roleD };
web.RoleAssignments.Add(adGroup, roleDb);
web.Update();
}
ctx.ExecuteQuery();
logger.LogInformation("> Completata la fase di assegnazione dei ruoli.");
#endregion
}
else if (!string.IsNullOrEmpty(docLibName))
{
#region DocLib Permission
var web = ctx.Web;
var list = web.Lists.GetByTitle(docLibName);
ctx.Load(list);
list.BreakRoleInheritance(false, true);
ctx.ExecuteQuery();
foreach (var role in itsAdGruopSalesRole)
{
var adGroup = web.EnsureUser(role.Key);
ctx.Load(adGroup);
var roleD = web.RoleDefinitions.GetByName(role.Value);
var roleDb = new RoleDefinitionBindingCollection(ctx) { roleD };
list.RoleAssignments.Add(adGroup, roleDb);
list.Update();
}
ctx.ExecuteQuery();
logger.LogInformation($"> Completata la fase di assegnazione dei ruoli in {docLibName}.");
#endregion
}
}
catch (Exception ex)
{
logger.LogError("Rights Service", ex);
}
}
#endregion
}
}
@@ -0,0 +1,159 @@
using console_spo_utils.Enums;
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Online.SharePoint.TenantAdministration;
using Microsoft.SharePoint.Client;
using System.Security;
using System.Xml.Linq;
using console_spo_utils.Constants;
namespace console_spo_utils.Services
{
internal class SharePointCustomOperation : ISharePointCustomOperation
{
private readonly ILogger logger;
private readonly IRightsService rightsService;
private readonly ISharePointAuthenticationManager authMgr;
public SharePointCustomOperation(
ILogger<SharePointCustomOperation> logger,
IRightsService rightsService,
ISharePointAuthenticationManager authMgr)
{
this.logger = logger;
this.rightsService = rightsService;
this.authMgr = authMgr;
}
#region Check If Exist
public bool SiteExist(ClientContext ctx)
{
try
{
var web = ctx.Web;
ctx.Load(web, w => w.Title);
ctx.ExecuteQuery();
return true;
}
catch (Exception ex)
{
logger.LogWarning("Site Exists", ex);
return false;
}
}
public bool ListExist(ClientContext ctx, string listTitle)
{
try
{
var targetList = ctx.Web.Lists.GetByTitle(listTitle);
ctx.ExecuteQuery();
return true;
}
catch (Exception ex)
{
logger.LogWarning("List Exist", ex);
return false;
}
}
public bool FolderExistsInsideList(ClientContext context, string listTitle, string folderName)
{
var folderExists = context.Web.Lists.GetByTitle(listTitle).RootFolder;
context.Load(folderExists, f => f.Folders);
try
{
context.ExecuteQuery();
if (folderExists.Folders.Any(fn => fn.Name == folderName))
{
return true;
}
return false;
}
catch (Exception ex)
{
logger.LogWarning("Folder Exists Inside List", ex);
return false;
}
}
public bool SiteFeaturesExist(ClientContext ctx, string featureName)
{
var web = ctx.Web;
var wFeatures = web.Features;
ctx.Load(web);
ctx.Load(wFeatures);
ctx.ExecuteQuery();
foreach (var wFeature in wFeatures)
{
ctx.Load(wFeature, wf => wf.DisplayName);
ctx.ExecuteQuery();
logger.LogInformation(wFeature.DisplayName);
if (wFeature.DisplayName == featureName) return true;
}
return false;
}
public bool ListContentTypeExist(ClientContext ctx, string listTitle, string contentTypeName)
{
ContentTypeCollection cntCollection = ctx.Web.Lists.GetByTitle(listTitle).ContentTypes;
ctx.Load(cntCollection, cntyp => cntyp.Include(ct => ct.Name).Where(ct => ct.Name == contentTypeName));
ctx.ExecuteQuery();
if (!cntCollection.Any())
{
Console.WriteLine(cntCollection.Count);
return false;
}
else
{
Console.WriteLine(cntCollection.Count);
return true;
}
}
#endregion
public void PalCustomField(ClientContext ctx, PalFieldType et)
{
var fieldList = et switch
{
PalFieldType.Project => Fields.ProjectCustomFields,
PalFieldType.Quotation => Fields.QuotationCustomFields,
PalFieldType.NonCompliance => Fields.NonComplianceCustomFields,
_ => throw new NotImplementedException("Entity type not found")
};
try
{
var site = ctx.Site;
ctx.Load(site, w => w.RootWeb, w => w.RootWeb.Fields);
ctx.ExecuteQuery();
foreach (var 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>";
site.RootWeb.Fields.AddFieldAsXml(fieldXml, false, AddFieldOptions.AddToDefaultContentType);
ctx.Load(site.RootWeb.Fields);
}
ctx.ExecuteQuery();
}
catch (Exception ex)
{
logger.LogError("PalCustomField", ex);
}
}
}
}
@@ -0,0 +1,82 @@
using System.Linq.Expressions;
using console_spo_utils.Interfaces.Repositories;
using console_spo_utils.Interfaces.Services;
namespace console_spo_utils.Services;
internal class SharePointStructureBuilderService : ISharePointStructureBuilder
{
private readonly ISiteService siteService;
private readonly ISubProjectRepository subProjectRepository;
private readonly ISubProjectBuilderService subProjectBuilderService;
private readonly IQuotationBuildService quotationBuildService;
private readonly INonComplianceBuildService nonComplianceBuildService;
private readonly IWebpartService webpartService;
public SharePointStructureBuilderService(
ISiteService siteService,
ISubProjectRepository subProjectRepository,
ISubProjectBuilderService subProjectBuilderService,
IQuotationBuildService quotationBuildService,
INonComplianceBuildService nonComplianceBuildService,
IWebpartService webpartService)
{
this.siteService = siteService;
this.subProjectRepository = subProjectRepository;
this.subProjectBuilderService = subProjectBuilderService;
this.quotationBuildService = quotationBuildService;
this.nonComplianceBuildService = nonComplianceBuildService;
this.webpartService = webpartService;
}
public bool BuildProject(string projName)
{
var siteBuilderResult = siteService.CreateProjectSiteIfNotExists();
if (!siteBuilderResult)
{
throw new Exception($"Impossibile to build site");
}
var subSiteBuilderResult = siteService.CreateSubSiteIfNotExists(projName);
if (!subSiteBuilderResult)
{
throw new Exception($"Impossibile to build subSite for {projName} ");
}
var subProjects = subProjectRepository.GetFromProject(projName);
var subProjectBuilderResult = subProjectBuilderService.SubProjectDocSet(projName, subProjects);
if (!subProjectBuilderResult)
{
throw new Exception($"Impossibile to build subSite for {projName} ");
}
return true;
}
public bool BuildQuotation(string quotationName)
{
var siteBuildResult = siteService.CreateQuotationSiteIfNotExists(quotationName);
if (!siteBuildResult)
{
throw new Exception($"Impossibile to build site");
}
quotationBuildService.CreateIfNotExists(quotationName);
return true;
}
public bool BuildNonCompliance(string nonComplianceName)
{
var siteBuildResult = siteService.CreateNonComplianceSiteIfNotExists(nonComplianceName);
if (!siteBuildResult)
{
throw new Exception($"Impossibile to build site");
}
nonComplianceBuildService.CreateIfNotExists(nonComplianceName);
return true;
}
}
+92
View File
@@ -0,0 +1,92 @@
using System.Net;
using System.Security;
using console_spo_utils.Interfaces.Services;
namespace console_spo_utils.Services;
public class SiteOptions : ISiteOptions
{
private const string SiteCollection = "https://italsortbuttrio.sharepoint.com";
public string TokenEndpoint { get; } = "https://login.microsoftonline.com/common/oauth2/token";
public string DefaultAadAppId { get; } = "46e6296e-176f-4ebb-a14b-bdd5678c16e6";
public Uri GetProjectYearSite()
{
return new Uri($"{SiteCollection}/sites/{GetProjYearTenant().Replace(" ","")}");
}
private static int GetYear()
{
return DateTime.Today.Year;
}
public string GetProjYearTenant()
{
return $"Commesse {GetYear()}";
}
public string GetUser()
{
return "svcItsSharePointAdmin@italsort.com";
}
public SecureString GetPassword()
{
return new NetworkCredential("", "$O,D1XBp1O5.OdjZt86#a=").SecurePassword;
}
public string GetProjListTitle()
{
return $"Lista {GetProjYearTenant()}";
}
public Uri GetSubProjSite(string projName)
{
return new Uri($"{GetProjectYearSite()}/{projName}");
}
public string GetQuotationTenant()
{
return "Offerte";
}
public string GetQuotationLibrary()
{
return $"{GetQuotationTenant()} {GetYear()}";
}
public Uri GetQuotationSite()
{
return new Uri($"{SiteCollection}/sites/{GetQuotationTenant()}");
}
public string GetSubProjList(string projName)
{
return $"SottoCommesse {projName}";
}
private string GetProjectTenant()
{
return "Commesse";
}
public Uri GetProjectSite()
{
return new Uri($"{SiteCollection}/sites/{GetProjectTenant()}");
}
public string GetNonComplianceTenant()
{
return "Non Conformità";
}
public string GetNonComplianceLibrary()
{
return $"{GetNonComplianceTenant()} {GetYear()}";
}
public Uri GetNonComplianceSite()
{
return new Uri($"{SiteCollection}/sites/{GetNonComplianceTenant().Replace(" ", String.Empty)}");
}
}
+185
View File
@@ -0,0 +1,185 @@
using console_spo_utils.Enums;
using console_spo_utils.Interfaces.Repositories;
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
namespace console_spo_utils.Services;
internal class SiteService : ISiteService
{
private readonly ILogger<SiteService> logger;
private readonly ISharePointAuthenticationManager authMgr;
private readonly ISharePointCustomOperation spc;
private readonly ISiteOptions siteOptions;
private readonly ISubSiteService subSiteService;
private readonly IOneNoteService oneNoteService;
private readonly IProjectSettingsRepository projectSettingsRepository;
private readonly IProjectDocLibraryService projectDocLibraryService;
private readonly IProjectQuickMenuService projectQuickMenuService;
private readonly ITenantService tenantService;
private readonly IProjectYearService projectYearService;
public SiteService(
ILogger<SiteService> logger,
ISharePointAuthenticationManager authMgr,
ISharePointCustomOperation spc,
ISiteOptions siteOptions,
ISubSiteService subSiteService,
IOneNoteService oneNoteService,
IProjectSettingsRepository projectSettingsRepository,
IProjectDocLibraryService projectDocLibraryService,
IProjectQuickMenuService projectQuickMenuService,
ITenantService tenantService,
IProjectYearService projectYearService)
{
this.logger = logger;
this.authMgr = authMgr;
this.spc = spc;
this.siteOptions = siteOptions;
this.subSiteService = subSiteService;
this.oneNoteService = oneNoteService;
this.projectSettingsRepository = projectSettingsRepository;
this.projectDocLibraryService = projectDocLibraryService;
this.projectQuickMenuService = projectQuickMenuService;
this.tenantService = tenantService;
this.projectYearService = projectYearService;
}
public bool CreateProjectSiteIfNotExists()
{
try
{
var site = siteOptions.GetProjectYearSite();
var listTitle = siteOptions.GetProjListTitle();
using var ctx = authMgr.GetContext(site);
if (spc.SiteExist(ctx))
{
return true;
}
tenantService.CreateForProject();
projectYearService.CreateList(listTitle,ctx);
}
catch (Exception e)
{
logger.LogError(e, "Site Service");
return false;
}
return true;
}
public bool CreateSubSiteIfNotExists(string projName)
{
var projectSite = siteOptions.GetProjectYearSite();
var subProjSite = siteOptions.GetSubProjSite(projName);
var siteContext = authMgr.GetContext(projectSite);
var subSiteContext = authMgr.GetContext(subProjSite);
if (!spc.SiteExist(siteContext))
{
throw new Exception($"Site {siteContext.Url} not exists");
}
if (spc.SiteExist(subSiteContext))
{
logger.LogInformation($"The subsite {subSiteContext.Url} already exists");
return true;
}
var existSubSite = subSiteService.Create(projName, siteContext);
if (!existSubSite)
{
return true;
}
oneNoteService.CreateFolderInsideProject(projName, siteContext);
CreateProjectListEntry(projName, siteContext);
projectDocLibraryService.Create(projName, subSiteContext);
subSiteService.AddColumnsToListView(projName, subSiteContext);
projectQuickMenuService.CreateForProject(projName, subSiteContext);
return true;
}
public bool CreateQuotationSiteIfNotExists(string quotationName)
{
try
{
var site = siteOptions.GetQuotationSite();
using var ctx = authMgr.GetContext(site);
if (spc.SiteExist(ctx))
{
return true;
}
tenantService.CreateForQuotation();
}
catch (Exception e)
{
logger.LogError(e, "Site Service");
return false;
}
return true;
}
public bool CreateNonComplianceSiteIfNotExists(string nonComplianceName)
{
try
{
var site = siteOptions.GetNonComplianceSite();
using var ctx = authMgr.GetContext(site);
if (spc.SiteExist(ctx))
{
return true;
}
tenantService.CreateForNonCompliance();
}
catch (Exception e)
{
logger.LogError(e, "Site Service");
return false;
}
return true;
}
private void CreateProjectListEntry(string projName, ClientContext ctx)
{
try
{
var listTitle = siteOptions.GetProjListTitle();
var tenant = siteOptions.GetProjYearTenant().Replace(" ",string.Empty);
var list = ctx.Web.Lists.GetByTitle(listTitle);
var itemCreateInfo = new ListItemCreationInformation();
var oItem = list.AddItem(itemCreateInfo);
var link = new FieldUrlValue
{
Url = $"/sites/{tenant}/{projName}",
Description = projName
};
oItem["PAL_ID_Project"] = link;
oItem["PAL_Customer"] = projectSettingsRepository.DefaultCostumer(projName);
oItem["PAL_DlvReason"] = projectSettingsRepository.DefaultDlvReason(projName);
oItem.Update();
ctx.ExecuteQuery();
logger.LogInformation($"> La Commessa {projName} è stata aggiunta alla lista {listTitle} con successo!");
}
catch (Exception ex)
{
logger.LogError(ex, "Create List Entry");
}
}
}
@@ -0,0 +1,128 @@
using console_spo_utils.Constants;
using console_spo_utils.Interfaces.Repositories;
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client.DocumentSet;
using Microsoft.SharePoint.Client;
namespace console_spo_utils.Services;
internal class SubProjectBuilderService : ISubProjectBuilderService
{
private readonly ISiteOptions siteOptions;
private readonly ISharePointCustomOperation cpt;
private readonly ISharePointAuthenticationManager authMgr;
private readonly ISubProjectRepository subProjectRepository;
private readonly ILogger<SubProjectBuilderService> logger;
public SubProjectBuilderService(ISiteOptions siteOptions,
ISharePointCustomOperation cpt,
ISharePointAuthenticationManager authMgr,
ISubProjectRepository subProjectRepository,
ILogger<SubProjectBuilderService> logger)
{
this.siteOptions = siteOptions;
this.cpt = cpt;
this.authMgr = authMgr;
this.subProjectRepository = subProjectRepository;
this.logger = logger;
}
public bool SubProjectDocSet(string projName, List<string> subProjTitle)
{
try
{
#region Context
var listTitle = siteOptions.GetSubProjList(projName);
var subSite = siteOptions.GetSubProjSite(projName);
var ctx = authMgr.GetContext(subSite);
var web = ctx.Web;
ctx.Load(web, w => w.Url);
var list = web.Lists.GetByTitle(listTitle);
ctx.Load(list, l => l.RootFolder, l => l.ContentTypes, l => l.Fields, l => l.ContentTypesEnabled);
ctx.ExecuteQuery();
list.ContentTypesEnabled = true;
list.Update();
if (cpt.ListContentTypeExist(ctx, listTitle, "Set di documenti") == false)
{
var documentCT = ctx.Site.RootWeb.AvailableContentTypes.GetById("0x0120D5");
ctx.Load(documentCT);
ctx.ExecuteQuery();
var ctDocSet = new ContentTypeCreationInformation()
{
Name = "Set di documenti",
ParentContentType = documentCT
};
list.ContentTypes.Add(ctDocSet);
list.Update();
ctx.ExecuteQuery();
}
var ctData = list.ContentTypes.Where(c => c.Name == "Set di documenti");
var contentType = ctData.FirstOrDefault();
ctx.Load(contentType);
ctx.ExecuteQuery();
#endregion
foreach (var spt in subProjTitle)
{
if (cpt.FolderExistsInsideList(ctx, listTitle, spt) == false)
{
Console.WriteLine($"> Inizializzata la fase di creazione per {spt}.");
#region DocSet Field Entry
DocumentSet.Create(ctx, list.RootFolder, spt, contentType.Id);
ctx.ExecuteQuery();
Console.WriteLine($"> DocumentSet {spt} creata.");
var dsItem = list.RootFolder.Folders.GetByUrl(spt).ListItemAllFields;
dsItem["_ExtendedDescription"] = subProjectRepository.DefaultDescription(projName);
dsItem["PAL_Item"] = subProjectRepository.DefaultItem(projName);
dsItem["PAL_ItemCode"] = subProjectRepository.DefaultItemCode(projName);
dsItem["PAL_ItemDescription"] = subProjectRepository.DefaultItemDescription(projName);
dsItem["PAL_SerialNumber"] = subProjectRepository.DefaultSerialNumber(projName);
dsItem.Update();
ctx.ExecuteQuery();
Console.WriteLine($"> Field value update.");
#endregion
#region SubProject Folder
foreach (var name in Folders.SubProjectDocSet)
{
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 SottoCommesse {projName}.");
}
}
}
catch (Exception ex)
{
logger.LogError(ex,"Sub Proj Doc Set");
return false;
}
return true;
}
}
@@ -0,0 +1,114 @@
using console_spo_utils.Constants;
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
namespace console_spo_utils.Services;
internal class SubSiteService : ISubSiteService
{
private readonly ILogger<SubSiteService> logger;
private readonly IProjectQuickMenuService projectQuickMenuService;
public SubSiteService(ILogger<SubSiteService> logger,
IProjectQuickMenuService projectQuickMenuService)
{
this.logger = logger;
this.projectQuickMenuService = projectQuickMenuService;
}
public bool Create(string projName, ClientContext siteContext)
{
try
{
logger.LogInformation($"> Il sito {projName} è in fase di creazione!");
var wci = new WebCreationInformation
{
Url = projName,
Title = projName,
UseSamePermissionsAsParentSite = true,
WebTemplate = "SITEPAGEPUBLISHING#0"
};
var web = siteContext.Site.RootWeb.Webs.Add(wci);
siteContext.ExecuteQuery();
logger.LogInformation($"> Il sito {projName} è stato creato con successo!");
}
catch (Exception ex)
{
logger.LogError("Create Sub Site", ex);
return false;
}
return true;
}
public void AddColumnsToListView(string ssProjectTitle, ClientContext ctx)
{
try
{
var listTitle = $"SottoCommesse {ssProjectTitle}";
#region Field
logger.LogInformation($"> Inizializzata la fase di importazione dei campi in 'SottoCommesse {ssProjectTitle}'.");
var web = ctx.Web;
var list = web.Lists.GetByTitle(listTitle);
var siteField = ctx.Site.RootWeb;
ctx.Load(list, l => l.Fields);
ctx.Load(siteField, s => s.Fields);
ctx.ExecuteQuery();
AddFieldsToSubProj(siteField, list, ctx);
logger.LogInformation($"> Completata la fase di importazione dei campi in 'SottoCommesse {ssProjectTitle}'.");
#endregion
#region View
logger.LogInformation($"> Inizializzata la fase di creazione della ListView '{listTitle}'.");
var views = list.Views;
var viewCreation = new ViewCreationInformation
{
SetAsDefaultView = true,
Title = listTitle,
ViewTypeKind = ViewType.None,
ColumnWidth = "350",
ViewFields = Fields.SubProject
};
var view = views.Add(viewCreation);
ctx.Load(view);
ctx.ExecuteQuery();
logger.LogInformation($"> 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();
logger.LogInformation($"> Abilitata la visualizzazione su mobile '{viewCreation.Title}'.");
#endregion
}
catch (Exception ex)
{
logger.LogError("Create Sub Site", ex);
}
}
private static void AddFieldsToSubProj(Web siteField, List list, ClientContext ctx)
{
foreach (var fieldName in Fields.SubSiteProject)
{
var fields = siteField.Fields.GetByTitle(fieldName);
list.Fields.Add(fields);
}
ctx.ExecuteQuery();
}
}
+108
View File
@@ -0,0 +1,108 @@
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Online.SharePoint.TenantAdministration;
using console_spo_utils.Enums;
namespace console_spo_utils.Services;
internal class TenantService : ITenantService
{
private readonly ISharePointAuthenticationManager authMgr;
private readonly ISharePointCustomOperation cpt;
private readonly IRightsService rightsService;
private readonly IOneNoteService oneNoteService;
private readonly ILogger<TenantService> logger;
private readonly ISiteOptions siteOptions;
public TenantService(
ISharePointAuthenticationManager authMgr,
ISharePointCustomOperation cpt,
IRightsService rightsService,
IOneNoteService oneNoteService,
ILogger<TenantService> logger,
ISiteOptions siteOptions)
{
this.authMgr = authMgr;
this.cpt = cpt;
this.rightsService = rightsService;
this.oneNoteService = oneNoteService;
this.logger = logger;
this.siteOptions = siteOptions;
}
public void CreateForProject()
{
TenantCreation(
siteOptions.GetProjYearTenant(),
siteOptions.GetProjectYearSite(),
PalFieldType.Project
);
}
public void CreateForQuotation()
{
TenantCreation(
siteOptions.GetQuotationTenant(),
siteOptions.GetQuotationSite(),
PalFieldType.Quotation
);
}
public void CreateForNonCompliance()
{
TenantCreation(
siteOptions.GetNonComplianceTenant(),
siteOptions.GetNonComplianceSite(),
PalFieldType.NonCompliance
);
}
public void TenantCreation(string tenantName, Uri site, PalFieldType fieldType)
{
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);
var tenantCtx = authMgr.GetContext(path);
var ctx = authMgr.GetContext(site);
try
{
var tenant = new Tenant(tenantCtx);
var scp = new SiteCreationProperties
{
Url = site.ToString(),
Title = tenantName,
Owner = siteOptions.GetUser(),
Template = "SITEPAGEPUBLISHING#0"
};
SpoOperation spo = tenant.CreateSite(scp);
tenantCtx.Load(tenant);
tenantCtx.Load(spo, i => i.IsComplete);
tenantCtx.ExecuteQuery();
while (!spo.IsComplete)
{
Console.WriteLine($"Il sito '../{scp.Title}' è in fase di pubblicazione.");
System.Threading.Thread.Sleep(30000);
spo.RefreshLoad();
tenantCtx.ExecuteQuery();
}
Console.WriteLine($"> Il sito è stato creato con successo. ({site})");
rightsService.DomainGroupRights(ctx, tenantName, string.Empty);
oneNoteService.EnableFeature(ctx);
cpt.PalCustomField(ctx, fieldType);
}
catch (Exception ex)
{
logger.LogError(ex, "Tenant Creation");
}
}
}
@@ -0,0 +1,40 @@
using console_spo_utils.Interfaces.Services;
using Microsoft.Extensions.Logging;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WebParts;
namespace console_spo_utils.Services
{
internal class WebpartService : IWebpartService
{
private readonly ILogger<WebpartService> logger;
private readonly ISharePointAuthenticationManager authMgr;
private readonly ISiteOptions siteOptions;
public WebpartService(
ILogger<WebpartService> logger,
ISharePointAuthenticationManager authMgr,
ISiteOptions siteOptions)
{
this.logger = logger;
this.authMgr = authMgr;
this.siteOptions = siteOptions;
}
public void AddWebPart(Web web, LimitedWebPartManager mgr, string xmlSchema, string listName, string zoneName, int zoneId)
{
try
{
WebPartDefinition def = mgr.ImportWebPart(xmlSchema);
def.WebPart.Hidden = false;
mgr.AddWebPart(def.WebPart, zoneName, zoneId);
mgr.Context.ExecuteQuery();
}
catch (Exception ex)
{
logger.LogError(ex, "Project Years");
}
}
}
}