我试图建立一个网站地图,为我的莫斯出版的网站,我有两个办法,但似乎坚持的两个。

我的第一个方法是使用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());
}

这是完美的,除了问题的回返所有网在一个平坦的名单。

理想情况下,我希望能够增加压痕显示儿童网。

有帮助吗?

解决方案 3

谢谢你的回信每一个人,这是什么我想到了

        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

其他提示

一般这是一个坏主意,使用对象模型的递归。这是非常缓慢和资源密集型做到这一点。 PortalSiteMapProvider被预高速缓存的换你,并且可以通过在毫秒的整个站点结构撕裂。

关于你提到的有关SPSite.AllWebs问题,即属性确实返回所有网的平面列表。这就是它是。如果你只想要的直接的子网的列表,请使用SPSite.RootWeb.Webs属性。递归超过在SPWeb属性中的每个.Webs,并调用在轮到自己.Webs属性获取树视图。

此外,使用对象模型打交道时,请务必将处理每一个网站和站点即可。如果你不这样做,这将导致不好的史诗问题。这包括每一个网络配置的.Webs集合中,即使你没有碰过它。

编辑:

要获得PortalSiteMapProvider只返回网,设置其IncludePages属性false

您是否尝试使用PortalSiteMapNode.Type属性和显示类型NodeTypes.Area的唯一节点检查每个节点的类型的foreach循环?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top