0

There is this script which I got from Here.

I just have to add ScriptEditor WebPart to my library page and apply this code Not sure how to go with this. I know how to go edit and add a Webpart and paste the code there but I would have liked some step by step help

I really want to make this work.

(function (_window) {
        var maxTimeForReplaceUploadProgressFunc = 10000;
        function replaceUploadProgressFunc() {
            if (typeof _window.UploadProgressFunc != 'undefined') {
                _window.Base_UploadProgressFunc = _window.UploadProgressFunc;
                _window.UploadProgressFunc = Custom_UploadProgressFunc;
                console.log('replaced dialog');
            } else if (maxTimeForReplaceUploadProgressFunc > 0) {
                maxTimeForReplaceUploadProgressFunc -= 100;
                setTimeout(replaceUploadProgressFunc, 100);
            }
        }
        setTimeout(replaceUploadProgressFunc, 100);


        function Custom_UploadProgressFunc(percentDone, timeElapsed, state) {
            _window.Base_UploadProgressFunc(percentDone, timeElapsed, state);
            var messageType = ProgressMessage.EMPTY;
            switch (state.status) {
                case 1:
                    messageType = ProgressMessage.VALIDATION;
                    break;
                case 3:
                    messageType = ProgressMessage.UPLOADING;
                    break;
                case 4:
                    messageType = ProgressMessage.UPLOADED;
                    OpenEditFormForLastItem(state);
                    break;
                case 5:
                    messageType = ProgressMessage.CANCELLED;
                    break;
            }

            function OpenEditFormForLastItem(state) {
                var caml = '';
                caml += "<View>";
                caml += "<Query>";
                caml += "<Where>";

                if (state.files.length > 1) {
                    caml += "<In>";
                    caml += "<FieldRef Name='FileLeafRef'/>";
                    caml += "<Values>";
                } else {
                    caml += "<Eq>";
                    caml += "<FieldRef Name='FileLeafRef'/>";
                }

                state.files.forEach(function (file) {
                    //only succesfull uploaded files that arent overwrites
                    console.log(file);
                    if (file.status === 5 /*&& !file.overwrite*/) {
                        caml += "<Value Type='File'>" + file.fileName + "</Value>";
                    }
                }, this);

                if (state.files.length > 1) {
                    caml += "</Values>";
                    caml += "</In>";
                } else {
                    caml += "</Eq>";
                }

                caml += "</Where>";
                caml += "<OrderBy><FieldRef Name='ID' Ascending='True' /></OrderBy>";
                caml += "</Query>";
                caml += "<ViewFields><FieldRef Name='ID' /></ViewFields>";
                caml += "<RowLimit>500</RowLimit>";
                caml += "</View>";
                console.log(caml);

                var cntxt = SP.ClientContext.get_current();
                var web = cntxt.get_web();
                var list = web.get_lists().getByTitle(window.ctx.ListTitle);
                var query = new SP.CamlQuery();
                query.set_viewXml(caml);
                var items = list.getItems(query);
                cntxt.load(list, 'DefaultEditFormUrl');
                cntxt.load(items);
                cntxt.executeQueryAsync(function () {
                    var listEnumerator = items.getEnumerator();
                    function openEditForItem() {
                        if (listEnumerator.moveNext()) {
                            var item = listEnumerator.get_current();
                            var id = item.get_id();

                            var options = SP.UI.$create_DialogOptions();
                            options.title = "Add File Metadata";
                            options.url = list.get_defaultEditFormUrl() + '?ID=' + id;
                            options.autoSize = true;
                            options.dialogReturnValueCallback = openEditForItem;
                            SP.UI.ModalDialog.showModalDialog(options);
                        } else {
                            location.reload();
                        }
                    }
                    openEditForItem();
                }, function (error, args) {
                        console.log("failed to get new uploaded items");
                        console.log(error);
                        console.log(args);
                    });
            }
        }
    })(window);

  • There are several things. You need to wrap with script tags. <script type=”text/javascript”></script>. And the CAML query are not threshold-safe. – Christoffer May 17 at 21:20
  • I'm sorry I just havent worked with scripts and sharepoint before. could you give me more help regarding this – Ed1995 May 18 at 9:20
2

If you want to open edit form page after Drag&Drop in document library list view, we can add the code below to script editor web part in the list view page.

<script type="text/javascript">
(function (_window) {
    var maxTimeForReplaceUploadProgressFunc = 10000;
    function replaceUploadProgressFunc() {
        if (typeof _window.UploadProgressFunc != 'undefined') {
            _window.Base_UploadProgressFunc = _window.UploadProgressFunc;
            _window.UploadProgressFunc = Custom_UploadProgressFunc;
            console.log('replaced dialog');
        } else if (maxTimeForReplaceUploadProgressFunc > 0) {
            maxTimeForReplaceUploadProgressFunc -= 100;
            setTimeout(replaceUploadProgressFunc, 100);
        }
    }
    setTimeout(replaceUploadProgressFunc, 100);


    function Custom_UploadProgressFunc(percentDone, timeElapsed, state) {
        _window.Base_UploadProgressFunc(percentDone, timeElapsed, state);
        var messageType = ProgressMessage.EMPTY;
        switch (state.status) {
            case 1:
                messageType = ProgressMessage.VALIDATION;
                break;
            case 3:
                messageType = ProgressMessage.UPLOADING;
                break;
            case 4:
                messageType = ProgressMessage.UPLOADED;
                OpenEditFormForLastItem(state);
                break;
            case 5:
                messageType = ProgressMessage.CANCELLED;
                break;
        }

        function OpenEditFormForLastItem(state) {
            var caml = '';
            caml += "<View>";
            caml += "<Query>";
            caml += "<Where>";

            if (state.files.length > 1) {
                caml += "<In>";
                caml += "<FieldRef Name='FileLeafRef'/>";
                caml += "<Values>";
            } else {
                caml += "<Eq>";
                caml += "<FieldRef Name='FileLeafRef'/>";
            }

            state.files.forEach(function (file) {
                //only succesfull uploaded files that arent overwrites
                console.log(file);
                if (file.status === 5 /*&& !file.overwrite*/) {
                    caml += "<Value Type='File'>" + file.fileName + "</Value>";
                }
            }, this);

            if (state.files.length > 1) {
                caml += "</Values>";
                caml += "</In>";
            } else {
                caml += "</Eq>";
            }

            caml += "</Where>";
            caml += "<OrderBy><FieldRef Name='ID' Ascending='True' /></OrderBy>";
            caml += "</Query>";
            caml += "<ViewFields><FieldRef Name='ID' /></ViewFields>";
            caml += "<RowLimit>500</RowLimit>";
            caml += "</View>";
            console.log(caml);

            var cntxt = SP.ClientContext.get_current();
            var web = cntxt.get_web();
            var list = web.get_lists().getByTitle(window.ctx.ListTitle);
            var query = new SP.CamlQuery();
            query.set_viewXml(caml);
            var items = list.getItems(query);
            cntxt.load(list, 'DefaultEditFormUrl');
            cntxt.load(items);
            cntxt.executeQueryAsync(function () {
                var listEnumerator = items.getEnumerator();
                function openEditForItem() {
                    if (listEnumerator.moveNext()) {
                        var item = listEnumerator.get_current();
                        var id = item.get_id();

                        var options = SP.UI.$create_DialogOptions();
                        options.title = "Add File Metadata";
                        options.url = list.get_defaultEditFormUrl() + '?ID=' + id;
                        options.autoSize = true;
                        options.dialogReturnValueCallback = openEditForItem;
                        SP.UI.ModalDialog.showModalDialog(options);
                    } else {
                        location.reload();
                    }
                }
                openEditForItem();
            }, function (error, args) {
                    console.log("failed to get new uploaded items");
                    console.log(error);
                    console.log(args);
                });
        }
    }
})(window);
</script>
  • thank you it works. However this pop up window from Internet Explorer shows up. it says ''To display the webpage again, Internet Explorer needs to resend the information you've previously submitted. If you were making a purchase, you should click the Cancel to avoid a duplicate transaction. ... Otherwise, click Retry to display the webpage again'' Can you help me with this ? it might be something involving the code – Ed1995 May 20 at 13:08
  • Replace the code location.reload() with location.href = location.href – LZ_MSFT May 21 at 7:33
  • Is it possible to modify the code so when I upload multiple files, the dialog box to only get shown once . Please help me @LZ_MSFT – Ed1995 May 28 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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