【问题标题】:Why does this code result in an infinite loop?为什么这段代码会导致无限循环?
【发布时间】:2015-12-18 09:14:14
【问题描述】:

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

getSubWebs(newpath);

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)
         {                

         }           
     }

要检索子网站需要更改哪些代码?

【问题讨论】:

  • 你收到StackOverflowException了吗?

标签: c# timeout sharepoint-2013 csom request-timed-out


【解决方案1】:

您正在将子路由添加到变量 ma​​inpath

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)
    {          
    }           
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多