Copying and appending physical files

Another day, another challenge. I'm still working on our Office Connector integration and one of the features I want to present to my user community is the ability to use templates when creating new Document items. In order to do this a user can set the "from_template" property, after which I want to copy the physical file appended to that particular Document item and append it to the new Document item so the user won't have to do so manually. This saves our users a lot of work as new Document items often are created in bulk with the batchloader, literally by the hundreds. Instead of having to upload a file for each and every individual Document item this'll be all done programmatically so our engineers etc. can get straight to work on the various documents. Unfortunately however I can't seem to figure out how to copy the physical file on the server. Creating a File and relationship item is easy enough, but how do I copy the physical file the original File item points to?
  • From what I understood you want to retrive File Item from the document template and copy that File Item so you can attach it to the actual Document created from that template? If yes you can copy the file with "copyAsNew" method. The code could look something like that:
    var fileCopy = this.getInnovator().newItem("File", "copyAsNew");
    fileCopy.setAttribute("id", fileID); // ID of original File Item
    fileCopy.setAttribute("useInputProperties", "1");
    fileCopy.setProperty("filename", fileName); // fileName is the name of your new copied File
    fileCopy = fileCopy.apply();
  • Well, we're not just talking about the File item but also the physical file that resides in the vault. You don't want people working in the original file. This is the code I have so far: Item thisItem = this; Innovator inn = thisItem.getInnovator(); string templateId = thisItem.getProperty("from_template", ""); //If item is not based on a template skip this entire method if (templateId.Equals("")){ return thisItem; } //Get the relationship item pointing to the template file string aml = "<AML><Item action='get' type='Document File'><source_id>" + templateId + "</source_id></Item></AML>"; Item officeFileItem = inn.applyAML(aml); if(officeFileItem.isError()){ return thisItem; } //Get the file ID string fileId = officeFileItem.getProperty("related_id"); Item templateFile = inn.getItemById("File", fileId); //Prepare a name for the file string fileName = thisItem.getProperty("keyed_name"); Item myFile = inn.newItem("File","copyAsNew"); myFile.setAttribute("id", fileId); myFile.setAttribute("useInputProperties", "1"); myFile.setProperty("filename", fileName); myFile = myFile.apply(); //Above didn't result into anything, so I added the following code to manually relate the File item to the Document item Item myFileRel = inn.newItem("Document File","add"); myFileRel.setRelatedItem(myFile); thisItem.addRelationship(myFileRel); //Still no visible result return thisItem; I've managed to determine that the related file item is found so the problem specifically resides in the creation/copying of the new file and File item.
  • Hi remco.van.oosterhout, Is this a custom office connector you've built, or the Aras Office Connector? I believe the Aras Office Connector supports this functionality without custom code. Eli
    Eli Donahue Aras Labs Software Engineer
  • Hi Eli, We're using the Aras Office Connector. When creating a new Document item based on a template Document item in Word this functionality works, but when creating a new Document item in Aras and setting the from_template property this does not result in an appended file item on the new Document item. Could it be a rights issue on the server, or could I have configured something incorrectly? Remco
  • Happy to report the problem is solved. The method did already copy the file but failed to append it to the Document item, so all I had to do was create that relationship item. I also added a check at the beginning of the code so I don't accidentally overwrite any existing file. This method is now fired on the onAfterAdd and onAfterUpdate events. //Description: If the document item is based on a template, copy the base file and append it to the Document item Item thisItem = this; Innovator inn = thisItem.getInnovator(); string thisItemId = thisItem.getID(); string templateId = thisItem.getProperty("from_template", ""); //First check if the Document item doesn't already have an Office file attached string aml = "<AML><Item action='get' type='Document File'><source_id>" + thisItemId + "</source_id></Item></AML>"; Item alreadyHasAFile = inn.applyAML(aml); //If a relationship item was found, skip the rest of the method if (!alreadyHasAFile.isError()){ return thisItem; } //If item is not based on a template skip this entire method if (templateId.Equals("")){ return thisItem; } //Get the relationship item pointing to the template file aml = "<AML><Item action='get' type='Document File'><source_id>" + templateId + "</source_id></Item></AML>"; Item officeFileItem = inn.applyAML(aml); if(officeFileItem.isError()){ return thisItem; } //Get the file ID string fileId = officeFileItem.getProperty("related_id"); Item templateFile = inn.getItemById("File", fileId); //Recycle the old filename for the new one string fileName = templateFile.getProperty("filename"); // Item myFile = inn.newItem("File","copyAsNew"); myFile.setAttribute("id", fileId); myFile.setAttribute("useInputProperties", "1"); myFile.setProperty("filename", fileName); myFile = myFile.apply(); string newFileId = myFile.getID(); aml = "<AML><Item action='add' type='Document File' ><source_id>" + thisItemId + "</source_id><related_id>" + newFileId + "</related_id></Item></AML>"; Item newRelItem = inn.applyAML(aml); return thisItem;
  • Thanks for sharing your solution, Remco! Eli
    Eli Donahue Aras Labs Software Engineer