子网站的共享点子网站

sharepoint subsite of subsite

我需要从 sharpeoint 服务器循环所有文档库,但是我在子站点的子站点失败了

首先我使用下面的方法获取我的 SiteCollections

internal List<string> GetAllSite()
    {
        List<string> siteCollection = new List<string>();
        try
        {

            SPSecurity.RunWithElevatedPrivileges(delegate ()
            {
                SPServiceCollection services = SPFarm.Local.Services;
                foreach (SPService curService in services)
                {
                    if (curService is SPWebService)
                    {
                        SPWebService webService = (SPWebService)curService;


                        foreach (SPWebApplication webApp in webService.WebApplications)
                        {
                            foreach (SPSite sc in webApp.Sites)
                            {
                                try
                                {
                                    siteCollection.Add(sc.Url);
                                    Console.WriteLine("Do something with site at: {0}", sc.Url);
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine("Exception occured: {0}\r\n{1}", e.Message, e.StackTrace);
                                }
                            }
                        }
                    }
                }


            });
        }
        catch (Exception ex)
        {

            throw;
        }

        return siteCollection;
    }

之后,我使用 return 网站集 url 循环访问子网站,如下代码

 using (SPSite site = new SPSite(SiteCollectionUrl))
                {

                    foreach (SPWeb web in site.AllWebs)
                    {
                          //i get the subsite url from here

                    }

                }

现在这是我的问题,正如我之前提到的,我想获得子网站的子网站,所以我将我的子网站 url 传递给 SPSite,但是它只会循环 SiteCollections 而不是我的子网站

foreach(site.AllWebs 中的 SPWeb 网站)<-- 我的意思是这里,这里只会循环我的站点集合项目虽然我已经将我的子站点 url 作为参数传递

如果您只想 直接 给定站点的后代子站点,您应该使用 SPWeb.Webs 属性 而不是 SPSite.AllWebs 属性.

Gets a website collection object that represents all websites immediately beneath the website, excluding children of those websites.

using (SPSite siteCollection = new SPSite(SiteCollectionUrl))
{
    using(SPWeb parentSite = siteCollection.OpenWeb())
    {
       foreach (SPWeb subsite in parentSite.Webs)
       {
            // do something with subsite here
            subsite.Dispose();
       }
    }
    siteCollection.Dispose();
}

如果您还需要获取这些子站点的所有子站点,直到所有后代(直接和间接)都被考虑在内,您应该在循环中使用递归。

请注意,为每个子站点实例化和处理 SPWeb 对象可能会造成性能负担。

如果您不需要访问每个子网站的全部属性,最好使用网站集的 AllWebs 属性 来获取对 SPWebCollection 的引用目的。然后,您可以使用 SPWebCollectionWebsInfo 属性 来获取 List<> 轻量级 SPWebInfo 对象,如 .

中所述