Tuesday, August 14, 2012

jQuery to override SharePoint OOTB Upload.aspx default for "Add as a new version to existing files" checkbox

A common query from SharePoint users with versioning enabled on document libraries is to default the "Add as a new version to existing files" checkbox on the OOTB upload.aspx page to unchecked. If you google how to uncheck it by default you'll get lots of entries showing you how to simply edit the Upload.aspx file in the \TEMPLATE\LAYOUTS\ folder in the 12/14 hive. This works fine of course if you don't care about tomorrow and having a supported/upgradeable SharePoint installation.

If you do there is a better way as described by Per Jakobsen here.
Using the AdditionalPageHead delegate control to inject javascript to the page through a feature not only gives you a supportable system but allows you to switch the default to checked or not checked by activating or deactivating the feature.

I tried a variation of this on a SharePoint 2007 installation and it worked fine, after an upgrade to 2010 it stopped working however. Having spent five minutes trying to debug the issue I decided it would be swifter to just change it to use jQuery's $(document).ready function rather than solve the specific problem. There seems to be others having weird things going on with _spBodyOnLoadFunctionNames.push in 2010, so this is probably a quicker fix (at least if you've already got jQuery available on the page). If you don't you could add a reference to it in the DefaultUploadOverwriteOff.ascx user control.

The change to Jakobsen's javascript is simple, just replace the function DefaultUploadOverwriteOff() with this:

?
1
2
3
4
5
6
$(document).ready(function() {
    if (document.title == "Upload Document") {
        $("input[id$='OverwriteSingle']").attr("checked",false);
        $("input[id$='OverwriteMultiple']").attr("checked",false);
    }
});

and remove the last line: _spBodyOnLoadFunctionNames.push('DefaultUploadOverwriteOff');

This now works fine on both 2007 and 2010.

No comments:

Post a Comment