|
1 | | -using System.Management.Automation; |
| 1 | +using System.Collections; |
| 2 | +using System.Management.Automation; |
2 | 3 | using Microsoft.SharePoint.Client; |
| 4 | +using PnP.PowerShell.Commands.Base.Completers; |
3 | 5 | using PnP.PowerShell.Commands.Base.PipeBinds; |
4 | | -using PnP.PowerShell.Commands.Model.SharePoint.BrandCenter; |
5 | 6 | using PnP.PowerShell.Commands.Utilities; |
6 | | -using PnP.PowerShell.Commands.Utilities.REST; |
7 | 7 |
|
8 | | -namespace PnP.PowerShell.Commands.Branding |
| 8 | +namespace PnP.PowerShell.Commands.Files |
9 | 9 | { |
10 | 10 | [Cmdlet(VerbsCommon.Set, "PnPBrandCenterFont")] |
11 | 11 | [OutputType(typeof(void))] |
12 | 12 | public class SetBrandCenterFont : PnPWebCmdlet |
13 | 13 | { |
14 | 14 | [Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)] |
| 15 | + [ArgumentCompleter(typeof(BrandCenterFontCompleter))] |
15 | 16 | public BrandCenterFontPipeBind Identity { get; set; } |
16 | 17 |
|
17 | | - [Parameter(Mandatory = false)] |
18 | | - public Store Store { get; set; } = Store.All; |
| 18 | + [Parameter(Mandatory = true)] |
| 19 | + public bool Visible; |
19 | 20 |
|
20 | 21 | protected override void ExecuteCmdlet() |
21 | 22 | { |
22 | | - CurrentWeb.EnsureProperty(w => w.Url); |
| 23 | + var webUrl = CurrentWeb.EnsureProperty(w => w.Url); |
23 | 24 |
|
24 | | - LogDebug("Trying to retrieve the font with the provided identity from the Brand Center"); |
25 | | - var font = Identity.GetFont(this, ClientContext, Connection, CurrentWeb.Url, Store) ?? throw new PSArgumentException($"The font with the provided identity was not found in the Brand Center. Please check the identity and try again.", nameof(Identity)); |
| 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(); |
26 | 39 |
|
27 | | - if (font.IsValid.HasValue && font.IsValid.Value == false) |
| 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) |
28 | 44 | { |
29 | | - LogWarning($"The font with identity {font.Id} titled '{font.Title}' is not valid. Will try to apply it anyway."); |
| 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(); |
30 | 48 | } |
31 | 49 |
|
32 | | - var url = $"{BrandCenterUtility.GetStoreUrlByStoreType(font.Store, CurrentWeb.Url)}/GetById('{font.Id}')/Apply"; |
33 | | - LogDebug($"Applying font by making a POST call to {url}"); |
34 | | - RestHelper.Post(Connection.HttpClient, url, ClientContext); |
| 50 | + brandCenterContext.Load(file); |
| 51 | + brandCenterContext.ExecuteQueryRetry(); |
| 52 | + |
| 53 | + LogDebug("Font uploaded successfully"); |
| 54 | + WriteObject(file); |
35 | 55 | } |
36 | 56 | } |
37 | 57 | } |
0 commit comments