To create a link to a file, use the createNode method of the NodesApi and create a node of the type app:filelink.
For more information about this ReST API endpoint, see Create a Link to a File.
For a description of the common parameters, such as include, see Common Parameters.
import org.alfresco.core.handler.NodesApi; import org.alfresco.core.model.Node; import org.alfresco.core.model.NodeBodyCreate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.IOException; import java.util.*; @Component public class LinkFileCmd { static final Logger LOGGER = LoggerFactory.getLogger(LinkFileCmd.class); @Autowired NodesApi nodesApi; public void execute(String parentFolderNodeId, String linkToNodeId) throws IOException { Map<String, String> linkProps = new HashMap<>(); linkProps.put("cm:destination", linkToNodeId); // Link points to this file node NodeBodyCreate nodeBodyCreate = new NodeBodyCreate(); nodeBodyCreate.setName("Link to a text file"); nodeBodyCreate.setNodeType("app:filelink"); // Out-of-the-box content model type for a file link nodeBodyCreate.setProperties(linkProps); Boolean autoRename = true; List<String> include = new ArrayList<>(); List<String> fields = null; Boolean majorVersion = true; Boolean versioningEnabled = true; // Include the isLink property in the response so we can see if a node is a link include.add("isLink"); Node fileLinkNode = nodesApi.createNode(parentFolderNodeId, nodeBodyCreate, autoRename, majorVersion, versioningEnabled, include, fields).getBody().getEntry(); LOGGER.info("File link: {}", fileLinkNode); } }
Executing this code would give the following result, passing in parent folder and node to link:
% java -jar target/rest-api-0.0.1-SNAPSHOT.jar link-file 7f041db0-fdb6-4185-b921-2fb9ed381480 48413f7a-066d-4e38-b2e6-c84ede635493 2021-05-04 13:27:43.981 INFO 29404 --- [ main] o.a.tutorial.restapi.RestApiApplication : Started RestApiApplication in 4.237 seconds (JVM running for 4.904) 2021-05-04 13:27:43.983 INFO 29404 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[0]: link-file 2021-05-04 13:27:43.985 INFO 29404 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[1]: 7f041db0-fdb6-4185-b921-2fb9ed381480 2021-05-04 13:27:43.985 INFO 29404 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[2]: 48413f7a-066d-4e38-b2e6-c84ede635493 2021-05-04 13:27:44.329 INFO 29404 --- [ main] o.alfresco.tutorial.restapi.LinkFileCmd : File link: class Node { id: c4ab808f-f42b-42a8-b308-d5d82df29830 name: Link to a text file nodeType: app:filelink isFolder: false isFile: true isLocked: false modifiedAt: 2021-05-04T12:27:44.166Z modifiedByUser: class UserInfo { displayName: Administrator id: admin } createdAt: 2021-05-04T12:27:44.166Z createdByUser: class UserInfo { displayName: Administrator id: admin } parentId: 7f041db0-fdb6-4185-b921-2fb9ed381480 isLink: true isFavorite: null content: null aspectNames: [cm:auditable] properties: {cm:destination=48413f7a-066d-4e38-b2e6-c84ede635493} allowableOperations: null path: null permissions: null definition: null }
Note that the include parameter has been populated with the isLink value, which means the response will contain a value for the isLink property.