Page 1 of 1

Dropped Document Function, duplicate Name

Posted: 20 Sep 2018
by TonyDuke
Good Afternoon,

I have an edit form with a Related Document Control and I am using the Dropped Document Functionality to add attributes to the document and rename the file, however I have come across one issue that I hope you can offer some advice on.

The Function

Code: Select all

// Add Parent ID to uploaded Document for retrieval
fd.updateDroppedDocuments($('.documentUploadRICssClass'), {
  Address_x0020_Name: '{CurrentItem}'
});

// Update Uploaded Document for Relevant fields
fd.updateDroppedDocuments($('.documentUploadRICssClass'), function(listItem) {
	debugger;
	$('.saveAndCloseBtnCssClass').hide();
	//get the values from the Form fields:
	var companyNameId = fd.field('Company_x0020_Name').value();
	var companyName = fd.field('Company_x0020_Name').control('data')['Title'];
	var addressName = fd.field('Title').value();
	var currentName = listItem.get_fieldValues().FileLeafRef;
    var newName = companyName + ' - ' + addressName + ' - ' + currentName;
	var documentCategory = fd.field('Document_x0020_Category').value();
	var newFileName = listItem.get_item('FileDirRef') + '/' + newName;
	//set the values on uploaded document:
	listItem.set_item('Company_x0020_Name', companyNameId);
	listItem.set_item('Title', currentName);
	listItem.set_item('Document_x0020_Category', documentCategory);
	listItem.update();
    listItem.get_file().moveTo(newFileName);
	setTimeout(showButton, 5000)
});

// Function to show Save Button after Timeout
function showButton () {
	$('.saveAndCloseBtnCssClass').show();
}
The function works perfectly, except if a user tries to upload another document with the same name initial Name.

Example - A user uploads a document called "Document 1.docx", the function runs and the document is renamed to "Company Name - Address Name - Document 1.docx"

The issue comes if another document named Document 1.docx is uploaded using the same form, the function runs and everything happens except the rename, however no error is generated.

What I would like to happen is an error message display to the user and the new document to then be removed from the document library.

Could I use a callback to test the rename and get the ID of the new Document?

Re: Dropped Document Function, duplicate Name

Posted: 24 Sep 2018
by AlexZver
Hi!

Sorry for the late response, it's a little bit tricky issue. Check this code:

Code: Select all

fd.updateDroppedDocuments($('.related-docs'), function(listItem) {
    var result = $.Deferred();

    $('.saveAndCloseBtnCssClass').hide();
    //get the values from the Form fields:
    var companyNameId = fd.field('Company_x0020_Name').value();
    var companyName = fd.field('Company_x0020_Name').control('data')['Title'];
    var addressName = fd.field('Title').value();
    var currentName = listItem.get_fieldValues().FileLeafRef;
    var newName = companyName + ' - ' + addressName + ' - ' + currentName;
    var documentCategory = fd.field('Document_x0020_Category').value();
    var newFileName = listItem.get_item('FileDirRef') + '/' + newName;
    //set the values on uploaded document:
    listItem.set_item('Company_x0020_Name', companyNameId);
    listItem.set_item('Title', currentName);
    listItem.set_item('Document_x0020_Category', documentCategory);

    listItem.update();
   
    getFileExists(newFileName,
        function(fileFound){
            console.log(fileFound);
            if (fileFound) {
                alert('File duplicate');
                listItem.get_file().deleteObject();
            }
            else {
                listItem.get_file().moveTo(newFileName);
            }
            result.resolve();
        },
        function(error){
            console.log(args.get_message());
        }
    );
    setTimeout(showButton, 5000);  
    return result.promise();
});


function getFileExists(fileUrl,complete,error)
{
    var ctx = SP.ClientContext.get_current();
    var file = ctx.get_web().getFileByServerRelativeUrl(fileUrl);
    ctx.load(file);
    ctx.executeQueryAsync(function() {
        complete(true);
    }, 
    function(sender, args) {
        if (args.get_errorTypeName() === "System.IO.FileNotFoundException") {
            complete(false);
        }
        else {
            error(args);
        }  
    });
}

Re: Dropped Document Function, duplicate Name

Posted: 26 Sep 2018
by TonyDuke
Hi Alex,

Thank you for that, worked perfectly.

Thanks