Use DocumentService when working with document records.
Whenever a Service operation requires DocumentService, set up DocumentService using the following code:
DocumentService documentService = platform.getDocumentService(); |
The following sample creates a document. During document creation, you can add the document to a folder, create a shortcut for the folder, and check in the file, among other things.
DocumentService documentService = platform.getDocumentService();
// Get attachment folder of a record Folder attachmentFolder = documentService.getAttachmentFolderForDocumentOwner(getRecord());
// Create a folder under the attachment folder Folder folder = attachmentFolder.addFolder("New Folder");
// Create a file directly under the attachment folder FileContentType contentType = platform.getLookupItemService().getFileContentTypeByFileExtension("txt"); byte[] fileContent = new byte[0]; File file = attachmentFolder.addFile("New File", fileContent, contentType);
// Create a hyperlink Hyperlink hyperlink = folder.addHyperlink("Mitratech", "http://www.mitratech.com");
// Create a shortcut to a folder Shortcut shortcutToFolder = folder.addShortcut(folder);
// Create a shortcut to a file Shortcut shortcutToFile = folder.addShortcut(file);
//Checkout file documentService.checkOut(file);
//Checkin file documentService.checkIn(file, fileContent, "check in comment"); |
The following sample returns a document using its folder path.
// Returns a document from the repository using its folder path Document document = documentService.findDocumentByPath("path"); |
The following sample returns the root document folder.
// Returns the root document folder Folder rootFolder = documentService.getRootFolder(); |
The following sample copies a document to a new folder.
// Returns an exact duplicate of a given entity object Document duplicateDocument = documentService.copyEntity(originalDocument);
// Copies a document to a new parent folder documentService.copyDocument(document, parentFolder); |
The following sample moves a document to a new folder.
// Changes the location of a file documentService.moveDocument(document, parentFolder); |
The following sample checks out a file and locks it so that other users cannot check it out.
// Checks out and locks a file for a single user to update documentService.checkOut(file); |
The following sample reverses a document checkout. The document remains unchanged and has no version number.
// Reverses the checkout of a file. documentService.undoCheckOut(file); |
The following sample checks in a document by converting the existing file to the new file version and replacing the file contents the byte array of the new contents.
// Checks in a file. documentService.checkIn(file, data, "versionText"); |
The following sample reverts a document to a previous version by converting the existing file to a different file version and replacing the file contents with the byte array of the new contents.
// Revert a document to a prior version FileVersion version = documentService.getFileVersions(file).get(0); documentService.revertTo(version); |