128 lines
4.6 KiB
C#
128 lines
4.6 KiB
C#
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;
|
|
}
|
|
} |