Disabling Additional Form Validation In Sharepoint 2010

18 November 2010

This problem cost me about four hours of my life, and lots of tedious debugging in Firebug (what I would do without Firebug in my everyday development work, I am not quite sure...)

The issue that I had was in a custom ASP.NET control hosted in Sharepoint that had a download file button. Prior to downloading a file, everything works as expected and all javascript calls proceed as per usual. However, after you click the download button and the Request (with the file contents) is sent to the browser, no subsequent javascript calls that post back to the server work.

It turns out this happens because Sharepoint is trying to validate for you - as part of a regular postback you get a full page reload, so Sharepoint does some validation to ensure the page has been reloaded after each postback to a page. However, if you're closing the HttpRequest as part of the postback (to send a file to the user, for example), the page never gets reloaded and thus doesn't get the updated validation information, causing Sharepoint to reject all your attempts at doing a subsequent postback.

Long story short: if you are having troubles with download controls in Sharepoint not working after being served a file, you can get around this through disabling the validation checks by specifying a property in Javascript:

<script type='text/javascript'>_spSuppressFormOnSubmitWrapper=true;</script>

Setting _spSuppressFormOnSubmitWrapper stops Sharepoint from intercepting your form submit requests and stops the behaviour mentioned above.

In most cases you would want to leave this on (preventing users from doing triple postbacks in quick succession by clicking very fast, for example), but I think it's a reasonable tradeoff to turn off validation but allow the page to continue working after a file is served to the browser.

If you're interested in the nitty gritty of the Javascript flow from when you click a button and when the submit event is triggered by the browser, I highly recommend stepping through using Firebug.

Tags: download, Javascript, postback, SharePoint

Add a Comment

No Comments