Monday, October 30, 2017

Changing SharePoint list item attachment to allow multiple files

I just got this email from a colleague saying he wants to upload multiple files as attachments to an item, and having to click add and confirm each and every one separately is quite annoying and time consuming.

So I wrote a little script that changes this behavior of the OOB file upload to allow multiple files selection.

Suprisingly, it works well with multiple files. there was only one part I had to fix, which was the label it shows before you save the item. It still shows the first file name only.

Here is the JavaScript code you can use to enable it on your system today. Just add this script in your new/edit item forms and it should work:

ExecuteOrDelayUntilScriptLoaded && ExecuteOrDelayUntilScriptLoaded(function(){
var oldShowPartAttachment = window.ShowPartAttachment;
var oldOkAttach = window.OkAttach;
window.OkAttach = function(){
var index = String(FileUploadIndex);
oldOkAttach();
var index2 = String(FileUploadIndex);
if(index2 === index)//it means OK fail.
    return;
var files = GetAttachElement(FileuploadString + index);
if(files && files.files.length > 1)//more than one file
 {
var text = "";
for(var i=0;i<files.files.length; i++) text+=', '+ files.files[i].name;
text = text.slice(2);
document.getElementById("attachRow" + index).cells[0].innerHTML = text;
 }
}
window.ShowPartAttachment = function(){
oldShowPartAttachment();
var index = String(FileUploadIndex);
var files = GetAttachElement(FileuploadString + index);
if(files)
files.multiple = true;
}}, "form.js");

Also, we will be adding this little script to a future release of our forms solution, so stay tuned for the update.

Let me know if you like it!

Edit: correction in the script, the if statement was missing the {}

2 comments:

clavezza said...

Hi can you help me with getting this functionality to work on my Sharepoint Site?

Sincerely

Christopher

Shai Petel said...

Hi Christopher,

Please be aware that using this or any other code on my blog is done at your own risk. Some functionality may change and code can break over time.
Especially simple hacks such as this one.

To get it on your site, all you have to do is add this code to a JS file and register it to run on your list pages. You can do that using a CusomAction ScriptLink element for example, depending on what your environment is (Online, On Premises, which version, etc).

Simply google how to add a JS file to your site/lists, should be plenty of results for every version of SharePoint.