MOSS SpNavigationNode.Children is always empty

I'm trying to get all the child nodes of a navigation nodeset back in sharepoint, the SDK suggests I should be doing something like this:

NodeColl = objSite.Navigation.TopNavigationBar 
Dim Node as SPNavigationNode

For Each Node In NodeColl
  if Node.IsVisible then
    Response.Write("<siteMapNode url=""" & Node.Url & """ title=""" & Node.Title & """  description=""" & Node.Title & """ >" & Environment.NewLine)
    Dim SubChildNodes as SPNavigationNodeCollection = Node.Children
    Response.Write(SubChildNodes.Count) 'returns 0 always even though I know theres over 20 nodes in some of the sections
    Dim ChildNode as SPNavigationNode
    For Each ChildNode in SubChildNodes
      if ChildNode.IsVisible then
        Response.Write("<siteMapNode url=""" & ChildNode.Url & """ title=""" & ChildNode.Title & """  description=""" & ChildNode.Title & """ />" & Environment.NewLine)
      End if
    Next
    Response.Write("</siteMapNode>" & Environment.NewLine)
  End If
Next

      

however whenever I do this it lists the top level navigation nodes but I cannot get them to display them.

0


source to share


2 answers


I faced the same problem: I tried to access SPWeb.Navigation.Quicklaunch

from a function receiver a web scope function activated from onet.xml, but SPWeb.Navigation.QuickLaunch.Count

it was always 0, although I definitely added instance examples in other functions activated earlier in the same onet.xml.

The solution for me was to open a new SPSite and a new SPWeb in my function receiver, after which I was able to access the quickstart items. For example this worked for me:



using (SPSite site = new SPSite("http://yourserver/"))
{
  using (SPweb web = site.OpenWeb("theweb"))
  {
    web.Navigation.QuickLaunch.Count ; // greater than zero

    // manipulate your quick launch here
  }
}

      

I assume this is because creating a new SPWeb object is loading the last state in the database from the database, and the SPWeb passed to my function receiver does not represent the last state. But that's me gues

0


source


I have the same problem, I found the solution as

using (SPSite site = new SPSite("http://server"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPNavigationNode quicklaunch = web.Navigation.GetNodeById(1025);
        if (quicklaunch != null)
        {
            foreach (SPNavigationNode heading in quicklaunch.Children)
            {
                PrintNode(heading);
            }
        }
    }
}

static void PrintNode(SPNavigationNode node)
{
    foreach (SPNavigationNode item in node.Children)
        PrintNode(item);
}

      



Make sure you have a SiteMapDataSource

quickstart related to your home page.

0


source







All Articles