2009-06-24 57 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