Solved

Move a document from Library to library without overwriting the version history with Nintex Workflow


I have a scenario where we store our documents in a 'Live' library for all to view. when a document needs updating the document is copied to an 'Update' library to be worked on by the document owner, leaving the original one in tack and still viewable in 'Live'. Once the document in 'Update' has been amended/changed and gone through an approval process, it needs to be moved back to the 'Live' library where the version history of the updated document is appended / added to the original document. As if you where uploading a new version to the 'Live' library. With Nintex workflow I can copy a document from one Library to another and using the Web Request feature I can Move a document and preserve the version history using Vadim Tabakman configuration. However when you move a document to a library that already has that same document, it overwrites it and the version history losing the original history. Is there any way to adjust Vadim code to not overwrite but to append the version history; in effect upload a document from one library to another so to add to the version history. Or is there another feature in Nintex that can achieve this. Any help will be very welcome.

icon

Best answer by S-man 29 October 2021, 10:11

View original

12 replies

Userlevel 3
Badge +12

Hi, Have you enabled version history on "Live" document library?


 

Yes. both libraries are set to major and minor versioning turned on.

I know when you 'Copy' you create a new document, 'Move' will take the version history and overwrite any pre existing version. I suppose I am looking to 'Upload' a document which will add on to a pre existing version to build on the version history. The Question is can Nintex workflow automate an upload from one library to another with losing any pre existing version history??? 

Badge +12

@S-man or you can use Sharegate

Unfortunately no bells or whistle. I have to achieve this with standard SharePoint 2016 and Nintex. There must be away for Nintex to perform an 'Upload' and Add as a new version to existing files. This has been achieve through SPService/JavaScript code. Is there away to apply code to a Nintex Workflow to create a custom automation feature? 

Userlevel 3
Badge +12

Have you tried using SharePoint Upload File action?

Unfortunately I don't have that feature on my Nintex. I am working with SharePoint 2016 on premise and central admin is controlled by a 3rd party, so unable to get add on.

Userlevel 3
Badge +12

Cann't the admin team to make it  "allow" that action?

I have asked the question. Would they need to connect to the Nintex external platform in order to get extra features? or is it already built in and just requires a tick in the box etc.
Userlevel 3
Badge +12

Yes, it requires to enable external platform features.

I have made a request to get the feature. whether it happens depend on business policy etc. If there are any forms of work arounds you could suggest in the mean time; I would be most grateful.

I found a way to upload a document from a library to another library and update the version history. I have managed to code the function into to the form using SPservices. So in order to perform the actions I created a separate button that ran the following javascript code to call SPservices functions on the form instead of the workflow. However I must add that the code only works on "http://" URLs and not "https://" URL.


 


//The dothis() is the function that start the code running when a button is click on the form.


function dothis()    
{
    var fileName = NWF$('#' + varFileNameID).val();   
    thesourcefilename = fileName; 
    
    if (fileName === '' )
    {
        alert('No File Selected');
    }
  else
    {
        var thefiletocheckout = "/URL of Live Library/" + thesourcefilename;
   
        checkoutthefile(thefiletocheckout);
    }
 
}


//The checkoutthefile() function find the selected document in the live library and checks it out in readiness.
  var coFile;
 
 function checkoutthefile(thefile)
{
         //Get client context and web
         var clientContext_co = new SP.ClientContext();
         var coWeb = clientContext_co.get_web();
         //Get List and File object
         var coList = coWeb.get_lists().getByTitle('Live Library Name');
         coFile=coWeb.getFileByServerRelativeUrl(thefile);
          //Publish the file and execute the batch
         coFile.checkOut();
         clientContext_co.load(coFile);
         clientContext_co.executeQueryAsync(
            function()
            {
                var majorVersion =coFile.get_majorVersion();
                   
                transferFile();            }
            ,
            function(sender,args) {
                alert('Checkout failed - '+args.get_message());
                LogError('Publish - Checkout failed - ',thefile+':'+args.get_message())//               
                console.log('Request failed - '+args.get_message());
            }
        );
}      
 


//The transferFile() function is the main part that grabs the document from the change library and uploads it to the live library.
function transferFile()
{
      var fileName = NWF$('#' + varFileNameID).val();   
      var thesourcefilename = fileName; 
     
      var _destinationlibUrl = "/Local URL of Live Library/" + thesourcefilename;
      var _sourcelibUrl = "/Local URL of the Update Library/" + thesourcefilename;
 
     var myStream; 
     var itemFields = [];
            NWF$().SPServices({
            operation: "GetItem",
            async: false,
            Url: "https://Your URL******" +  _sourcelibUrl,
            completefunc: function(xData, Status) {
            myStream = NWF$(xData.responseXML).find("Stream").text(); //MSDN: base-64 representation of the retrieved document's binary data.     
            NWF$(xData.responseXML).find("FieldInformation").each(function(){
             itemFields.push(NWF$(this).get(0).xml);
             });
        } // end completefunc
    }); // end getItem
           
                 NWF$().SPServices({
                operation: "CopyIntoItems",
                async: false,
                SourceUrl: "https:// Your URL******" + _sourcelibUrl,
                DestinationUrls: ["Your URL******" + _destinationlibUrl],
                Stream: myStream,
                Fields: itemFields,
                completefunc: function(xData, Status) {
                 alert("Status=" + Status + " XML=" + xData.responseXML.xml); 
 
                    findFileInLive(fileName);                          
                }
            });
 
}



//The findFileInLive() checks that the document is in the live ready to update the columns with supporting information
 function findFileInLive(filename)
 {


        var subquery = "<View><Query></Query></View>";
        var ctxs = SP.ClientContext.get_current();
        var web = ctxs.get_web();       
       var aOrgsList = web.get_lists().getByTitle("Live Library Name");
        //alert(aOrgsList.Title);
        var aQuery = new SP.CamlQuery();
        aQuery.set_viewXml(subquery);
        var aOrgsItems = aOrgsList.getItems(baQuery);
        ctxs.load(aOrgsItems, "Include(ID, Title, LinkFilename)");       
        ctxs.executeQueryAsync(function () {
 
            var findresult = aOrgsItems.get_count();
             // alert(findresult);
            var aOrgsItemEnumerator = aOrgsItems.getEnumerator();           
            while (aOrgsItemEnumerator.moveNext()) {                
 
                var thename = aOrgsItemEnumerator.get_current().get_item("LinkFilename");
               
                if (thename === thesourcefilename)
                {
                    var theid =  aOrgsItemEnumerator.get_current().get_item("ID");   
                  
                checkinthefile(thefile);
                    break;
                }
             }       
      });   
}    
 


//Once the document has uploaded the file is check in with checkinthefile() function
var ciFile;
 
 function checkinthefile(thefile)
 {
      var checkinfile  = "/ Local URL of Live Library /" + thefile;
       
         //Get client context and web
         var clientContext_ci = new SP.ClientContext();
         var ciWeb = clientContext_ci.get_web();
         //Get List and File object
         var ciList = ciWeb.get_lists().getByTitle('Live Library Name');
         ciFile=ciWeb.getFileByServerRelativeUrl(checkinfile);
          //Publish the file and execute the batch
         ciFile.checkIn();
         clientContext_ci.load(ciFile);
         clientContext_ci.executeQueryAsync(
            function()
            {
                //Get major version
                var majorVersion =ciFile.get_majorVersion();
              
                publishFile(checkinfile);
            }
            ,
            function(sender,args) {
                alert('checkin failed - '+args.get_message());
                LogError('Publish - Checkin failed - ',thefile+':'+args.get_message())
 
                //console.log('Request failed - '+args.get_message());
            }
        );
 }
   


//The publishFile() function then published the file to the next major version. 
 var pFile;
 
function publishFile(thefile)
 {
         //Get client context and web
         var clientContext_p = new SP.ClientContext();
         var pWeb = clientContext_p.get_web();
         //Get List and File object
         var pList = pWeb.get_lists().getByTitle(''Live Library Name'');
         pFile=pWeb.getFileByServerRelativeUrl(thefile);
          //Publish the file and execute the batch
         pFile.publish();
         clientContext_p.load(pFile);
         clientContext_p.executeQueryAsync(
            function()
            {
                //Get major version
                //alert('file published'+thefile);
                var majorVersion =pFile.get_majorVersion();
               
                // remove the update document
                var thefullsource = "/ Local URL of the Update Library /"+ thesourcefilename;
                 deleteUpdateFile(thefullsource);
          }
            ,
            function(sender,args) {
                alert('Publish failed - '+args.get_message());
                LogError('Publish - Publish failed - ',thefile+':'+args.get_message())
 
                //console.log('Request failed - '+args.get_message());
            }
        );
 }   

//the deleteUpdateFile() function would then delete the document from the change library. Note, for testing purposes we are not using this code. Instead it give an alert to say that the process is complete.
function deleteUpdateFile(thefullsource)
{
    alert("The file " + thefullsource + " has been moved. The process is complete.");
 


 


 


 


Hope all of that makes sense.


 

Reply