这是我的代码:
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();
}