【问题标题】:How to loop through from a root web and all its subsites如何从根网站及其所有子网站循环访问
【发布时间】:2014-02-14 09:23:32
【问题描述】:

我遇到了一个需要解决的问题。我想遍历根网站及其所有子网站,并想设置一些属性

【问题讨论】:

  • 我已经为您提供了相同的解决方案,但是一个更正您不能对图片库应用相同的解决方案,因为图片库不支持内容批准,因此如果您必须将该解决方案应用于其他类型的列出你可以这样做
  • 感谢您的代码,但我如何设置每个网站中图像列表的属性?

标签: sharepoint sharepoint-2007


【解决方案1】:
    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite oSPsite = new SPSite("http://sharepointdev:2021"))
            {
                using (SPWeb oSPWeb = oSPsite.OpenWeb())
                {
                    foreach (SPList list in oSPWeb.Lists)
                    {
                        if ( list.ContentTypes.Count > 0)
                        {

                            foreach (SPContentType contentType in list.ContentTypes)
                            {
                                if (contentType.Name == "Document")
                                {
                                    list.EnableModeration = true;
                                    list.Update();  
                                }
                            }
                        }
                    }

                    if(oSPWeb.Webs.Count > 0) 
                    recursivewebcheck(oSPWeb); 
                }
            }
        }



       static  void recursivewebcheck(SPWeb oSPWeb)
        {

            foreach (SPWeb web in oSPWeb.Webs)
            {
                foreach (SPList list in oSPWeb.Lists)
                {
                    if (list.ContentTypes.Count > 0)
                    {

                        foreach (SPContentType contentType in list.ContentTypes)
                        {
                            if (contentType.Name == "Document")
                            {
                                list.EnableModeration = true;
                                list.Update();
                            }
                        }
                    }
                }

                if (web.Webs.Count > 0)
                {
                    recursivewebcheck(web);
                }
                web.Dispose();
            }

        }
    }
}

【讨论】:

  • 此解决方案适用于文档库并经过良好测试
  • 这只是遍历根网站中的所有列表,不是吗?
  • 不会循环访问网站集下的所有网站
  • 我对 sharepoint 还很陌生,所以也许我遇到了一些问题,但这是我的理解: oSPsite.OpenWeb() 在示例代码中打开与 SPSite 构造函数对应的 Web,即是根网。并且代码正在获取 oSPWeb.Lists,它是根 Web 中的所有列表。如何检索来自其他 SPWeb 的列表?
  • recursivewebcheck 方法可以解决这个问题
【解决方案2】:
using (SPSite oSPsite = SpSecurityHelper.GetElevatedSite(GetSiteCollection(properties)))
        {
            SPWebCollection siteWebs = oSPsite.AllWebs;
            foreach (SPWeb web in siteWebs)
            {
                try
                {
                    SPList list = null;
                    try
                    {
                        list = web.Lists["Images"];
                    }
                    catch { }

                    if (list != null)
                    {

                        list.EnableModeration = isEnabled == false ? false: true;
                        list.Update();
                    }
                }
                finally
                {
                    if (web != null)
                        web.Dispose();
                }
            }
        }

【讨论】:

    猜你喜欢
    • 2016-07-17
    • 1970-01-01
    • 2011-09-23
    • 2021-04-01
    • 2021-06-02
    • 2016-11-27
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    相关资源
    最近更新 更多