using library_spo_utils.Constants; using library_spo_utils.Enums; using library_spo_utils.Interfaces.Services; using Microsoft.Extensions.Logging; using Microsoft.SharePoint.Client; namespace library_spo_utils.Services { internal class SharePointCustomOperation : ISharePointCustomOperation { private readonly ILogger logger; private readonly IRightsService rightsService; private readonly ISharePointAuthenticationManager authMgr; public SharePointCustomOperation( ILogger 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 FolderExist(ClientContext ctx, string path) { var folder = ctx.Web.GetFolderByServerRelativeUrl(path).Folders; ctx.Load(folder, f => f.Include(fi => fi.ListItemAllFields)); try { ctx.ExecuteQuery(); return true; } catch (Exception) { 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 = $""; site.RootWeb.Fields.AddFieldAsXml(fieldXml, false, AddFieldOptions.AddToDefaultContentType); ctx.Load(site.RootWeb.Fields); } ctx.ExecuteQuery(); } catch (Exception ex) { logger.LogError("PalCustomField", ex); } } } }