SPNavigationNode.IsVisible returns always true

SPNavigationNode.IsVisible returns always true no matter whether the web is marked as hidden or visible in the navigation.

Example :

using(SPSite Site = new SPSite(URL))
{
  using(SPWeb Web = Site.OpenWeb())
 {
    SPNavigationNodeCollection Nodes = Web.Navigation.TopNavigationBar;

    foreach(SPNavigationNode Node in Nodes)
    {
       if(Node.IsVisible)...//returns always true even if the node is marked as hidden..

    }
 }
}

There is a workaround provided here. However this is applicable for Publishing sites. For other sites, the following workaround can be used.

private static bool isHiddenWeb(SPWeb targetWeb)
{
   SPSite site = new SPSite(URL);
   SPWeb web = site.RootWeb;

   string GlobalNavigationExcludes = (string)web.AllProperties["__GlobalNavigationExcludes"];

   Hashtable ReturnExcludedUrls = new Hashtable();
   if (!String.IsNullOrEmpty(GlobalNavigationExcludes))
  {
     string[] ExcludedGuids = GlobalNavigationExcludes.Split(';');
     for (int i = 0; i < ExcludedGuids.Length - 1; i++)
    {
        string guid = ExcludedGuids[i];
        Guid ExcludedGuid = new Guid(guid);
        SPWeb SubWeb = web.Webs[ExcludedGuid];

        if (SubWeb.Url == targetWeb.Url)
        {
           return true;
        }
    }
 }
 else
 {
    return false;
  }
 return false;
}

No comments: