C# exception handling in sharepoint know for sure file is not found

4.1k Views Asked by At

This is my Code:

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

I want to know if what I have done for making sure file not found in sharepoint is correct.

Basically I have used exception message to validate if file is not found in sharepoint. The code where the exception will get thrown is:

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

I have used different catch blocks for catching:ServerUnauthorizedAccessexception,WebException and server exception. I found that Server exception is the one used to make sure file is not found in sharepoint. In that part of the code I have done

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

There are 1 best solutions below

15
On

When you get a WebException, you can use the Response property to access the response from the web server (if there is one). You can then cast that to the appropriate subclass, and check the error code:

catch (WebException e)
{
    var response = (HttpWebResponse) e.Response;
    if (response != null && response.StatusCode == HttpStatusCode.NotFound)
    {
        // You got a 404...
    }
}