0

这是我的代码:

   try{
       using (Microsoft.SharePoint.Client.ClientContext context = new Microsoft.SharePoint.Client.ClientContext(siteURL))
            {
                #region get the name of the file and check if the  file is valid

                context.AuthenticationMode = Microsoft.SharePoint.Client.ClientAuthenticationMode.FormsAuthentication;
                Microsoft.SharePoint.Client.FormsAuthenticationLoginInfo formsAuthInfo = new
                Microsoft.SharePoint.Client.FormsAuthenticationLoginInfo(UserName, Password);
                context.FormsAuthenticationLoginInfo = formsAuthInfo;
                File file = context.Web.GetFileByServerRelativeUrl(relativeFilePath);
                context.Load(file);
                context.ExecuteQuery();
                documentName = Convert.ToString(file.Name);                    
                #endregion

    }
    }
    catch(ServerUnauthorizedAccessexception ex)
    {

    }
    catch(WebException We)
    {

    }

    catch (ServerException s)
    {

if (s.Message == "File Not Found.")
{
    htmlClose = "<html><title>Cannot Get File</title><body><script type='text/javascript'>alert('The specified file is not found in Sharepoint.');self.close();</script></body></html>";
}
else
{
    htmlClose = "<html><title>Cannot Get File</title><body><script type='text/javascript'>alert('Unable to retrieve file.Please contact your administrator');self.close();</script></body></html>";
}
httpContext.Response.Write(htmlClose);
httpContext.Response.Flush();
    }

我想知道我为确保在 sharepoint 中找不到文件所做的工作是否正确。

基本上我已经使用异常消息来验证是否在共享点中找不到文件。抛出异常的代码是:

    context.Load(file);
    context.ExecuteQuery();

我使用了不同的捕获块来捕获:ServerUnauthorizedAccessexception、WebException 和服务器异常。我发现服务器异常是用于确保在共享点中找不到文件的异常。在我完成的那部分代码中

   catch (ServerException s)
   {

   if (s.Message == "File Not Found.")
   {
   htmlClose = "<html><title>Cannot Get File</title><body><script type='text/javascript'>alert('The specified file is not found in Sharepoint.');self.close();</script></body></html>";
   }
   else
   {
   htmlClose = "<html><title>Cannot Get File</title><body><script type='text/javascript'>alert('Unable to retrieve file.Please contact your administrator');self.close();</script></body></html>";
   }
   httpContext.Response.Write(htmlClose);
   httpContext.Response.Flush();
   }
4

1 回答 1

2

当您获得 aWebException时,您可以使用该Response属性访问来自 Web 服务器的响应(如果有的话)。然后,您可以将其转换为适当的子类,并检查错误代码:

catch (WebException e)
{
    var response = (HttpWebResponse) e.Response;
    if (response != null && response.StatusCode == HttpStatusCode.NotFound)
    {
        // You got a 404...
    }
}
于 2013-12-30T10:14:09.437 回答