160 lines
5.0 KiB
C#
160 lines
5.0 KiB
C#
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);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|