如何从根网站及其所有子网站进行循环

时间:2010-12-08 09:59:38

标签: sharepoint sharepoint-2007

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

2 个答案:

答案 0 :(得分:5)

    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();
            }

        }
    }
}

答案 1 :(得分: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();
                }
            }
        }