Skip to content

Commit

Permalink
Search in sub nodes for id
Browse files Browse the repository at this point in the history
  • Loading branch information
JuergenRB committed Dec 7, 2023
1 parent db182bb commit 5e3bb15
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Commands/Model/SharePoint/NavigationNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class NavigationNode
public bool IsHidden { get; set; }
public bool IsTitleForExistingLanguage { get; set; }
public string Key { get; set; }
public List<object> Nodes { get; set; }
public List<NavigationNode> Nodes { get; set; }
public int NodeType { get; set; }
public bool? OpenInNewWindow { get; set; }
public string SimpleUrl { get; set; }
Expand Down
14 changes: 13 additions & 1 deletion src/Commands/Navigation/AddNavigationNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ protected override void ExecuteCmdlet()
CurrentWeb.EnsureProperties(w => w.Url);
var menuState = Utilities.REST.RestHelper.GetAsync<Model.SharePoint.NavigationNodeCollection>(Connection.HttpClient, $"{CurrentWeb.Url}/_api/navigation/MenuState", ClientContext.GetAccessToken(), false).GetAwaiter().GetResult();

var currentItem = menuState?.Nodes?.Where(t => t.Key == addedNode.Id.ToString()).FirstOrDefault();
var currentItem = menuState?.Nodes?.Select(node => SearchNodeById(node, addedNode.Id))
.FirstOrDefault(result => result != null);
if (currentItem != null)
{
currentItem.OpenInNewWindow = OpenInNewTab.ToBool();
Expand All @@ -150,5 +151,16 @@ protected override void ExecuteCmdlet()
throw new Exception("Unable to define Navigation Node collection to add the node to");
}
}

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);
}

}
}

0 comments on commit 5e3bb15

Please sign in to comment.