'Is it possible to copy or move files from one record to another?' is a repeating
question from our partners and clients. This post will help you out...
Depending on the setup, you'll want to move or copy a file from record X to record
Y. We'll discuss both.
Copy
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
// Load the file you want to copy
Record originalRecord = new Record(app);
Guid recordIdToLoad = new Guid("1CB188B1-2BC9-4232-9392-A8314B9C99E7");
originalRecord.Load(recordIdToLoad);
Guid fileToCopyId = new Guid("61D0EFDD-FD30-46A7-9E18-FF5B617575E6");
File fileToCopy = originalRecord.Files[fileToCopyId];
// Load the target record
Record targetRecord = new Record(app);
Guid targetRecordId = new Guid("D2628343-4DD3-41BB-91D9-E48978497D3A");
targetRecord.Load(targetRecordId);
// Copy the file to your record
targetRecord.Files.AddCopy(fileToCopy);
// Save the record
targetRecord.Save();
|
What's important to know is that this action results in:
- a copy of the specified file, including its Fields and FileVersions
- the copied file being cataloged in 'Capture mode' on the new record
- copy of All Item links if a publication is copied
- no copy of Publication links if an item is copied
- no copy of original paths of the file
- no removal of the original file.
Move
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// Load the file you want to move
Record originalRecord = new Record(app);
Guid originalRecordId = new Guid("1CB188B1-2BC9-4232-9392-A8314B9C99E7");
originalRecord.Load(originalRecordId);
Guid fileToMoveId = new Guid("61D0EFDD-FD30-46A7-9E18-FF5B617575E6");
File fileToMove = originalRecord.Files[fileToMoveId];
// Get the target recordId
Guid targetRecordId = new Guid("D2628343-4DD3-41BB-91D9-E48978497D3A");
// Create an instance of the RecordHelper class and move the file
RecordHelper helper = new RecordHelper(app);
helper.TransferFile(fileToMove.Id, targetRecordId);
|
This results in:
- the file being moved from one record to another, including its Fields, FileVersions,
Publication-All Item Links and original paths
- the file being cataloged in the catalog mode equal to the original file (Capture
or Alias)
- the file being deleted on the original record.
Also important to know that there is no Record.Save() triggered while transfering
the file which means that no validation is activated on the records.