File management examples

Medium

Example. Get an instance of a class that implements the IFile interface.

/* Get the file factory. */
IFileFactory fileFactory = UserConnection.GetFileFactory();
/* Creating a file locator for a file identified by the recordId and stored in the "ActivityFile" DB table ("Attachments" for the "Activity" object). */
var fileLocator = new EntityFileLocator("ActivityFile", recordId);
/* Pass the created locator to the Get method of the factory. */
IFile file = fileFactory.Get(fileLocator);
/* This will return an instance of a class that implements the IFile interface. Use it to manupulate the file and its contents. */
/* Create a file locator for a file identified by the recordId and stored in the "ActivityFile" DB table ("Attachments" for the "Activity" object). */
var fileLocator = new EntityFileLocator("ActivityFile", recordId);
/* Pass the created locator to the GetFile extending method. */
IFile file = UserConnection.GetFile(fileLocator);
/* This will return  an instance of a class that implements the IFile interface. Use it to manipulate the file and its contents. */
/* Get the file factory. */
IFileFactory fileFactory = UserConnection.GetFileFactory(); 
/* Creating a unique identifier for the new file. */
Guid recordId = Guid.NewGuid();
/* Create a file locator for a new file identified by the recordId and stored in the "ActivityFile" DB table ("Attachments" for the "Activity" object). */
var fileLocator = new EntityFileLocator("ActivityFile", recordId);
/* Pass the created locator to the Create method of the factory. */
IFile file = fileFactory.Create(fileLocator);
/* This will return an instance of a class that implements the IFile interface. Use it to manipulate the file and its contents. */
/* Create a unique ID for the new file. */
Guid recordId = Guid.NewGuid(); 
/* Create a file locator for a new file identified by the recordId and stored in the "ActivityFile"  DB table ("Attachments" for the "Activity" object). */
var fileLocator = new EntityFileLocator("ActivityFile", recordId); 
/* Pass the created locator to the CreateFile extension method. */ 
IFile file = UserConnection.CreateFile(fileLocator); 
/* This will return an instance of a class that implements the IFile interface. Use it to manipulate the file and its contents. */

Example. Create a new file bound to an Activities(Activities) record.

Creating a new file
/* Create a unique ID for the new file. */
Guid fileId = Guid.NewGuid(); 
/* Create a file locator for the new file. */
var fileLocator= new EntityFileLocator("ActivityFile", fileId); 
/* Get an IFile object for the new file. */ 
IFile file = UserConnection.CreateFile(fileLocator); 
/* There is no file metadata or file content in the available file storages. Specify the file name in the file metadata. */
file.Name = "New file"; 
/* Set an attribute for the new file: bind the file to an Activity record with the activityId key. This is file metadata as well. */
file.SetAttribute("ActivityId", activityId); 
/* Save the file metadata Do this BEFORE saving the content. */
file.Save(); 
/* byte[] content – this is the file contents. */
var content = new byte[] {0x12, 0x34, 0x56}; 
using (var stream = new MemoryStream(content)) { 
    /* Save the content to the database. FileWriteOptions.SinglePart implies that the contents will be transferred without breaking into smaller parts. */
    file.Write(stream, FileWriteOptions.SinglePart); 
}

Example. Get the file content.

Read the file content
using Terrasoft.Common;

var content = new byte[]();
Guid recordId = Guid.NewGuid();

/* Create a file locator for the new file. */
var fileLocator = new EntityFileLocator("ActivityFile", recordId);
IFile file = UserConnection.GetFile(fileLocator);

/* Read the contents of the file in the content byte array. Remember to free the stream object by utilizing using! */
using (Stream stream = file.Read()) {
   content = stream.ReadToEnd();
}

Example. Copy a file, then move it to a new location and delete it.

Copy, move, and delete a file
var content = new byte[](); 
/* Get the file with the file locator. */
var fileLocator = new EntityFileLocator("ActivityFile", fileId); 
IFile file = UserConnection.GetFile(fileLocator); 
/* Read the contents of the file in the content byte array. Remember to free the stream object by utilizing using! */
using (Stream stream = file.Read()) { 
    content = stream.ReadToEnd(); 
} 
/* Copy the file. */ 
  
/* Create a new IFile to copy 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(); 
  
/* Copy the contents of the first file to the new file. */ 
copyFile.Write(new MemoryStream(content), FileWriteOptions.SinglePart); 
  
/* An alternate way to copy the file. */
file.Copy(copyFile); 
  
/* Move the file. */
  
/* Create a new file to copy the current file. */ 
Guid moveFileId = Guid.NewGuid();
var moveFileLocator = new EntityFileLocator("ContactFile", moveFileId); IFile moveFile = UserConnection.CreateFile(moveFileLocator); 
moveFile.Save(); 
  
/* Move to the new location. */ 
file.Move(moveFile); 
  
/* Delete the original file. */
file.Delete();