How to: Determine if a Navigation Node Exists
Posted
Thursday, February 21, 2008 12:58 PM
by
CoreyRoth
Today's topic seems like a simple task and it really is if you take a brute force method of solving it, however I wish I knew of a better solution. When specifically working with the TopNavigationBar collection, the only way to access the content is using an integer indexer (again why can't SharePoint collection ever have a contains method?). There is neither a contains method nor is there an indexer that you can pass a title or URL into. This is unfortunate. So how do you determine if a node is there? Brute force. Iterate through the collection and look for something that matches the title and URL. In this case, assume currentSite is an SPWeb object of the current site.
public bool NavigationNodeExists(string title, string url)
{ for (int i = 0; i < currentSite.Navigation.TopNavigationBar.Count; i++) { if ((string.Compare(currentSite.Navigation.TopNavigationBar[i].Title, title, StringComparison.CurrentCultureIgnoreCase) == 0)
&& (string.Compare(currentSite.Navigation.TopNavigationBar[i].Url, url, StringComparison.OrdinalIgnoreCase) == 0)
) return true; } return false;}
I have not found a better way to do this. Unfortunately, this looks like such a noob solution, but I really can't think of anything better. Know a better way? Leave a comment please.