Question

Using Files in script Element in Release 7.17.2

Hi,

 

I would appreciate if someone could assist with the usage of files in the new release.

I need to create to printables in a process and then use them inside a script task.

I basically need to implement the following:

 

var OrderFiles1 = Get>("ReportFileProcessingUserTask1.ReportFiles");

var OrderFiles2 = Get>("ReportFileProcessingUserTask2.ReportFiles");

File File1;

File File2;

foreach(var OrderFile1 in OrderFiles1) {

    OrderFile1.TryGetValue("File", out File1);

}

foreach(var OrderFile2 in OrderFiles2) {

    OrderFile2.TryGetValue("File", out File2);

}

return true;

 

I attached a screenshot of the process and the errors I'm getting.

 

Thanks,

Raz

Like 0

Like

4 comments

Hi Raz,

 

Starting from 7.17.2 Creatio core gives a number of classes and interfaces to work with files:

 

Terrasoft.File.Abstractions - interfaces and abstract classes that describe the file processing logic in Creatio

Terrasoft.File - concrete abstractions implementations that are used in Creatio

 

File location in the file storage is declared using file locator - the object that implements the Terrasoft.File.Abstractions.IFileLocator interface. File locator must contain the unique file identificator - RecordId.

 

For example here is the description of how to create a new file:

//Create new Guid for a file
Guid fileId = Guid.NewGuid();
//Create a file locator
var fileLocator= new EntityFileLocator("ActivityFile", fileId);
// Receive IFile object for a new file.
IFile file = UserConnection.CreateFile(fileLocator);
// There is neither metadata nor content of a new file
// Create a new file name
file.Name = "New file";
// Set the attribute value for a file: connect a file to activity with activityId ID. 
// This is also metadata
file.SetAttribute("ActivityId", activityId);
//Metadata for a file should be saved BEFORE the content for a file is saved
file.Save();
// byte[] content — this is content
var content = new byte[] {0x12, 0x34, 0x56};
using (var stream = new MemoryStream(content)) {
    // Save content to the database
    // FileWriteOptions.SinglePart means that the content is transferred in one single batch
    file.Write(stream, FileWriteOptions.SinglePart); 
}

Here is an example of how to get the content of a file:

var content = new byte[]();
// Get a file by its locator
var fileLocator= new EntityFileLocator("ActivityFile", recordId);
IFile file = UserConnection.GetFile(fileLocator);
// Read all the content to the content byte-array. Don't forget to free the stream object with the help of using!
using (Stream stream = file.Read()) {
    content = stream.ReadAllBytes();
}

And here is an example of how to copy a file, move it to a new place and then delete an original file:

var content = new byte[]();
// Get a file by its locator
var fileLocator= new EntityFileLocator("ActivityFile", fileId);
IFile file = UserConnection.GetFile(fileLocator);
// Read all the content to the content byte-array. Don't forget to free the stream object with the help of using!
using (Stream stream = file.Read()) {
   content = stream.ReadAllBytes();
}
 
// Copying a file
 
// Create a new IFile for copying the current file.
Guid copyFileId = Guid.NewGuid();
var copyFileLocator = new EntityFileLocator("ActivityFile", copyFileId);
IFile copyFile = UserConnection.CreateFile(copyFileLocator);
copyFile.Name = file.Name + " - Copy";
copyFile.Save();
 
// Copying the content of an original file to a new file
copyFile.Write(new MemoryStream(content), FileWriteOptions.SinglePart);
 
//Alternative way to copy a file
file.Copy(copyFile);
 
// Moving a file
 
// Create a new file 
Guid moveFileId = Guid.NewGuid();
var moveFileLocator = new EntityFileLocator("ContactFile", moveFileId);
IFile moveFile = UserConnection.CreateFile(moveFileLocator);
moveFile.Save();
 
// Move it to a new place
file.Move(moveFile);
 
// Remove an original file
file.Delete();

You can also take a look at the basic implementation in the FileLoader module in configurations and you need to redesign the process in the way it uses file locator.

 

Best regards,

Oscar

Oscar Dylan,

Hi Oscar,  WOW :)

 

Thank you for the detailed answer.

So, if I want to use the file I created in the File element, in the script task, Do I need to read the content and then copy it to a new file? Isn't a way to just read like a collection of records?

 

Let's say that I solved that in one of the ways I mentioned above. Now I need to pass the file to an external function.

The challenge is that it doesn't receive a stream data, but the path to the file and a stream file.

 

For example: foo("path",StreamFile);

 

first of all, how can I give it the path to the file? my creatio is a cloud base installation and second does the "IFile" object you mentioned is like a FileStream?

 

Looking forward to having your answers.

 

PS:

I'm basically trying to use the Aspose Word Cloud library to merge 2 pdf printables into 1. And the merging function is the "foo" function I mentioned above.

 

Thanks,

Raz

 

 

Raz Guille Rosman,

Hello,

 

As for the path to the file that is an attachment in the system, you can get it using the following link:

 

https://<instance>/0/rest/FileService/GetFile/

+

UID value from SysSchema table for the detail from which you try to get the file from: OrderFile, InvoiceFile, AccountFile etc

+

ID value from the table which represents this detail.

 

For example I have a record in OrderFile table (order attachment) with an ID: 2AC7AA00-2E61-4AEC-8BD0-621269731DAA. To complete the link I execute:

SELECT UID FROM SysSchema WHERE Name = 'OrderFile'

 

and get a value of D75D815B-0B2E-4E33-973A-ED9A43601B44.

 

So my link is: https://<instance>/0/rest/FileService/GetFile/D75D815B-0B2E-4E33-973A-ED9A43601B44/2AC7AA00-2E61-4AEC-8BD0-621269731DAA

 

You can probably try to call the created file to your function using the formed link.

 

The IFile itself is not like a FileStream so probably you won't be able to use it in a custom method.  Check the GetFile method of the FileService, maybe it contains examples that could help in achieving your task.

 

Best regards,

Oscar

Thank you very much :)

I will try that and let you know if I'll have further questions.

Show all comments