如何在ClientContext.ExecuteQuery之前检查Sharepoint连接

时间:2016-08-22 06:43:49

标签: c# sharepoint

我对sharepoint很新。在ClientContext.ExecuteQuery之前检查Sharepoint站点连接的最快和最有效的方法是什么。

ClientContext ctx = new ClientContext(ConfigurationManager.AppSettings["sharepoint siteUrl"]);

float pageLoadTime = getPageLoadTime(ctx);
if(pageLoadTime > 0.5)
{
  MessageBox.Show("Sharepoint site is not available!");
  return;
}
//do very heavy query
....
ctx.ExecuteQuery();

1 个答案:

答案 0 :(得分:0)

没有这样的标准方法。但你可以像下面的代码那样实现这个目标

   using (ClientContext sourceContext = new ClientContext("Sharepoint Url"))
        {
            try
            {                    
                sourceContext.ExecuteQuery();
                List list = sourceContext.Web.Lists.GetByTitle("Test");
                ListItemCollection itemColl = list.GetItems(CamlQuery.CreateAllItemsQuery());
                sourceContext.Load(itemColl);
                sourceContext.ExecuteQuery();
            }                
            catch (System.Net.WebException ex)
            {
                if (ex.Message == "The remote server returned an error: (404) Not Found.")
                {
                    Console.WriteLine("SharePoint not available");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }              
        }

因为" ExecuteQuery"是通过Client.svc wcf服务连接到SharePoint的方法。

希望这会有所帮助......