Handling maxRequestLength setting in MS CRM

By default, maxRequestLength setting is 4Mb in web.config. If you need to upload more than that you can manually change the web.config for MS CRM. Best way is to download IIS administration pack from http://www.iis.net/download/administrationpack and configure from IIS Manager.

After you increase the maxRequestLength you may face the HttpException when you upload more than the limit. HttpException which is saying post size limit. But this exception will throw before it will reach to your code. So you can't handle that Exception by using simple try catch statement.

So how I handle this error is simple override the ProcessRequest method from System.Web.UI.Page which is normally we inherit this class to create ASP.NET web page. Below is the sample code.

//Reading web.config setting
HttpRuntimeSection runTime = (HttpRuntimeSection)WebConfigurationManager.GetSection("system.web/httpRuntime");

//Approx 100 Kb(for page content) size has been deducted because the maxRequestLength proprty is the page size, not only the file upload size
int maxRequestLength = (runTime.MaxRequestLength - 100) * 1024;

if (context.Request.ContentLength < maxRequestLength)
{
base.ProcessRequest(context);
}
else
{
context.Server.ClearError();
context.Response.Redirect("../../_common/error/uploadFailure.aspx?hr=0x80043e08");
}

Don't forget to clear the server error from HttpContext. Otherwise CRM will assume that there is an error and show general error message. But CRM only show error when you turn off DevErrors from web.config. If it is turn on your code will working fine. It cause me to sick one whole day.

Comments