How to loop through from a root web and all its su

2019-05-28 14:22发布

I came across a issue that I need to solve. I want to loop through a root web and all its subsites, and want to set some properties

2条回答
等我变得足够好
2楼-- · 2019-05-28 14:56
    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();
            }

        }
    }
}
查看更多
甜甜的少女心
3楼-- · 2019-05-28 15:01
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();
                }
            }
        }
查看更多
登录 后发表回答