imageIf you have ever used SharePoint, you know what I’m talking about.  You have a file you want to upload into a document library.  Fantastic.  You click Upload document from the ribbon, and get the dialog box below.

You have the option to “overwrite existing files” and it is checked by default.  You want this under normal circumstances.  You want to have versioning turned on in the library, so when you do overwrite an existing file, it retains the previous copy for prosperity and all forms of natural disasters. 

image

However, I had a client (and there are others out there, you know who you are) who wanted the ability to have this setting unchecked by default for a specific library. 

The Easy Answer

This is where everyone on the internet gives the easy answer.  This answer is assuming you are on-premise of course (not using SharePoint Online in Office 365 or some other hosting version).  The first thing to understand is that the dialog box you see (“Upload Document”) is the same file that is used for every single library in SharePoint! 

imageSo you are directed to go to the upload.aspx file in the 14 hive (2010) or 15 hive (2013).  This file contains the actual code of the dialog box page.  As directed, you crack open the file and go to the line that contains “Overwritesingle”.  This is line 248 for me.  You change the Checked=”true” to Checked=”false”.  Save your file, refresh, and BLAM it’s unchecked. 

Great!  Except that remember I mentioned this one file is used everywhere?  So guess what, it’s unchecked everywhere!  That’s actually not what we want.  We only want this on ONE library. 

Target a Single Library

If your goal is to only target one library, don’t perform the above steps.  There are multiple blogs and forums out there that offer solutions.  Basically you can put some JavaScript in your master page that can target a specific library.  This blog has a good example of how to do this.

What you do is basically place this code in your master page, and the code checks if the library tied to the open dialog box matches the one you want by GUID.  If it matches, it can hide or uncheck the control. 

I just wanted to uncheck it, so I just used this line after the if statement:

$("input[type='checkbox']").attr('checked',false);

You go back to your library, and you click the “Add document” link, and it works!  It’s unchecked.  You think your job is done and good to go.  Alas, you’re not.  And this brings me to the point of this blog.

This should work on 2010, 2013 and 2016.  This should even work in SharePoint Online.  One different possibility of not using the master page is JavaScript Embed / Injection. 

One More Step

As I mentioned, this fixes ONLY if you click the “Add Document” link from the bottom of the library.  When you click File –> Upload Document from the ribbon, guess what.  It’s checked again.  What’s going on?  After some javascript debugging, we figured out why.  I’ll spare you the debugging, but it boils down to a text compare mismatch. 

The JavaScript checks the context of the URL of the dialog box, and looks for the GUID.  This is the line:

if (location.href.indexOf('A9A5E8AC-3FC6-4279-AE8D-A7C8B9688209') >= 0)

When you click Add document from the bottom of the page,  it matches because the URL the code checks is in the same format (encoded):

http://<sharepoint URL>/site/_layouts/Upload.aspx?List={8CBCBFB4-B45D-43BD-A261-4BC3D8A09CC2}&RootFolder=&IsDlg=1

Because this is encoded, it works.  Now let’s look at the format when loaded from the Upload Document ribbon button:

http://<sharepoint URL>/site/_layouts/Upload.aspx?List=%7B8CBCBFB4%2DB45D%2D43BD%2DA261%2D4BC3D8A09CC2%7D&RootFolder=%2Fsite%2FShared%20Documents&Source=http%3A%2F%2Fsharepoint%2Fsite%2FShared%20Documents%2FForms%2FAllItems%2Easpx&IsDlg=1

Well strike one for Microsoft for consistency.  Notice how the GUIDs are not encoded, meaning ugly raw HTML.  Ok now we know why, and it’s an easy fix.

There are multiple ways and can vary depending on your preference in JavaScript.  The quickest way was just to make the if statement an OR statement like this:

if ((location.href.indexOf(8CBCBFB4-B45D-43BD-A261-4BC3D8A09CC2') >= 0) || (location.href.indexOf(8CBCBFB4%2DB45D%2D43BD%2DA261%2D4BC3D8A09CC2) >= 0))

Of course be sure to replace the GUID with the ID of YOUR library.  NOW our solution is fully functional! 

After you’re done congratulating yourself, be sure to check out our upcoming events at https://events.c5insight.com.  There are some great free webinars on SharePoint and other topics coming up for sure.  But I want to call out that you’ve only got a few day left to get the lower price of our Business Workshop: Building A Roadmap for Intranet Collaboration Success.  We hope to see you there!