Edit Form Popup – Drag And Drop

By default, the edit form does not pop up when you drag and drop documents into a library. This piece of JavaScript will force an edit form to pop up for as many documents you drag and drop. For example, if you drag 5 documents at the same time, 5 individual edit forms will pop up in succession.

1. Make sure that the Metadata Navigation and Filtering site feature is enabled.

2. Navigate to and edit the page you are adding this to.

3. Insert a Script Editor web part and embed the following code (no changes need to be made, just copy/paste):


<script unselectable="on">

(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>

4. Click OK and save the page.

5. Confirm that the edit form pops up when you drag and drop documents.

Library Paging – First Page Button

Adding a “First Page” button is a simple work around for getting back to the first page when paging through documents. This is especially handy if you use document sets.

  1. Edit the page and add a Script Editor web part.
  2. Click EDIT SNIPPET.embed
  3. Embed the following code:

<!DOCTYPE html>
<html>
<body>

<input type="button" value="First Page" onclick="location.href=''"/>

</body>
</html>

I moved the Script Editor web part down below the library web part and changed the Direction to Right to Left.

scripteditor

Final product:

docsetbutton.PNG

Next page:

docsetbutton1.PNG

Show Files & Library Tabs

Problem: 

You want the Files and Library tabs to be visible when you browse to a page with multiple web parts.

Solution:

JavaScript! Add this to a Script Editor web part on the same page.


<script>

        setTimeout(function() {

        var elem = document.getElementById("MSOZoneCell_WebPartWPQ2");

           if(elem != null) {

                var dummyevent = new Array();

                dummyevent["target"] = elem;

                dummyevent["srcElement"] = elem;

                WpClick(dummyevent);

                _ribbonStartInit("Ribbon.Browse", true)

            }

        }, 2000);

        </script>

You may need to change “MSOZoneCell_WebPartWPQ2” to the web part ID of the web part you want to focus on. You can find this by opening dev tools.

f12-1

Disable Drag And Drop

This handy piece of JavaScript will disable the drag and drop function for all library web parts on a single page. Other upload options will still work.

Just paste this into a Script Editor web part:


<script>

ExecuteOrDelayUntilScriptLoaded(function() {

    g_uploadType = DragDropMode.NOTSUPPORTED;

    SPDragDropManager.DragDropMode = DragDropMode.NOTSUPPORTED;

}, "DragDrop.js");

</script>