Skip to content

Commit 465c7c8

Browse files
committed
Added completer for PropertyBag Keys
1 parent 7feff0e commit 465c7c8

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Management.Automation;
6+
using System.Management.Automation.Language;
7+
using System.Reflection.Metadata;
8+
using Microsoft.SharePoint.Client;
9+
using PnP.Framework.Utilities;
10+
11+
namespace PnP.PowerShell.Commands.Base.Completers
12+
{
13+
public sealed class PropertyBagKeyCompleter : IArgumentCompleter
14+
{
15+
public IEnumerable<CompletionResult> CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
16+
{
17+
wordToComplete = wordToComplete.Trim('"');
18+
19+
IEnumerable<string> keys = null;
20+
if (fakeBoundParameters["Folder"] == null)
21+
{
22+
PnPConnection.Current.Context.Web.EnsureProperty(w => w.AllProperties);
23+
24+
keys = PnPConnection.Current.Context.Web.AllProperties.FieldValues.Select(x => x.Key);
25+
}
26+
else
27+
{
28+
var folderName = fakeBoundParameters["Folder"] as string;
29+
PnPConnection.Current.Context.Web.EnsureProperty(w => w.ServerRelativeUrl);
30+
31+
var folderUrl = UrlUtility.Combine(PnPConnection.Current.Context.Web.ServerRelativeUrl, folderName);
32+
var folder = PnPConnection.Current.Context.Web.GetFolderByServerRelativePath(ResourcePath.FromDecodedUrl(folderUrl));
33+
folder.EnsureProperty(f => f.Properties);
34+
35+
keys = folder.Properties.FieldValues.Select(x => x.Key);
36+
37+
}
38+
39+
foreach (var key in keys.Where(k => k.StartsWith(wordToComplete, StringComparison.InvariantCultureIgnoreCase)))
40+
{
41+
yield return new CompletionResult($"\"{key}\"");
42+
}
43+
}
44+
}
45+
}

src/Commands/Web/GetPropertyBag.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.SharePoint.Client;
55

66
using PnP.Framework.Utilities;
7+
using PnP.PowerShell.Commands.Base.Completers;
78
using PnP.PowerShell.Commands.Model;
89

910
namespace PnP.PowerShell.Commands
@@ -17,6 +18,7 @@ public class GetPropertyBag : PnPWebCmdlet
1718
public const string ParameterSet_Key = "Key";
1819

1920
[Parameter(Mandatory = false, Position = 0, ValueFromPipeline = true, ParameterSetName = ParameterSet_Key)]
21+
[ArgumentCompleter(typeof(PropertyBagKeyCompleter))]
2022
public string Key = string.Empty;
2123

2224
[Parameter(Mandatory = false)]

0 commit comments

Comments
 (0)