Add a security trimmed node to Sharepoint top bar navigation

After programatically provisioning a new SPWeb object, it is sometimes necessary to create a new entry on the root web top bar menu.
So far so good, but what happens if the subsite has different permissions from the root site. What if someone with access to the root site sees the menu entry for a site that he’s not entitled to see?

The following piece of code allows the creation of the menu node with swcurity trimming: only the users with permissions on the sub-sites will see the nodes on the menu.

Lets take for example the baseWeb onject as the root SPWeb and the provisionedWeb object as the recent provisioned one.

public void aaa()
        var menuNodes= baseWeb.Navigation.TopNavigationBar;
	// Check for an existing link to the subsite.
	SPNavigationNode newNode = menuNodes
		.FirstOrDefault(n => n.Url.Equals(siteNew.ServerRelativeUrl));
	// No link, so add one.
	if (newNode == null)
		// Create the node.
		newNode = new SPNavigationNode(provisionedWeb .Title, provisionedWeb .ServerRelativeUrl);
		// Add it to the collection.
		newNode = menuNodes.AddAsLast(newNode);

Leave a Reply

Your email address will not be published. Required fields are marked *



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>