これは私のコードです:
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();
}
共有ポイントにファイルが見つからないことを確認するために行ったことが正しいかどうかを知りたいです。
基本的に、共有ポイントにファイルが見つからないかどうかを検証するために例外メッセージを使用しました。例外がスローされるコードは次のとおりです。
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();
}