-1

我必须使用 CSOM 打印共享点站点下的子网站列表。我使用了这段代码和我的服务器凭据,但我在 foreach 循环的第二行进入了一个无限循环。这条线是

getSubWebs(新路径);

static string mainpath = "http://triad102:1001";
     static void Main(string[] args)
     {
         getSubWebs(mainpath);
         Console.Read();
     }
     public static  void  getSubWebs(string path)
     {          
         try
         {
             ClientContext clientContext = new ClientContext( path );
             Web oWebsite = clientContext.Web;
             clientContext.Load(oWebsite, website => website.Webs, website => website.Title);
             clientContext.ExecuteQuery();
             foreach (Web orWebsite in oWebsite.Webs)
             {
                 string newpath = mainpath + orWebsite.ServerRelativeUrl;
                 getSubWebs(newpath);
                 Console.WriteLine(newpath + "\n" + orWebsite.Title );
             }
         }
         catch (Exception ex)
         {                

         }           
     }

必须进行哪些代码更改才能检索子网站?

4

1 回答 1

3

您正在将子路由添加到变量mainpath

static string mainpath = "http://triad102:1001";

public static  void  getSubWebs(string path)
{          
    try
    {
        ...
        foreach (Web orWebsite in oWebsite.Webs)
        {
            string newpath = mainpath + orWebsite.ServerRelativeUrl; //<---- MISTAKE
            getSubWebs(newpath);
        }
    }
    catch (Exception ex)
    {          
    }           
}

这会导致无限循环,因为您总是在相同的路由上循环。例如:

主路径 ="http://triad102:1001"

  1. 首先循环你的newPath将是"http://triad102:1001/subroute"
  2. 然后,您将使用 Mainpath 调用 getSubWebs,它将再次从 1.) 开始递归。

将您的子路由添加到这样的路径:

static string mainpath = "http://triad102:1001";

public static  void  getSubWebs(string path)
{          
    try
    {
        ...
        foreach (Web orWebsite in oWebsite.Webs)
        {
            string newpath = path + orWebsite.ServerRelativeUrl; 
            getSubWebs(newpath);
        }
    }
    catch (Exception ex)
    {          
    }           
}
于 2015-12-18T09:24:57.370 回答