如何遍历SharePoint网站的所有网站和子网站 [英] How to loop through all webs and sub webs of a SharePoint Web

查看:117
本文介绍了如何遍历SharePoint网站的所有网站和子网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图遍历网络中这些网络的所有网络和子网络.

我的父网站

http://EXAMPLE.com/parentweb (这是一个子网站,而不是根网站)

子网站 http://example.com/parentweb/subweb (这是可能或可能会没有子网站)

我尝试的唯一方法是转到网站集并遍历所有网站,这是一个更长的过程,因为我知道唯一需要检查的子网站.我的网站集有多个子网站.我不想遍历所有订阅站点.

此代码有效,但是需要花费大量时间,并且会遍历所有不需要检查的子站点

 使用(SPSite站点=新的SPSite("http://myserver/mysitecol"){foreach(SPWeb网站位于site.AllWebs){}} 

解决方案

您可以使用 SPWeb 对象的 .Webs 属性访问其 direct 子子网站.

  using(SPSite站点=新的SPSite("http://myserver/mysitecol"){使用(SPWeb parentweb = site.OpenWeb("subweb/subweb"){foreach(parentweb.Webs中的SPWeb网站){}}} 

要访问Web下方的所有(包括间接)子代子站点,您可以在每个直接后代上递归访问该 .Webs 属性,但这会更直接从 SPSite 网站集对象的 .AllWebs 属性开始.

.AllWebs 返回的SPWebCollection的 .WebsInfo 属性为您提供了轻量级 SPWebInfo List<> 代码>对象.您可以使用此轻量级集合来获取您关心的Web的过滤列表,而无需实例化和处置该集合中的任何其他SPWeb.

  string webUrl ="/mysitecol/subweb/subweb";使用(SPSite站点=新的SPSite("http://myserver/mysitecol"){列表< SPWebInfo>websInfo = site.AllWebs.WebsInfo.FindAll(委托(WebsInfo webInfo){//进行过滤,以仅获取以所需网址开头的网站返回webInfo.ServerRelativeUrl.StartsWith(webUrl);});foreach(websInfo中的SPWebInfo webInfo){使用(SPWeb网站= site.OpenWeb(webInfo.Id)){}}} 

I am trying to loop through all webs and subs-webs of those webs under a web.

My Parent web

http://EXAMPLE.com/parentweb (This is a sub site not a root web)

sub webs http://example.com/parentweb/subweb ( This is one of the sub web which may or may not have sub webs)

The only thing i tried is go to the site collection and loop throug all webs which is much longer process because i know the only sub site that needs to be checked. my site collection has multiple sub sites. i don't want to loop through all subs sites.

This code works but it takes so much of time and it loops through all subsites which don't need to be checked

     using(SPSite site = new SPSite("http://myserver/mysitecol") 
           { 
               foreach(SPWeb web in site.AllWebs) 
               {
               }
           } 

解决方案

You can use the .Webs property of an SPWeb object to access its direct child subsites.

using(SPSite site = new SPSite("http://myserver/mysitecol") 
{ 
    using(SPWeb parentweb = site.OpenWeb("subweb/subweb") 
    {
        foreach(SPWeb web in parentweb.Webs)
        {

        }
    }
} 

To access all (including indirect) descendant child sites below a Web, you could access that .Webs property recursively on each direct descendent, but it would be more straightforward to start with the .AllWebs property of the SPSite site collection object.

The .WebsInfo property of the SPWebCollection returned by .AllWebs gives you a List<> of lightweight SPWebInfo objects. You can use this lightweight collection to get a filtered list of the Webs you care about without having to instantiate and dispose of any of the other SPWebs in the collection.

string webUrl = "/mysitecol/subweb/subweb";
using(SPSite site = new SPSite("http://myserver/mysitecol") 
{ 
    List<SPWebInfo> websInfo = site.AllWebs.WebsInfo.FindAll(
        delegate(WebsInfo webInfo)
        {
            // filter to get us only the webs that start with our desired URL
            return webInfo.ServerRelativeUrl.StartsWith(webUrl);
        }
    );
    foreach(SPWebInfo webInfo in websInfo)
    {
         using(SPWeb web = site.OpenWeb(webInfo.Id))
         {

         }
    }
}

这篇关于如何遍历SharePoint网站的所有网站和子网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆