Skip to content

Commit 287085a

Browse files
committed
Refactored use of Context in completers
1 parent 05ca190 commit 287085a

File tree

6 files changed

+20
-18
lines changed

6 files changed

+20
-18
lines changed

src/Commands/Base/Completers/ContentTypeCompleter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Linq;
55
using System.Management.Automation;
66
using System.Management.Automation.Language;
7-
using System.Reflection.Metadata;
87
using Microsoft.SharePoint.Client;
98

109
namespace PnP.PowerShell.Commands.Base.Completers
@@ -13,8 +12,8 @@ public sealed class ContentTypeCompleter : PnPArgumentCompleter
1312
{
1413
protected override IEnumerable<CompletionResult> GetArguments(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
1514
{
16-
IEnumerable<ContentType> result = PnPConnection.Current.Context.LoadQuery(PnPConnection.Current.Context.Web.AvailableContentTypes.Include(f => f.Name));
17-
PnPConnection.Current.Context.ExecuteQueryRetry();
15+
IEnumerable<ContentType> result = ClientContext.LoadQuery(ClientContext.Web.AvailableContentTypes.Include(f => f.Name));
16+
ClientContext.ExecuteQueryRetry();
1817
foreach (var ct in result.Where(l => l.Name.StartsWith(wordToComplete, StringComparison.InvariantCultureIgnoreCase)))
1918
{
2019
yield return new CompletionResult(ct.Name);

src/Commands/Base/Completers/FieldInternalNameCompleter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Linq;
55
using System.Management.Automation;
66
using System.Management.Automation.Language;
7-
using System.Reflection.Metadata;
87
using Microsoft.SharePoint.Client;
98

109
namespace PnP.PowerShell.Commands.Base.Completers
@@ -13,8 +12,8 @@ public sealed class FieldInternalNameCompleter : PnPArgumentCompleter
1312
{
1413
protected override IEnumerable<CompletionResult> GetArguments(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
1514
{
16-
IEnumerable<Field> result = PnPConnection.Current.Context.LoadQuery(PnPConnection.Current.Context.Web.AvailableFields.Include(f => f.InternalName));
17-
PnPConnection.Current.Context.ExecuteQueryRetry();
15+
IEnumerable<Field> result = ClientContext.LoadQuery(PnPConnection.Current.Context.Web.AvailableFields.Include(f => f.InternalName));
16+
ClientContext.ExecuteQueryRetry();
1817
foreach (var field in result.Where(l => l.InternalName.StartsWith(wordToComplete, StringComparison.InvariantCultureIgnoreCase)))
1918
{
2019
yield return new CompletionResult(field.InternalName);

src/Commands/Base/Completers/ListNameCompleter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
using System.Management.Automation;
66
using System.Management.Automation.Language;
77
using Microsoft.SharePoint.Client;
8-
using PnP.Framework.Extensions;
9-
using PnP.Framework.Provisioning.Model.Drive;
108

119
namespace PnP.PowerShell.Commands.Base.Completers
1210
{
@@ -15,8 +13,8 @@ public sealed class ListNameCompleter : PnPArgumentCompleter
1513
protected override IEnumerable<CompletionResult> GetArguments(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
1614
{
1715
List<CompletionResult> arguments = new List<CompletionResult>();
18-
IEnumerable<List> result = PnPConnection.Current.Context.LoadQuery(PnPConnection.Current.Context.Web.Lists.Include(list => list.Title, list => list.RootFolder.Name));
19-
PnPConnection.Current.Context.ExecuteQueryRetry();
16+
IEnumerable<List> result = ClientContext.LoadQuery(ClientContext.Web.Lists.Include(list => list.Title, list => list.RootFolder.Name));
17+
ClientContext.ExecuteQueryRetry();
2018
foreach (var list in result.Where(l => l.Title.StartsWith(wordToComplete, StringComparison.InvariantCultureIgnoreCase) || l.RootFolder.Name.StartsWith(wordToComplete, StringComparison.InvariantCultureIgnoreCase)))
2119
{
2220
if (list.RootFolder.Name != list.Title)

src/Commands/Base/Completers/PageCompleter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ protected override IEnumerable<CompletionResult> GetArguments(string commandName
1212
{
1313
List<CompletionResult> results = new List<CompletionResult>();
1414
wordToComplete = wordToComplete.Replace('\\', '/');
15-
var pages = PnPConnection.Current.PnPContext.Web.GetPages(wordToComplete.TrimStart('/'));
15+
var pages = PnPContext.Web.GetPages(wordToComplete.TrimStart('/'));
1616
foreach (var page in pages.OrderBy(p => p.Name))
1717
{
1818
var result = string.IsNullOrEmpty(page.Folder) ? page.Name : page.Folder + "/" + page.Name;

src/Commands/Base/Completers/PnPArgumentCompleter.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.ComponentModel;
45
using System.Management.Automation;
56
using System.Management.Automation.Language;
6-
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.SharePoint.Client;
9+
using PnP.Core.Services;
10+
using PnP.PowerShell.Commands.Base;
811
using PnP.PowerShell.Commands.Extensions;
912

1013
public abstract class PnPArgumentCompleter : IArgumentCompleter
1114
{
15+
public Microsoft.SharePoint.Client.ClientContext ClientContext => PnPConnection.Current.Context;
16+
17+
public PnP.Core.Services.PnPContext PnPContext => PnPConnection.Current.PnPContext;
18+
1219
private const int Timeout = 2000;
1320
public IEnumerable<CompletionResult> CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
1421
{

src/Commands/Base/Completers/PropertyBagKeyCompleter.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Linq;
55
using System.Management.Automation;
66
using System.Management.Automation.Language;
7-
using System.Reflection.Metadata;
87
using Microsoft.SharePoint.Client;
98
using PnP.Framework.Utilities;
109

@@ -17,17 +16,17 @@ protected override IEnumerable<CompletionResult> GetArguments(string commandName
1716
IEnumerable<string> keys = null;
1817
if (fakeBoundParameters["Folder"] == null)
1918
{
20-
PnPConnection.Current.Context.Web.EnsureProperty(w => w.AllProperties);
19+
ClientContext.Web.EnsureProperty(w => w.AllProperties);
2120

22-
keys = PnPConnection.Current.Context.Web.AllProperties.FieldValues.Select(x => x.Key);
21+
keys = ClientContext.Web.AllProperties.FieldValues.Select(x => x.Key);
2322
}
2423
else
2524
{
2625
var folderName = fakeBoundParameters["Folder"] as string;
27-
PnPConnection.Current.Context.Web.EnsureProperty(w => w.ServerRelativeUrl);
26+
ClientContext.Web.EnsureProperty(w => w.ServerRelativeUrl);
2827

29-
var folderUrl = UrlUtility.Combine(PnPConnection.Current.Context.Web.ServerRelativeUrl, folderName);
30-
var folder = PnPConnection.Current.Context.Web.GetFolderByServerRelativePath(ResourcePath.FromDecodedUrl(folderUrl));
28+
var folderUrl = UrlUtility.Combine(ClientContext.Web.ServerRelativeUrl, folderName);
29+
var folder = ClientContext.Web.GetFolderByServerRelativePath(ResourcePath.FromDecodedUrl(folderUrl));
3130
folder.EnsureProperty(f => f.Properties);
3231

3332
keys = folder.Properties.FieldValues.Select(x => x.Key);

0 commit comments

Comments
 (0)