1

I am trying to enable the script below in a document library. I originally found this script here.

Basically whenever a user drag and drops a file it pulls up a prompt to enter metadata.

It is working as intended if it is utilized in the parent level of the document library. However, there are multiple folders within the document library.

When a user drag and drops inside of a folder I think part of the script runs (the page refreshes as intended), however the edit form never pops up to ask the user for metadata.

I need this form to pop up as it does at the parent level.

I would greatly appreciate help in trying to get this last puzzle piece solved.

Thanks!

Update 2: (I hope I'm stating this right). In case it matters, the folders that I am trying to upload files within are are a custom content type. As I reflect on it, I'm reasonably confident that the script is failing because the script does not understand the location of the folder. Unfortunately my Javascript skills are weak at best, so I'm not sure how to correct the issue....

UPDATE: More context, I can tell that the script is evaluating to this point at the bottom of the code:

     } else {
                        location.reload();
                    }

When I remove that reload text, it stops performing the page refresh. However, I still do not understand why the if statement does not evaluate correctly when inside of a folder.

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

Funny I was just using that code for some other issues today. I haven't tried to use it in a folder, but I think I know what the issue is. The call to OpenEditFormForLastItem() constructs a CAML query to find the information about the uploaded documents and creats something like this:

<View>
    <Query>
        <Where>
            <Eq>
                <FieldRef Name='FileLeafRef' />
                <Value Type='File'>file_name.doc</Value>
            </Eq>
        </Where>
        <OrderBy>
            <FieldRef Name='ID' Ascending='True' />
        </OrderBy>
    </Query>
    <ViewFields>
        <FieldRef Name='ID' />
        <FieldRef Name='FileDirRef' />
        <FieldRef Name='FileLeafRef' />
    </ViewFields>
    <RowLimit>500</RowLimit>
</View>

A CAML query like that only looks at the top level of the library and as a result is returning no entries. Because there are no entries the line if (listEnumerator.moveNext()) evaluates to false and it skips to the reload.

If you want the CAML to look further down in the tree you'll need to tell it to do that. Here is a thread about that. I think you need to change

caml += "<View>";

to the his:

caml += "<View Scope=\"RecursiveAll\">";

This will make it look through all the folders, but that seems like it might be the best solution.

You can also add additional information to your query to restrict it to a specific folder url. I'm following this thread here. I assume you already know or have the url for the folder you are uploading to. in that case you you change your <Where> to something like this

<Where>
   <And>
     <Eq>
       <FieldRef Name='FileLeafRef' />
       <Value Type='File'>file_name.doc</Value>
     </Eq>
     <Eq>
       <FieldRef Name='FileDirRef' />
       <Value Type='File'>folderUrl</Value>
     </Eq>
    </And>
 </Where>

If you're uploading to multiple folders at a time (not sure how you would be doing that with a drag and drop!) you would probably want to go with the RecursiveAll solution. Otherwise the FileDirRef one seems good

  • You nailed it. The caml += "<View Scope=\"RecursiveAll\">"; fixed it perfectly. Thank you! As tends to be the way of things, there is one new issue... After I save the file in the form, the form reloads and you have to click save again. Any thoughts? – atw26 Aug 14 at 12:58
  • You're welcome. Start another question about the form reloading so that we can keep it clear a bit. – Rothrock Aug 14 at 16:42
  • Sorry for not getting back to you. The reloading was a user error issue, not related to the script. All is working well. Thanks – atw26 Aug 16 at 12:17

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.