-
Notifications
You must be signed in to change notification settings - Fork 403
Expand file tree
/
Copy pathAddNavigationNode.cs
More file actions
176 lines (153 loc) · 6.64 KB
/
AddNavigationNode.cs
File metadata and controls
176 lines (153 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using Microsoft.SharePoint.Client;
using PnP.Framework.Enums;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text.Json;
namespace PnP.PowerShell.Commands.Branding
{
[Cmdlet(VerbsCommon.Add, "PnPNavigationNode", DefaultParameterSetName = ParameterSet_Default)]
[OutputType(typeof(NavigationNode))]
public class AddNavigationNode : PnPWebCmdlet
{
private const string ParameterSet_Default = "Default";
private const string ParameterSet_PreviousNode = "Add node after another node";
[Parameter(ParameterSetName = ParameterSet_Default)]
[Parameter(ParameterSetName = ParameterSet_PreviousNode)]
[Parameter(Mandatory = true)]
public NavigationType Location;
[Parameter(ParameterSetName = ParameterSet_Default)]
[Parameter(ParameterSetName = ParameterSet_PreviousNode)]
[Parameter(Mandatory = true)]
public string Title;
[Parameter(ParameterSetName = ParameterSet_Default)]
[Parameter(ParameterSetName = ParameterSet_PreviousNode)]
[Parameter(Mandatory = false)]
public string Url;
[Parameter(ParameterSetName = ParameterSet_Default)]
[Parameter(ParameterSetName = ParameterSet_PreviousNode)]
[Parameter(Mandatory = false)]
public NavigationNodePipeBind Parent;
[Parameter(ParameterSetName = ParameterSet_Default)]
[Parameter(Mandatory = false)]
public SwitchParameter First;
[Parameter(ParameterSetName = ParameterSet_Default)]
[Parameter(ParameterSetName = ParameterSet_PreviousNode)]
[Parameter(Mandatory = false)]
public List<Guid> AudienceIds;
[Parameter(ParameterSetName = ParameterSet_Default)]
[Parameter(ParameterSetName = ParameterSet_PreviousNode)]
[Parameter(Mandatory = false)]
public SwitchParameter OpenInNewTab;
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_PreviousNode)]
public NavigationNodePipeBind PreviousNode;
protected override void ExecuteCmdlet()
{
if (Url == null)
{
ClientContext.Load(CurrentWeb, w => w.Url);
ClientContext.ExecuteQueryRetry();
Url = CurrentWeb.Url;
}
string menuNodeKey = string.Empty;
switch (Location)
{
case NavigationType.QuickLaunch:
menuNodeKey = "1025";
break;
case NavigationType.TopNavigationBar:
menuNodeKey = "1002";
break;
case NavigationType.Footer:
menuNodeKey = "3a94b35f-030b-468e-80e3-b75ee84ae0ad";
break;
case NavigationType.SearchNav:
menuNodeKey = "1040";
break;
default:
throw new ArgumentOutOfRangeException("Location");
}
// Get the current menu state
CurrentWeb.EnsureProperties(w => w.Url);
var menuState = Utilities.REST.RestHelper.Get<Model.SharePoint.NavigationNodeCollection>(
Connection.HttpClient,
$"{CurrentWeb.Url}/_api/navigation/MenuState?menuNodeKey='{menuNodeKey}'",
ClientContext.GetAccessToken(),
false);
if (menuState == null)
{
throw new Exception("Unable to retrieve current menu state.");
}
// Build the new node
var newNode = new Model.SharePoint.NavigationNode
{
NodeType = 0,
Title = Title,
SimpleUrl = Url,
FriendlyUrlSegment = "",
IsTitleForExistingLanguage = true,
AudienceIds = AudienceIds ?? new List<Guid>(),
CustomProperties = new List<object>(),
Translations = new List<object>(),
OpenInNewWindow = OpenInNewTab.IsPresent ? true : null,
Nodes = new List<Model.SharePoint.NavigationNode>()
};
// Find where to insert the node
List<Model.SharePoint.NavigationNode> targetNodes = menuState.Nodes;
if (ParameterSpecified(nameof(Parent)))
{
var parentNode = menuState.Nodes.Select(node => SearchNodeById(node, Parent.GetNavigationNode(CurrentWeb)?.Id ?? 0))
.FirstOrDefault(result => result != null);
if (parentNode == null)
{
throw new Exception("Parent node not found in menu state.");
}
if (parentNode.Nodes == null)
parentNode.Nodes = new List<Model.SharePoint.NavigationNode>();
targetNodes = parentNode.Nodes;
}
int insertIndex = -1;
if (ParameterSpecified(nameof(PreviousNode)))
{
var prevNode = targetNodes.Select(node => SearchNodeById(node, PreviousNode.GetNavigationNode(CurrentWeb)?.Id ?? 0))
.FirstOrDefault(result => result != null);
if (prevNode != null)
{
insertIndex = targetNodes.IndexOf(prevNode) + 1;
}
}
else if (First.IsPresent)
{
insertIndex = 0;
}
if (insertIndex >= 0 && insertIndex <= targetNodes.Count)
targetNodes.Insert(insertIndex, newNode);
else
targetNodes.Add(newNode);
// Prepare the payload
var payload = JsonSerializer.Serialize(new { menuState });
// Save the new menu state
Utilities.REST.RestHelper.Post(
Connection.HttpClient,
$"{CurrentWeb.Url}/_api/navigation/SaveMenuState",
ClientContext,
payload,
"application/json",
"application/json;odata=nometadata"
);
// Output the new node (for consistency with previous behavior)
WriteObject(newNode);
}
private static Model.SharePoint.NavigationNode SearchNodeById(Model.SharePoint.NavigationNode root, int id)
{
if (root.Key == id.ToString())
{
return root;
}
return root.Nodes.Select(child => SearchNodeById(child, id)).FirstOrDefault(result => result != null);
}
// LogWarning("Something went wrong while trying to set AudienceIDs or Open in new tab property");
}
}