0

I use this code, and it is working:

SP.NavigationNodeCreationInformation newRootNode = new SP.NavigationNodeCreationInformation();
newRootNode.Title = nodeTitle;
newRootNode.Url = _web.Url + "/Lists";
//newRootNode.Url = _web.Url;
_web.Navigation.TopNavigationBar.Add(newRootNode);
await RootCtx.ExecuteQueryAsync();

But the code is working only as the url has the "/Lists" appended. In fact if I uncomment the following line the Top link doesn't show up anymore! Why?

Besides I'd like to have this top navigation link act only as placeholder for other sub-links, so I'd like to have this link "unclickable". is it possible?

  • can you add newRootNode.IsExternal = true; and check ? Also, its mandatory to have a link URL, so unclickable is not possible. – Gautam Sheth Aug 29 '18 at 5:10
  • I have tried IsExternal but seems nothing different. The problem is that if the user accidentally clicks on the link instead of just hovering above it, it goes to "404 NOT FOUND" blank page! – spiderman Aug 29 '18 at 9:10
0

If you want to create a node as a heading without a clickable link, you don't need to specify Url for the node.

// Get the Gloabl navigation node collection.
NavigationNodeCollection collTopNavigationNode = web.Navigation.TopNavigationBar;
//create a heading without a clickable link    
NavigationNodeCreationInformation ciNavicationNode = new NavigationNodeCreationInformation();
ciNavicationNode.Title = "New Node";
ciNavicationNode.AsLastNode = true;
ciNavicationNode.IsExternal=true;
collTopNavigationNode.Add(ciNavicationNode);

For the link, I reproduced your issue.

Have checked:

(Create a Heading and add sub-links to a heading)

string siteUrl = "http://sp/sites/team";
ClientContext clientContext = new ClientContext(siteUrl);
Web web = clientContext.Web;
clientContext.Load(web);

// Get the Gloabl navigation node collection.
NavigationNodeCollection collTopNavigationNode = web.Navigation.TopNavigationBar;

// create a heading
// Set properties for a new navigation node.
NavigationNodeCreationInformation ciNavicationNode = new NavigationNodeCreationInformation();
ciNavicationNode.Title = "New Node";
ciNavicationNode.AsLastNode = true;
ciNavicationNode.IsExternal=true;
var head = collTopNavigationNode.Add(ciNavicationNode);
//clientContext.Load(collTopNavigationNode);
clientContext.Load(head);
clientContext.ExecuteQuery();

// create links in heading
NavigationNodeCreationInformation link = new NavigationNodeCreationInformation();
link.Title = web.Title;

//working
link.Url = "http://sp/sites/team"; //works
//link.Url = web.Url + "/Lists/"; //works
//link.Url = "http://sp"+web.ServerRelativeUrl+"/Lists/"; //works

//non-working
//link.Url = "/sites/team"; //does not work
//link.Url = siteUrl; //does not work
//link.Url = web.Url; //does not work 
//link.Url = web.Url + "/"; //does not work
//link.Url = web.ServerRelativeUrl; //does not works
//link.Url = "http://sp"+web.ServerRelativeUrl; //does not work 
//link.Url = "http://sp"+web.ServerRelativeUrl+"/"; //does not work  

link.IsExternal = true;
clientContext.Load(head.Children.Add(link));          
clientContext.ExecuteQuery();

Working:

link.Url = "http://sp/sites/team"; //works
link.Url = web.Url + "/Lists/"; //works
link.Url = "http://sp"+web.ServerRelativeUrl+"/Lists/"; //works

non-working:

link.Url = "/sites/team"; //does not work
link.Url = siteUrl; //does not work
link.Url = web.Url; //does not work 
link.Url = web.Url + "/"; //does not work
link.Url = web.ServerRelativeUrl; //does not works
link.Url = "http://sp"+web.ServerRelativeUrl; //does not work 
link.Url = "http://sp"+web.ServerRelativeUrl+"/"; //does not work 

You may need to enter the Url manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.