Skip to content

Commit a9c17ca

Browse files
committed
Refactored to use PnP Core SDK and introduced a completer
1 parent 287085a commit a9c17ca

File tree

4 files changed

+148
-22
lines changed

4 files changed

+148
-22
lines changed

resources/PnP.PowerShell.Format.ps1xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,51 @@
624624
</TableRowEntries>
625625
</TableControl>
626626
</View>
627+
<View>
628+
<Name>UserCustomAction</Name>
629+
<ViewSelectedBy>
630+
<TypeName>PnP.Core.Model.SharePoint.UserCustomAction</TypeName>
631+
</ViewSelectedBy>
632+
<TableControl>
633+
<TableHeaders>
634+
<TableColumnHeader>
635+
<Label>Name</Label>
636+
<Alignment>left</Alignment>
637+
</TableColumnHeader>
638+
<TableColumnHeader>
639+
<Label>Location</Label>
640+
<Alignment>left</Alignment>
641+
</TableColumnHeader>
642+
<TableColumnHeader>
643+
<Label>Scope</Label>
644+
<Width>5</Width>
645+
<Alignment>left</Alignment>
646+
</TableColumnHeader>
647+
<TableColumnHeader>
648+
<Label>Id</Label>
649+
<Alignment>left</Alignment>
650+
</TableColumnHeader>
651+
</TableHeaders>
652+
<TableRowEntries>
653+
<TableRowEntry>
654+
<TableColumnItems>
655+
<TableColumnItem>
656+
<PropertyName>Name</PropertyName>
657+
</TableColumnItem>
658+
<TableColumnItem>
659+
<PropertyName>Location</PropertyName>
660+
</TableColumnItem>
661+
<TableColumnItem>
662+
<PropertyName>Scope</PropertyName>
663+
</TableColumnItem>
664+
<TableColumnItem>
665+
<PropertyName>Id</PropertyName>
666+
</TableColumnItem>
667+
</TableColumnItems>
668+
</TableRowEntry>
669+
</TableRowEntries>
670+
</TableControl>
671+
</View>
627672
<View>
628673
<Name>WebTemplate</Name>
629674
<ViewSelectedBy>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 Microsoft.SharePoint.Client;
8+
using PnP.Core.Model.SharePoint;
9+
using PnP.Core.QueryModel;
10+
using PnP.Core.Services;
11+
using PnP.PowerShell.Commands.Enums;
12+
13+
namespace PnP.PowerShell.Commands.Base.Completers
14+
{
15+
public sealed class CustomerActionCompleter : PnPArgumentCompleter
16+
{
17+
protected override IEnumerable<CompletionResult> GetArguments(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
18+
{
19+
var results = new List<CompletionResult>();
20+
var scope = CustomActionScope.Web;
21+
if (fakeBoundParameters["Scope"] != null)
22+
{
23+
scope = Enum.Parse<CustomActionScope>(fakeBoundParameters["Scope"].ToString());
24+
}
25+
switch (scope)
26+
{
27+
case CustomActionScope.Web:
28+
{
29+
PnPContext.Web.UserCustomActions.LoadAsync(u => u.Id);
30+
31+
foreach (var ca in PnPContext.Web.UserCustomActions)
32+
{
33+
results.Add(new CompletionResult(ca.Id.ToString()));
34+
}
35+
break;
36+
}
37+
case CustomActionScope.Site:
38+
{
39+
PnPContext.Site.UserCustomActions.LoadAsync(u => u.Id);
40+
foreach (var ca in PnPContext.Site.UserCustomActions)
41+
{
42+
results.Add(new CompletionResult(ca.Id.ToString()));
43+
}
44+
break;
45+
}
46+
default:
47+
{
48+
PnPContext.Web.UserCustomActions.LoadAsync(u => u.Id);
49+
PnPContext.Site.UserCustomActions.LoadAsync(u => u.Id);
50+
51+
foreach (var ca in PnPContext.Web.UserCustomActions)
52+
{
53+
results.Add(new CompletionResult(ca.Id.ToString()));
54+
}
55+
foreach (var ca in PnPContext.Site.UserCustomActions)
56+
{
57+
results.Add(new CompletionResult(ca.Id.ToString()));
58+
}
59+
break;
60+
}
61+
62+
}
63+
return results.Where(c => c.CompletionText.StartsWith(wordToComplete)).OrderBy(c => c.CompletionText);
64+
}
65+
}
66+
}

src/Commands/Base/PipeBinds/UserCustomActionPipeBind.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public UserCustomActionPipeBind(string id)
5050
}
5151
}
5252

53-
public IEnumerable<IUserCustomAction> GetCustomActions(PnPContext context, CustomActionScope scope)
53+
public IEnumerable<IUserCustomAction> GetCustomActions(PnPContext context, CustomActionScope scope)
5454
{
5555
if (_coreUserCustomAction != null)
5656
{

src/Commands/Branding/GetCustomAction.cs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
using Microsoft.SharePoint.Client;
55
using System;
66
using PnP.PowerShell.Commands.Enums;
7+
using PnP.PowerShell.Commands.Base.Completers;
8+
using PnP.PowerShell.Commands.Base.PipeBinds;
9+
using PnP.Core.Model.SharePoint;
10+
using PnP.Core.Model;
11+
using PnP.Core.Auth.Services.Builder.Configuration;
712

813
namespace PnP.PowerShell.Commands.Branding
914
{
1015
[Cmdlet(VerbsCommon.Get, "PnPCustomAction")]
1116
public class GetCustomAction : PnPWebRetrievalsCmdlet<UserCustomAction>
1217
{
1318
[Parameter(Mandatory = false)]
14-
public Guid Identity;
19+
[ArgumentCompleter(typeof(CustomerActionCompleter))]
20+
public UserCustomActionPipeBind Identity;
1521

1622
[Parameter(Mandatory = false)]
1723
public CustomActionScope Scope = CustomActionScope.Web;
@@ -21,33 +27,42 @@ public class GetCustomAction : PnPWebRetrievalsCmdlet<UserCustomAction>
2127

2228
protected override void ExecuteCmdlet()
2329
{
24-
List<UserCustomAction> actions = new List<UserCustomAction>();
30+
// List<UserCustomAction> actions = new List<UserCustomAction>();
2531

26-
if (Scope == CustomActionScope.All || Scope == CustomActionScope.Web)
27-
{
28-
actions.AddRange(CurrentWeb.GetCustomActions(RetrievalExpressions));
29-
}
30-
if (Scope == CustomActionScope.All || Scope == CustomActionScope.Site)
31-
{
32-
actions.AddRange(ClientContext.Site.GetCustomActions(RetrievalExpressions));
33-
}
32+
// if (Scope == CustomActionScope.All || Scope == CustomActionScope.Web)
33+
// {
34+
// actions.AddRange(CurrentWeb.GetCustomActions(RetrievalExpressions));
35+
// }
36+
// if (Scope == CustomActionScope.All || Scope == CustomActionScope.Site)
37+
// {
38+
// actions.AddRange(ClientContext.Site.GetCustomActions(RetrievalExpressions));
39+
// }
3440

35-
if (Identity != Guid.Empty)
41+
if (Identity != null)
3642
{
37-
var foundAction = actions.FirstOrDefault(x => x.Id == Identity);
38-
if (foundAction != null || !ThrowExceptionIfCustomActionNotFound)
39-
{
40-
WriteObject(foundAction, true);
41-
}
42-
else
43-
{
44-
throw new PSArgumentException($"No CustomAction found with the Identity '{Identity}' within the scope '{Scope}'", "Identity");
45-
}
43+
WriteObject(Identity.GetCustomActions(PnPContext, Scope).FirstOrDefault());
4644
}
4745
else
4846
{
47+
List<IUserCustomAction> actions = null;
48+
switch (Scope)
49+
{
50+
case CustomActionScope.Web:
51+
{
52+
PnPContext.Web.LoadAsync(w => w.UserCustomActions).GetAwaiter().GetResult();
53+
actions = PnPContext.Web.UserCustomActions.ToList();
54+
break;
55+
}
56+
case CustomActionScope.Site:
57+
{
58+
PnPContext.Site.LoadAsync(s => s.UserCustomActions).GetAwaiter().GetResult();
59+
actions = PnPContext.Site.UserCustomActions.ToList();
60+
break;
61+
}
62+
63+
}
4964
WriteObject(actions, true);
5065
}
5166
}
5267
}
53-
}
68+
}

0 commit comments

Comments
 (0)