To update metadata for a node, such as a file or folder, use the NodesApi.updateNode method.
For more information about this ReST API endpoint, see Update Metadata for a Folder or 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.NodeBodyUpdate; import org.alfresco.core.model.NodeEntry; import org.alfresco.core.model.PermissionsBody; 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.HashMap; import java.util.List; import java.util.Map; @Component public class UpdateNodeMetadataCmd { static final Logger LOGGER = LoggerFactory.getLogger(UpdateNodeMetadataCmd.class); @Autowired NodesApi nodesApi; public void execute(String nodeId) throws IOException { Map<String, Object> properties = new HashMap<>(); properties.put("cm:title", "UPDATED title"); properties.put("cm:description", "UPDATED description"); Node node = updateNode(nodeId, "newname.txt", properties, null, null); } /** * Update a node (such as file/folder). * * @param nodeId the id of the node that we want to update metadata for. * @param newName a new name for the node (sets cm:name) * @param properties the properties we want to update and their new values * @param aspectNames a list of aspect names to set the node, not that it needs to include all aspects as it will overwrite * @param permissionsBody permissions to set on the node * @return updated Node object */ private Node updateNode(String nodeId, String newName, Map<String, Object> properties, List<String> aspectNames, PermissionsBody permissionsBody) { List<String> include = null; List<String> fields = null; NodeBodyUpdate nodeBodyUpdate = new NodeBodyUpdate(); nodeBodyUpdate.setName(newName); nodeBodyUpdate.setProperties(properties); nodeBodyUpdate.setAspectNames(aspectNames); nodeBodyUpdate.setPermissions(permissionsBody); NodeEntry result = nodesApi.updateNode(nodeId, nodeBodyUpdate, include, fields).getBody(); LOGGER.info("Updated node {}", result.getEntry()); return result.getEntry(); } }
With the updateNode call we can update properties, aspects, and permissions for a node. Note that when updating aspects you need to include the complete list of aspects that should be set on the node as this call overwrites. You can fetch existing aspects with the getNodeMetadata call (see Getting Folder/File Metadata). If you are adding an aspect that has properties, then you can just add the properties and the aspect will be added automatically for you.
Executing this code result in this for a text file example:
% java -jar target/rest-api-0.0.1-SNAPSHOT.jar update-metadata 0492460b-6269-4ca1-9668-0d934d2f3370 2021-04-29 16:27:42.303 INFO 20246 --- [ main] o.a.tutorial.restapi.RestApiApplication : Started RestApiApplication in 3.185 seconds (JVM running for 3.683) 2021-04-29 16:27:42.306 INFO 20246 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[0]: update-metadata 2021-04-29 16:27:42.308 INFO 20246 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[1]: 0492460b-6269-4ca1-9668-0d934d2f3370 2021-04-29 16:27:43.089 INFO 20246 --- [ main] o.a.t.restapi.UpdateNodeMetadataCmd : Updated node class Node { id: 0492460b-6269-4ca1-9668-0d934d2f3370 name: newname.txt nodeType: acme:document isFolder: false isFile: true isLocked: false modifiedAt: 2021-04-29T15:27:42.528Z modifiedByUser: class UserInfo { displayName: Administrator id: admin } createdAt: 2021-04-28T12:02:33.143Z createdByUser: class UserInfo { displayName: Administrator id: admin } parentId: 8fa4e27d-35aa-411d-8bbe-831b6ed0c445 isLink: null isFavorite: null content: class ContentInfo { mimeType: text/plain mimeTypeName: Plain Text sizeInBytes: 30 encoding: ISO-8859-1 } aspectNames: [rn:renditioned, cm:versionable, cm:titled, cm:auditable, acme:securityClassified, cm:author, cm:thumbnailModification] properties: {cm:title=UPDATED title, cm:versionType=MAJOR, acme:documentId=DOC-001, cm:versionLabel=3.0, acme:securityClassification=Company Confidential, cm:lastThumbnailModification=[doclib:1619613896873, pdf:1619701086215], cm:description=UPDATED description} allowableOperations: null path: null permissions: null definition: null }
We can see the updated name and properties in the returned node object.