2009-09-18 51 views
1

我試圖在前端網站上構建輔助導航,但我無法解決問題。在下面的代碼,輸出顯示了類似於:SPNavigation - 顯示用戶定義的導航

 
water 
-----water 
-----pollination 
-----dormancy 
pollination 
-----water 
-----pollination 
-----dormancy 
dormancy 
-----water 
-----pollination 
-----dormancy 
// Loop through Root Webs in Site Collection 
foreach (SPWeb TopLevelWebs in CurrentSite.AllWebs) 
{ 
    SPNavigationNodeCollection FirstLevelWebs = TopLevelWebs.Navigation.GlobalNodes[0].Children; 

    // Loop through each Child of Web 
    foreach (SPNavigationNode FirstLevelWebChild in FirstLevelWebs) 
    { 
     // Skip over pages and only look at webs (or "Area" as it is called in 'Properties') 
     if (FirstLevelWebChild.Properties["NodeType"].ToString() != "Page") 
     { 
      Response.Write(FirstLevelWebChild.Title + "<br />"); 

      SPNavigationNodeCollection SecondLevelWebs = FirstLevelWebChild.Navigation.GlobalNodes[0].Children; 
      foreach (SPNavigationNode SecondLevelWebChild in SecondLevelWebs) 
      { 
       // Skip over pages and only look at webs (or "Area" as it is called in 'Properties') 
       if (SecondLevelWebChild.Properties["NodeType"].ToString() != "Page") 
       { 
        Response.Write("-----" + SecondLevelWebChild.Title + "<br />"); 
       } 
      } 
     } 
    } 
} 

爲什麼上面沒有邏輯工作的頂層節點下的子網站?我還沒有完全理解「GlobalNodes」屬性,所以我必須假設問題出在哪裏。但我無法想出一個解決方案。

任何幫助將不勝感激。

回答

1

我知道這是來自不久前,但代碼中存在一個錯誤,並且由於這對我來說在谷歌搜索結果中非常高,所以我想我應該糾正其他任何遇到它的人。

SPNavigationNodeCollection SecondLevelWebs = FirstLevelWebChild.Navigation.GlobalNodes [0] .Children;

應該是

SPNavigationNodeCollection SecondLevelWebs = FirstLevelWebChild.Children;

通過使用FirstLevelWebChild.Navigation.GlobalNodes,您將再次獲得根導航,而不是您想要獲得的子導航。