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();
    }

共有ポイントにファイルが見つからないことを確認するために行ったことが正しいかどうかを知りたいです。

基本的に、共有ポイントにファイルが見つからないかどうかを検証するために例外メッセージを使用しました。例外がスローされるコードは次のとおりです。

    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

を取得するWebExceptionと、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 に答える