Skip to content

Commit 659cf94

Browse files
committed
Added ContentType and Field completers
1 parent 765836c commit 659cf94

21 files changed

+89
-17
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
10+
namespace PnP.PowerShell.Commands.Base.Completers
11+
{
12+
public sealed class ContentTypeCompleter : IArgumentCompleter
13+
{
14+
public IEnumerable<CompletionResult> CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
15+
{
16+
IEnumerable<ContentType> result = PnPConnection.Current.Context.LoadQuery(PnPConnection.Current.Context.Web.AvailableContentTypes.Include(f => f.Name));
17+
PnPConnection.Current.Context.ExecuteQueryRetry();
18+
foreach (var ct in result.Where(l => l.Name.StartsWith(wordToComplete, StringComparison.InvariantCultureIgnoreCase)))
19+
{
20+
var ctName = ct.Name.IndexOf(" ") > 0? $"'{ct.Name}'" : ct.Name;
21+
yield return new CompletionResult(ctName);
22+
}
23+
}
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
10+
namespace PnP.PowerShell.Commands.Base.Completers
11+
{
12+
public sealed class FieldInternalNameCompleter : IArgumentCompleter
13+
{
14+
public IEnumerable<CompletionResult> CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
15+
{
16+
IEnumerable<Field> result = PnPConnection.Current.Context.LoadQuery(PnPConnection.Current.Context.Web.AvailableFields.Include(f => f.InternalName));
17+
PnPConnection.Current.Context.ExecuteQueryRetry();
18+
foreach (var field in result.Where(l => l.InternalName.StartsWith(wordToComplete, StringComparison.InvariantCultureIgnoreCase)))
19+
{
20+
yield return new CompletionResult(field.InternalName);
21+
}
22+
}
23+
}
24+
}

src/Commands/ContentTypes/AddContentTypeToList.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class AddContentTypeToList : PnPWebCmdlet
1414

1515
[Parameter(Mandatory = true)]
1616
[ValidateNotNullOrEmpty]
17+
[ArgumentCompleter(typeof(ContentTypeCompleter))]
1718
public ContentTypePipeBind ContentType;
1819

1920
[Parameter(Mandatory = false)]

src/Commands/ContentTypes/AddFieldToContentType.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Management.Automation;
33
using Microsoft.SharePoint.Client;
4-
4+
using PnP.PowerShell.Commands.Base.Completers;
55
using PnP.PowerShell.Commands.Base.PipeBinds;
66

77
namespace PnP.PowerShell.Commands.ContentTypes
@@ -10,10 +10,12 @@ namespace PnP.PowerShell.Commands.ContentTypes
1010
public class AddFieldToContentType : PnPWebCmdlet
1111
{
1212
[Parameter(Mandatory = true)]
13+
[ArgumentCompleter(typeof(FieldInternalNameCompleter))]
1314
public FieldPipeBind Field;
1415

1516
[Parameter(Mandatory = true)]
1617
[ValidateNotNullOrEmpty]
18+
[ArgumentCompleter(typeof(ContentTypeCompleter))]
1719
public ContentTypePipeBind ContentType;
1820

1921
[Parameter(Mandatory = false)]

src/Commands/ContentTypes/GetContentType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class GetContentType : PnPWebRetrievalsCmdlet<ContentType>
1717
{
1818
[Parameter(Mandatory = false, Position = 0, ValueFromPipeline = true)]
1919
[ValidateNotNullOrEmpty]
20+
[ArgumentCompleter(typeof(ContentTypeCompleter))]
2021
public ContentTypePipeBind Identity;
2122

2223
[Parameter(Mandatory = false, ValueFromPipeline = true)]

src/Commands/ContentTypes/GetContentTypePublishingStatus.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Management.Automation;
33
using Microsoft.SharePoint.Client;
4-
4+
using PnP.PowerShell.Commands.Base.Completers;
55
using PnP.PowerShell.Commands.Base.PipeBinds;
66

77
namespace PnP.PowerShell.Commands.ContentTypes
@@ -11,6 +11,7 @@ public class GetContentTypePublishingStatus : PnPWebCmdlet
1111
{
1212
[Parameter(Mandatory = false, Position = 0, ValueFromPipeline = true)]
1313
[ValidateNotNullOrEmpty]
14+
[ArgumentCompleter(typeof(ContentTypeCompleter))]
1415
public ContentTypePipeBind ContentType;
1516

1617
protected override void ExecuteCmdlet()
@@ -20,7 +21,7 @@ protected override void ExecuteCmdlet()
2021
ClientContext.Load(pub);
2122
ClientContext.ExecuteQueryRetry();
2223
var ct = ContentType.GetContentTypeOrError(this, nameof(ContentType), site.RootWeb);
23-
24+
2425
if (ct == null)
2526
{
2627
WriteError(new ErrorRecord(new Exception($"Invalid content type id."), "INVALIDCTID", ErrorCategory.InvalidArgument, ContentType));

src/Commands/ContentTypes/PublishContentType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Management.Automation;
33
using Microsoft.SharePoint.Client;
4-
4+
using PnP.PowerShell.Commands.Base.Completers;
55
using PnP.PowerShell.Commands.Base.PipeBinds;
66

77
namespace PnP.PowerShell.Commands.ContentTypes
@@ -11,6 +11,7 @@ public class PublishContentType : PnPWebCmdlet
1111
{
1212
[Parameter(Mandatory = false, Position = 0, ValueFromPipeline = true)]
1313
[ValidateNotNullOrEmpty]
14+
[ArgumentCompleter(typeof(ContentTypeCompleter))]
1415
public ContentTypePipeBind ContentType;
1516

1617
protected override void ExecuteCmdlet()

src/Commands/ContentTypes/RemoveContentType.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using PnP.PowerShell.Commands.Base.PipeBinds;
1+
using PnP.PowerShell.Commands.Base.Completers;
2+
using PnP.PowerShell.Commands.Base.PipeBinds;
23
using System.Management.Automation;
34
using Resources = PnP.PowerShell.Commands.Properties.Resources;
45

@@ -8,14 +9,16 @@ namespace PnP.PowerShell.Commands.ContentTypes
89
public class RemoveContentType : PnPWebCmdlet
910
{
1011
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
12+
[ArgumentCompleter(typeof(ContentTypeCompleter))]
1113
public ContentTypePipeBind Identity;
1214

15+
1316
[Parameter(Mandatory = false)]
1417
public SwitchParameter Force;
1518

1619
protected override void ExecuteCmdlet()
1720
{
18-
var ct = Identity?.GetContentTypeOrThrow(nameof(Identity), Connection.PnPContext);
21+
var ct = Identity?.GetContentTypeOrThrow(nameof(Identity), Connection.PnPContext);
1922
if (Force || ShouldContinue($"Remove Content Type '{ct.Name}'?", Resources.Confirm))
2023
{
2124
ct.Delete();

src/Commands/ContentTypes/RemoveContentTypeFromList.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class RemoveContentTypeFromList : PnPWebCmdlet
1414

1515
[Parameter(Mandatory = true)]
1616
[ValidateNotNullOrEmpty]
17+
[ArgumentCompleter(typeof(ContentTypeCompleter))]
1718
public ContentTypePipeBind ContentType;
1819

1920
protected override void ExecuteCmdlet()

src/Commands/ContentTypes/RemoveFieldFromContentType.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Linq;
33
using System.Management.Automation;
44
using Microsoft.SharePoint.Client;
5-
5+
using PnP.PowerShell.Commands.Base.Completers;
66
using PnP.PowerShell.Commands.Base.PipeBinds;
77

88
namespace PnP.PowerShell.Commands.ContentTypes
@@ -12,10 +12,12 @@ public class RemoveFieldFromContentType : PnPWebCmdlet
1212
{
1313
[Parameter(Mandatory = true)]
1414
[ValidateNotNullOrEmpty]
15+
[ArgumentCompleter(typeof(FieldInternalNameCompleter))]
1516
public FieldPipeBind Field;
1617

1718
[Parameter(Mandatory = true)]
1819
[ValidateNotNullOrEmpty]
20+
[ArgumentCompleter(typeof(ContentTypeCompleter))]
1921
public ContentTypePipeBind ContentType;
2022

2123
[Parameter(Mandatory = false)]
@@ -45,15 +47,15 @@ protected override void ExecuteCmdlet()
4547
field = null;
4648
}
4749
}
48-
50+
4951
if (field is null)
5052
{
5153
throw new PSArgumentException("Field not found", nameof(Field));
5254
}
53-
55+
5456
var ct = ContentType.GetContentTypeOrThrow(nameof(ContentType), CurrentWeb, true);
5557
ct.EnsureProperty(c => c.FieldLinks);
56-
58+
5759
var fieldLink = ct.FieldLinks.FirstOrDefault(f => f.Id == field.Id);
5860
if (fieldLink is null)
5961
{

0 commit comments

Comments
 (0)