I'm currently trying to create a "Child Node" using Javascript by grabbing the users input and selection of their "Heading" where the Child node is going to go under.
function addNavigationNodesChild() {
var idOfParentNode = $('#selectChildParentNavNode').val();
var nameOfParentNode = $('#selectChildParentNavNode option:selected').text();
var clientContextChild = new SP.ClientContext(siteRelURL);
if (clientContextChild != undefined && clientContextChild != null) {
var webChild = clientContextChild.get_web();
var quickLaunchNodeCollectionGet = webChild.get_navigation().get_quickLaunch();
var quickLaunchNodeCollectionGetData = quickLaunchNodeCollectionGet.get_item(idOfParentNode).get_children();
for (var c = 0; c < navArrayChild.length; c++) {
var navObjectChild = navArrayChild[c];
var navTitleChild = navObjectChild.title;
var navUrlChild = navObjectChild.url;
var navIsExternalChild = navObjectChild.isExternal;
// Set properties for a new navigation node.
var nnciChild = new SP.NavigationNodeCreationInformation();
nnciChild.set_title(navTitleChild);
nnciChild.set_url(navUrlChild);
nnciChild.set_isExternal(navIsExternalChild);
// Create node as the last node in the collection.
nnciChild.set_asLastNode(true);
quickLaunchNodeCollectionGetData.add(nnciChild);
}
clientContextChild.load(quickLaunchNodeCollectionGetData);
clientContextChild.executeQueryAsync(onQueryNavSucceededChild, onQueryNavFailedChild);
console.log(quickLaunchNodeCollectionGetData);
}
}
The error I'm getting with this is: Cannot Read Property "get_children" of undefined. Any help is greatly appreciated.
Reference: idOfParentNode is being pulled from a different script which actually works and pulls the correct id's of all the Headings. I've assigned the script that pulls these id's to populate a drop down that users can select from with values.
Example in my HTML:
<option value="5">Five</option>
Where 5 is the idOfParentNode - also my Header that I want a child node to go into.
EDIT: I have also tried this, with error.
function addNavigationNodesChild() {
var idOfParentNode = $('#selectChildParentNavNode').val();
var nameOfParentNode = $('#selectChildParentNavNode option:selected').text();
var clientContextChild = new SP.ClientContext(siteRelURL);
if (clientContextChild != undefined && clientContextChild != null) {
var webChild = clientContextChild.get_web();
var quickLaunchNodeCollection = webChild.get_navigation().getNodeById(idOfParentNode);
console.log(quickLaunchNodeCollection);
for (var c = 0; c < navArrayChild.length; c++) {
var navObjectChild = navArrayChild[c];
var navTitleChild = navObjectChild.title;
var navUrlChild = navObjectChild.url;
var navIsExternalChild = navObjectChild.isExternal;
// Set properties for a new navigation node.
var nnciChild = new SP.NavigationNodeCreationInformation();
nnciChild.set_title(navTitleChild);
nnciChild.set_url(navUrlChild);
nnciChild.set_isExternal(navIsExternalChild);
// Create node as the last node in the collection.
nnciChild.set_asLastNode(true);
quickLaunchNodeCollection.get_children.add(nnciChild);
quickLaunchNodeCollection.update();
}
clientContextChild.load(quickLaunchNodeCollection);
clientContextChild.executeQueryAsync(onQueryNavSucceededChild, onQueryNavFailedChild);
console.log(quickLaunchNodeCollection);
}
}
Here, I'm using getNodeById method but I get an error stating webChild.get_navigation(...).getNodeById is not a function