Skip to content

Commit 50317a2

Browse files
authored
Merge pull request #4831 from KoenZomers/BrandCenterFontUpdates
Added autocompleter to Brand Center Font cmdlets
2 parents 8948702 + b7def10 commit 50317a2

10 files changed

+137
-59
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
6666
- Added `Copy-PnPPage`, `Move-PnPPage` and `Get-PnPPageCopyProgress` cmdlets to allow for copying and moving site pages between sites [#4806](https://github.com/pnp/powershell/pull/4806)
6767
- Added `Get-PnPBrandCenterConfig` to retrieve the configuration of the Brand Center on the tenant [#4830](https://github.com/pnp/powershell/pull/4830)
6868
- Added `Get-PnPBrandCenterFont` to retrieve the available fonts from the various Brand Centers [#4830](https://github.com/pnp/powershell/pull/4830)
69-
- Added `Set-PnPBrandCenterFont` to apply the specified font from the Brand Center to the current site [#4830](https://github.com/pnp/powershell/pull/4830)
69+
- Added `Use-PnPBrandCenterFont` to apply the specified font from the Brand Center to the current site [#4830](https://github.com/pnp/powershell/pull/4830)
7070
- Added `Add-PnPBrandCenterFont` to upload a font to the tenant Brand Center [#4830](https://github.com/pnp/powershell/pull/4830)
7171

7272
### Changed

documentation/Add-PnPBrandCenterFont.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Add-PnPBrandCenterFont -Path <String> [-Visible <Boolean>] [-Connection <PnPConn
2121
## DESCRIPTION
2222
This cmdlet allows a font to be uploaded to the tenant Brand Center. The font will be available for use in the tenant and site collection Brand Center.
2323

24+
Use [Use-PnPBrandCenterFont](Use-PnPBrandCenterFont.md) to apply the font to the current site.
25+
Use [Get-PnPBrandCenterFont](Get-PnPBrandCenterFont.md) to retrieve the available fonts.
26+
2427
## EXAMPLES
2528

2629
### EXAMPLE 1

documentation/Set-PnPBrandCenterFont.md renamed to documentation/Use-PnPBrandCenterFont.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
Module Name: PnP.PowerShell
33
schema: 2.0.0
44
applicable: SharePoint Online
5-
online version: https://pnp.github.io/powershell/cmdlets/Set-PnPBrandCenterFont.html
5+
online version: https://pnp.github.io/powershell/cmdlets/Use-PnPBrandCenterFont.html
66
external help file: PnP.PowerShell.dll-Help.xml
7-
title: Set-PnPBrandCenterFont
7+
title: Use-PnPBrandCenterFont
88
---
99

10-
# Set-PnPBrandCenterFont
10+
# Use-PnPBrandCenterFont
1111

1212
## SYNOPSIS
1313
Applies the specified font from the Brand Center to the current site.
1414

1515
## SYNTAX
1616

1717
```powershell
18-
Set-PnPBrandCenterFont -Identity <BrandCenterFontPipeBind> [-Store <Tenant|OutOfBox|Site|All>] [-Connection <PnPConnection>]
18+
Use-PnPBrandCenterFont -Identity <BrandCenterFontPipeBind> [-Store <Tenant|OutOfBox|Site|All>] [-Connection <PnPConnection>]
1919
```
2020

2121
## DESCRIPTION
@@ -25,14 +25,14 @@ Applies the specified font from the Brand Center to the current site.
2525

2626
### EXAMPLE 1
2727
```powershell
28-
Set-PnPBrandCenterFont -Identity "2812cbd8-7176-4e45-8911-6a063f89a1f1"
28+
Use-PnPBrandCenterFont -Identity "2812cbd8-7176-4e45-8911-6a063f89a1f1"
2929
```
3030

3131
Looks up and applies the font with the identity "2812cbd8-7176-4e45-8911-6a063f89a1f1" from any of the Brand Centers to the current site
3232

3333
### EXAMPLE 2
3434
```powershell
35-
Set-PnPBrandCenterFont -Identity "My awesome font" -Store Tenant
35+
Use-PnPBrandCenterFont -Identity "My awesome font" -Store Tenant
3636
```
3737

3838
Looks up and applies the font with the title "My awesome font" from the tenant Brand Center
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Management.Automation;
5+
using System.Management.Automation.Language;
6+
using Microsoft.SharePoint.Client;
7+
using PnP.PowerShell.Commands.Utilities;
8+
9+
namespace PnP.PowerShell.Commands.Base.Completers
10+
{
11+
public sealed class BrandCenterFontCompleter : PnPArgumentCompleter
12+
{
13+
protected override IEnumerable<CompletionResult> GetArguments(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
14+
{
15+
ClientContext.Web.EnsureProperty(w => w.Url);
16+
var fonts = BrandCenterUtility.GetFonts(null, ClientContext, ClientContext.Web.Url);
17+
return fonts.Select(font => new CompletionResult(font.Title)).OrderBy(ct => ct.CompletionText);
18+
}
19+
}
20+
}

src/Commands/Base/Completers/PnPArgumentCompleter.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4-
using System.ComponentModel;
54
using System.Management.Automation;
65
using System.Management.Automation.Language;
76
using System.Threading.Tasks;
8-
using Microsoft.SharePoint.Client;
9-
using PnP.Core.Services;
107
using PnP.PowerShell.Commands.Base;
118
using PnP.PowerShell.Commands.Extensions;
129

1310
public abstract class PnPArgumentCompleter : IArgumentCompleter
1411
{
15-
public Microsoft.SharePoint.Client.ClientContext ClientContext => PnPConnection.Current.Context;
12+
public static Microsoft.SharePoint.Client.ClientContext ClientContext => PnPConnection.Current.Context;
1613

17-
public PnP.Core.Services.PnPContext PnPContext => PnPConnection.Current.PnPContext;
14+
public static PnP.Core.Services.PnPContext PnPContext => PnPConnection.Current.PnPContext;
1815

1916
private const int Timeout = 2000;
17+
2018
public IEnumerable<CompletionResult> CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
2119
{
2220
var quoteChar = '"';
23-
if(wordToComplete.StartsWith('\''))
21+
if (wordToComplete.StartsWith('\''))
2422
{
2523
quoteChar = '\'';
2624
}
2725
wordToComplete = wordToComplete.Trim(['"', '\'']);
2826
var task = Task.Run(() => GetArguments(commandName, parameterName, wordToComplete, commandAst, fakeBoundParameters));
29-
27+
3028
var results = task.TimeoutAfter(TimeSpan.FromMilliseconds(GetTimeOut())).GetAwaiter().GetResult();
31-
foreach(var result in results)
29+
foreach (var result in results)
3230
{
3331
var completionText = result.CompletionText;
34-
if(quoteChar == '"')
32+
if (quoteChar == '"')
3533
{
36-
completionText = completionText.Replace("\"","`\"");
37-
} else if(quoteChar == '\'')
34+
completionText = completionText.Replace("\"", "`\"");
35+
}
36+
else if (quoteChar == '\'')
3837
{
39-
completionText = completionText.Replace("'","`'");
38+
completionText = completionText.Replace("'", "`'");
4039
}
41-
yield return new CompletionResult($"{quoteChar}{completionText}{quoteChar}",result.ListItemText,result.ResultType,result.ToolTip);
40+
yield return new CompletionResult($"{quoteChar}{completionText}{quoteChar}", result.ListItemText, result.ResultType, result.ToolTip);
4241
}
4342
}
4443

src/Commands/Base/PipeBinds/BrandCenterFontPipeBind.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,24 @@ public BrandCenterFontPipeBind(Font font)
4242
/// </summary>
4343
/// <param name="cmdlet">The cmdlet instance to use to retrieve the Font in this pipe bind</param>
4444
/// <param name="clientContext">ClientContext to use to communicate with SharePoint Online</param>
45-
/// <param name="connection">Connection to use to communicate with SharePoint Online</param>
4645
/// <param name="webUrl">Url to use to check the site collection Brand Center</param>
4746
/// <param name="store">The store to check for the font. When NULL, it will check all stores.</param>
4847
/// <exception cref="Exception">Thrown when the ContainerProperties cannot be retrieved</exception>
4948
/// <returns>Font</returns>
50-
public Font GetFont(BasePSCmdlet cmdlet, ClientContext clientContext, PnPConnection connection, string webUrl, Store store = Store.All)
49+
public Font GetFont(BasePSCmdlet cmdlet, ClientContext clientContext, string webUrl, Store store = Store.All)
5150
{
5251
if (_font != null)
5352
{
5453
return _font;
5554
}
5655
else if (_id.HasValue)
5756
{
58-
_font = BrandCenterUtility.GetFontById(cmdlet, clientContext, connection, _id.Value, webUrl, store);
57+
_font = BrandCenterUtility.GetFontById(cmdlet, clientContext, _id.Value, webUrl, store);
5958
return _font;
6059
}
6160
else if (!string.IsNullOrEmpty(_title))
6261
{
63-
_font = BrandCenterUtility.GetFontByTitle(cmdlet, clientContext, connection, _title, webUrl, store);
62+
_font = BrandCenterUtility.GetFontByTitle(cmdlet, clientContext, _title, webUrl, store);
6463
return _font;
6564
}
6665
return null;

src/Commands/Branding/GetBrandCenterFont.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.Management.Automation;
33
using Microsoft.SharePoint.Client;
4+
using PnP.PowerShell.Commands.Base.Completers;
45
using PnP.PowerShell.Commands.Base.PipeBinds;
56
using PnP.PowerShell.Commands.Model.SharePoint.BrandCenter;
67
using PnP.PowerShell.Commands.Utilities;
@@ -16,6 +17,7 @@ public class GetBrandCenterFont : PnPWebCmdlet
1617
private const string ParameterSet_ALL = "All";
1718

1819
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ParameterSetName = ParameterSet_SINGLE)]
20+
[ArgumentCompleter(typeof(BrandCenterFontCompleter))]
1921
public BrandCenterFontPipeBind Identity { get; set; }
2022

2123
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_SINGLE)]
@@ -28,12 +30,12 @@ protected override void ExecuteCmdlet()
2830

2931
if (ParameterSpecified(nameof(Identity)))
3032
{
31-
var font = Identity.GetFont(this, ClientContext, Connection, CurrentWeb.Url, Store);
33+
var font = Identity.GetFont(this, ClientContext, CurrentWeb.Url, Store);
3234
WriteObject(font, false);
3335
}
3436
else
3537
{
36-
WriteObject(BrandCenterUtility.GetFonts(this, ClientContext, Connection, CurrentWeb.Url, Store), true);
38+
WriteObject(BrandCenterUtility.GetFonts(this, ClientContext, CurrentWeb.Url, Store), true);
3739
}
3840
}
3941
}
Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,57 @@
1-
using System.Management.Automation;
1+
using System.Collections;
2+
using System.Management.Automation;
23
using Microsoft.SharePoint.Client;
4+
using PnP.PowerShell.Commands.Base.Completers;
35
using PnP.PowerShell.Commands.Base.PipeBinds;
4-
using PnP.PowerShell.Commands.Model.SharePoint.BrandCenter;
56
using PnP.PowerShell.Commands.Utilities;
6-
using PnP.PowerShell.Commands.Utilities.REST;
77

8-
namespace PnP.PowerShell.Commands.Branding
8+
namespace PnP.PowerShell.Commands.Files
99
{
1010
[Cmdlet(VerbsCommon.Set, "PnPBrandCenterFont")]
1111
[OutputType(typeof(void))]
1212
public class SetBrandCenterFont : PnPWebCmdlet
1313
{
1414
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
15+
[ArgumentCompleter(typeof(BrandCenterFontCompleter))]
1516
public BrandCenterFontPipeBind Identity { get; set; }
1617

17-
[Parameter(Mandatory = false)]
18-
public Store Store { get; set; } = Store.All;
18+
[Parameter(Mandatory = true)]
19+
public bool Visible;
1920

2021
protected override void ExecuteCmdlet()
2122
{
22-
CurrentWeb.EnsureProperty(w => w.Url);
23+
var webUrl = CurrentWeb.EnsureProperty(w => w.Url);
2324

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();
2639

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)
2844
{
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();
3048
}
3149

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);
3555
}
3656
}
3757
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Management.Automation;
2+
using Microsoft.SharePoint.Client;
3+
using PnP.PowerShell.Commands.Base.Completers;
4+
using PnP.PowerShell.Commands.Base.PipeBinds;
5+
using PnP.PowerShell.Commands.Model.SharePoint.BrandCenter;
6+
using PnP.PowerShell.Commands.Utilities;
7+
using PnP.PowerShell.Commands.Utilities.REST;
8+
9+
namespace PnP.PowerShell.Commands.Branding
10+
{
11+
[Cmdlet(VerbsOther.Use, "PnPBrandCenterFont")]
12+
[OutputType(typeof(void))]
13+
public class UseBrandCenterFont : PnPWebCmdlet
14+
{
15+
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
16+
[ArgumentCompleter(typeof(BrandCenterFontCompleter))]
17+
public BrandCenterFontPipeBind Identity { get; set; }
18+
19+
[Parameter(Mandatory = false)]
20+
public Store Store { get; set; } = Store.All;
21+
22+
protected override void ExecuteCmdlet()
23+
{
24+
CurrentWeb.EnsureProperty(w => w.Url);
25+
26+
LogDebug("Trying to retrieve the font with the provided identity from the Brand Center");
27+
var font = Identity.GetFont(this, ClientContext, 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));
28+
29+
if (font.IsValid.HasValue && font.IsValid.Value == false)
30+
{
31+
LogWarning($"The font with identity {font.Id} titled '{font.Title}' is not valid. Will try to apply it anyway.");
32+
}
33+
34+
var url = $"{BrandCenterUtility.GetStoreUrlByStoreType(font.Store, CurrentWeb.Url)}/GetById('{font.Id}')/Apply";
35+
LogDebug($"Applying font by making a POST call to {url}");
36+
RestHelper.Post(Connection.HttpClient, url, ClientContext);
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)