5

I have custom button which opens a dialog box of out of the box page Upload.aspx of a document library.

First I am getting the GUID of the library by REST API and then passing the URL to open it into the dialog.

Below is my code to open the dialog box by button click:

var openDialog = function(pageUrl) { 
    var options = {
        url: pageUrl,
        title: 'Upload Document to Upload Library',
        allowMaximize: false,
        showClose: true,
        width: 500,
        height: 300
    };
    SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);       
}

var GenerateButtonURL = function(listTitle){
    try {
        var currWebURL = _spPageContextInfo.webAbsoluteUrl;
        $.ajax(
        {
            url:  currWebURL + "/_api/web/lists/getbytitle('" + listTitle + "')?$select=Id",
            type: "GET",
            async: false,
            headers: { "Accept": "application/json;odata=verbose" },
            success: function (data, textStatus, xhr) {
                openDialog(currWebURL + '/_layouts/15/Upload.aspx?List={' + data.d.Id + '}&RootFolder=');
            },
            error: function (data, textStatus, xhr) {
                console.error("Error: " + textStatus);
            }
        });
    }
    catch (ex) {
        console.error(ex);
    }
}

The code works properly. it allow user to upload a file and it is saved to actual document library.

But my problem is when I try to select multiple file, it doesn't allow me, where the actual out of the page when I click upload from the document library it allows the multiple file upload.

I have verified both the pages have the same URL. I have tried doing it with static GUID but still no luck.

What am I missing here? Anyone have encountered such issue before?

Edit:

Found that if page is opened from the out of the box library page UI Looks as below, button text for upload is Choose Files:

enter image description here

When I open the same page with custom code, it shows below screen, button text for upload is Choose File.

enter image description here

4
  • 1
    you can try to replicate the uploadex.aspx functionality , that's available in 15\template\layouts\ folder. By default sharepoint doesn't allow multiple file upload.
    – samolpp2
    Commented Aug 28, 2017 at 8:33
  • 1
    you can create a custom upload page for your requirement and this custom page inherits Microsoft.SharePoint.ApplicationPages.UploadExPage class. Refer the below link for more info: byteloom.marek-mierzwa.com/2012/01/20/…
    – dasarp
    Commented Aug 28, 2017 at 8:43
  • Thank you for your response @SaMolPP, but I cannot replicate this functionality as I have no option available to build the WSP.
    – Pradip R.
    Commented Aug 28, 2017 at 8:53
  • 1
    I have found one more useful link : But here also, C# is required to build httphandler. spbreed.wordpress.com/2013/10/10/…
    – samolpp2
    Commented Aug 28, 2017 at 8:56

2 Answers 2

3

The REST api does not provide for uploading multiple files in a single call. You would need to write your javascript to loop thru the collection of files and issue individual calls. Refer this one : Upload multiple files with REST API in document library with custom properties

3
  • Appreciate you answer but I am not trying to achieve this with REST, I am just trying to use out of the box page which is _layouts/15/Upload.aspx?List={59186CEC-D2B9-47C3-A2C3-EBCA2C4E2F1A}&RootFolder=&IsDlg=1 so the file upload will be handled by out of the page itself. Only thing is when I use above code, multiple file upload doesn't work.
    – Pradip R.
    Commented Aug 28, 2017 at 9:34
  • 1
    Multiple upload not allowed with out of the box functionality, you can try with this, /_layouts/15/Upload.aspx?List={' + data.d.Id + '}&MutipleUpload=1&RootFolder=' will give an error "Multiple upload not allowed"
    – Anuja
    Commented Aug 28, 2017 at 11:11
  • If I try this approach, it allows only one file to select, it is not giving any error like you mentioned. Does that mean what I am trying to achieve is not possible?
    – Pradip R.
    Commented Aug 28, 2017 at 13:17
0

Unfortunately I don't think this is possible when calling the upload.aspx from a custom form. You can set the upload.aspx page to allow the selection of multiple files using this:

$("[id$='InputFile']").attr("multiple", "true");

...and it looks like it will work. It allows you to select more than one file and it displays everything you selected, but it only uploads the first file.

I compared the onclick event of the OK button when called from the library and when called from my custom script and it's different. I tried to mimic the original and it runs without error, but it only uploads the first document.

There must be something under the code that ignores the additional ones but I haven't been able to find it yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.