|
| 1 | +using System.Collections; |
| 2 | +using System.Management.Automation; |
| 3 | +using Microsoft.SharePoint.Client; |
| 4 | +using PnP.PowerShell.Commands.Base.Completers; |
| 5 | +using PnP.PowerShell.Commands.Base.PipeBinds; |
| 6 | +using PnP.PowerShell.Commands.Utilities; |
| 7 | + |
| 8 | +namespace PnP.PowerShell.Commands.Files |
| 9 | +{ |
| 10 | + [Cmdlet(VerbsCommon.Set, "PnPBrandCenterFont")] |
| 11 | + [OutputType(typeof(void))] |
| 12 | + public class SetBrandCenterFont : PnPWebCmdlet |
| 13 | + { |
| 14 | + [Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)] |
| 15 | + [ArgumentCompleter(typeof(BrandCenterFontCompleter))] |
| 16 | + public BrandCenterFontPipeBind Identity { get; set; } |
| 17 | + |
| 18 | + [Parameter(Mandatory = true)] |
| 19 | + public bool Visible; |
| 20 | + |
| 21 | + protected override void ExecuteCmdlet() |
| 22 | + { |
| 23 | + var webUrl = CurrentWeb.EnsureProperty(w => w.Url); |
| 24 | + |
| 25 | + var brandCenterConfig = BrandCenterUtility.GetBrandCenterConfiguration(this, ClientContext); |
| 26 | + if (brandCenterConfig == null || !brandCenterConfig.IsBrandCenterSiteFeatureEnabled || string.IsNullOrEmpty(brandCenterConfig.SiteUrl)) |
| 27 | + { |
| 28 | + throw new PSArgumentException("Brand Center is not enabled for this tenant"); |
| 29 | + } |
| 30 | + |
| 31 | + LogDebug($"Connecting to the Brand Center site at {brandCenterConfig.SiteUrl}"); |
| 32 | + using var brandCenterContext = Connection.CloneContext(brandCenterConfig.SiteUrl); |
| 33 | + var font = Identity.GetFont(this, ClientContext, webUrl); |
| 34 | + |
| 35 | + LogDebug($"Retrieving the Brand Center font library with ID {brandCenterConfig.BrandFontLibraryId}"); |
| 36 | + var brandCenterFontLibrary = brandCenterContext.Web.GetListById(brandCenterConfig.BrandFontLibraryId); |
| 37 | + brandCenterContext.Load(brandCenterFontLibrary, l => l.RootFolder); |
| 38 | + brandCenterContext.ExecuteQueryRetry(); |
| 39 | + |
| 40 | + LogDebug($"Uploading the font to the Brand Center font library root folder at {brandCenterFontLibrary.RootFolder.ServerRelativeUrl}"); |
| 41 | + var file = brandCenterFontLibrary.RootFolder.UploadFile(System.IO.Path.GetFileName(Path), Path, true); |
| 42 | + |
| 43 | + if (ParameterSpecified(nameof(Visible)) && Visible.HasValue) |
| 44 | + { |
| 45 | + LogDebug($"Setting the font visibility to {Visible.Value}"); |
| 46 | + ListItemHelper.SetFieldValues(file.ListItemAllFields, new Hashtable { { "_SPFontVisible", Visible.Value ? "True" : "False" } }, this); |
| 47 | + file.ListItemAllFields.UpdateOverwriteVersion(); |
| 48 | + } |
| 49 | + |
| 50 | + brandCenterContext.Load(file); |
| 51 | + brandCenterContext.ExecuteQueryRetry(); |
| 52 | + |
| 53 | + LogDebug("Font uploaded successfully"); |
| 54 | + WriteObject(file); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments