2009-06-24 81 views
2

我正在嘗試爲我的MOSS發佈站點創建站點地圖,我有兩種方法,但似乎與兩者都卡住了。創建Sharepoint/MOSS站點地圖

我的第一種方法是使用PortalSiteMapProvider,這是已經創建並很好地緩存...

PublishingWeb rootWeb = PublishingWeb.GetPublishingWeb(SPContext.Current.Site.RootWeb); 

//Get the URL of the default page in the web 
string defaultPageUrl = rootWeb.DefaultPage.ServerRelativeUrl; 

PortalListItemSiteMapNode webNode = (PortalListItemSiteMapNode)PortalSiteMapProvider.CurrentNavSiteMapProviderNoEncode.FindSiteMapNode(defaultPageUrl); 

HttpContext.Current.Response.Output.WriteLine("Top Level: " + webNode.Title.ToString() + "<br />"); 

//iterate through each one of the pages and subsites 
foreach (SiteMapNode smnTopLevelItem in webNode.ParentNode.ChildNodes) 
{ 

    HttpContext.Current.Response.Output.WriteLine(smnTopLevelItem.Title.ToString() + "<br />"); 

    //if the current sitemap has children, create a submenu for it 
    if (smnTopLevelItem.HasChildNodes) 
    { 
     foreach (SiteMapNode smnChildItem in smnTopLevelItem.ChildNodes) 
     { 
     HttpContext.Current.Response.Output.WriteLine(smnChildItem.Title.ToString() + "<br />"); 
     } 
    } 
} 

HttpContext.Current.Response.End(); 

但這似乎返回網站集中一切(如列表,surverys)。我只想顯示Sharepoint網站。

我的另一種方法是使用這段代碼..

SPSite siteCollection = new SPSite("http://example.org"); 
SPWebCollection sites = siteCollection.AllWebs; 
foreach (SPWeb site in sites) 
{ 
    Console.WriteLine(site.Title.ToString() + " " + site.ServerRelativeUrl.ToString()); 
} 

這是完美的,除了在平面列表返回所有的網的問題。

理想情況下,我希望能夠添加縮進來顯示子網。

回答

1

感謝大家回信,這是我想出了

 public ListSiteMap() 
    { 
     PortalSiteMapProvider portalProvider1 = PortalSiteMapProvider.WebSiteMapProvider; 
     portalProvider1.DynamicChildLimit = 0; 
     portalProvider1.EncodeOutput = true; 

     SPWeb web = SPContext.Current.Site.RootWeb; 

     PortalSiteMapNode webNode = (PortalSiteMapNode)portalProvider1.FindSiteMapNode(web.ServerRelativeUrl); 

     if (webNode == null || webNode.Type != NodeTypes.Area) return; 

     Console.WriteLine(webNode.Title.ToString() + " - " + webNode.Description.ToString()); 

     // get the child nodes (sub sites) 
     ProcessSubWeb(webNode); 
    } 

    private void ProcessSubWeb(PortalSiteMapNode webNode) 
    { 
     foreach (PortalSiteMapNode childNode in webNode.ChildNodes) 
     { 
      Console.WriteLine(childNode.Title.ToString() + " - " + childNode.Description.ToString()); 

      //if the current web has children, call method again 
      if (childNode.HasChildNodes) 
      { 
       ProcessSubWeb(childNode); 
      } 
     } 
    } 

我發現這些文章,讓

http://blogs.msdn.com/ecm/archive/2007/05/23/increased-performance-for-moss-apps-using-the-portalsitemapprovider.aspx

http://blogs.mosshosting.com/archive/tags/SharePoint%20Object%20Model/default.aspx

http://www.hezser.de/blog/archive/tags/SPQuery/default.aspx

6

通常使用遞歸對象模型是一個壞主意。這樣做非常緩慢且資源密集。 PortalSiteMapProvider已預先緩存給您,並且可以在毫秒內穿透整個站點結構。

關於您對SPSite.AllWebs的問題,該屬性確實返回所有網站的平面列表。這就是它的目的。如果您只想要一個即時子網站的列表,請使用SPSite.RootWeb.Webs屬性。緩衝.Webs屬性中的每個SPWeb,並依次調用其.Webs屬性以獲取樹視圖。

此外,在處理對象模型時,請確保處理每個網頁和網站。如果你不這樣做,會造成史詩般的不好的問題。這包括在.Webs集合中處理每個網頁,即使您尚未觸摸它。

編輯:

爲了讓PortalSiteMapProvider只返回網,其IncludePages屬性設置爲false

+0

確定冷靜,我想使用PortalSiteMapProvider但我不知道該如何限制輸出才網。 – Rob 2009-06-24 16:43:46

2

您是否嘗試使用PortalSiteMapNode.Type屬性檢查foreach循環中每個節點的類型並僅顯示NodeTypes.Area類型的節點?

+0

優秀的答案! – Colin 2009-06-25 07:09:03