Adding content to a site uses the same API calls as are used to create folders and upload files elsewhere in the Repository. See Creating a Folder and Uploading a File for more information.
For more information about this ReST API endpoint, see Add Content to a Site.
For a description of the common parameters, such as fields, see Common Parameters.
The tricky bit is to figure out how to add content to the so called “Document Library” of a site. We can figure out the Node ID for the Document Library by using the listSiteContainers method of the SitesApi:
import org.alfresco.core.handler.NodesApi;
import org.alfresco.core.handler.SitesApi;
import org.alfresco.core.model.*;
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.List;
@Component
public class AddSiteContentCmd {
static final Logger LOGGER = LoggerFactory.getLogger(AddSiteContentCmd.class);
Integer skipCount = 0;
Integer maxItems = 100;
private List<String> fields = null;
private List<String> include = null;
private Boolean autoRename = true;
private Boolean majorVersion = true;
private Boolean versioningEnabled = true;
@Autowired
SitesApi sitesApi;
@Autowired
NodesApi nodesApi;
public void execute(String siteId) throws IOException {
// First get the Node ID for the Document Library
String docLibNodeId = null;
SiteContainerPaging siteContainerPaging = sitesApi.listSiteContainers(siteId, skipCount, maxItems, fields).getBody();
LOGGER.info("Listing site containers [{}]: ", siteId);
for (SiteContainerEntry siteContainerEntry: siteContainerPaging.getList().getEntries()) {
SiteContainer siteContainer = siteContainerEntry.getEntry();
LOGGER.info(" Site container: {}", siteContainer);
if (siteContainer.getFolderId().equalsIgnoreCase("DocumentLibrary")) {
docLibNodeId = siteContainer.getId();
}
}
if (docLibNodeId != null) {
// Create a folder in the document library
createFolder(docLibNodeId, "White papers");
} else {
LOGGER.info("Document library not found in site {}", siteId);
}
}
/**
* Make the remote call to create a folder in the repository, if it does not exist.
*
* @param parentFolderId the node ID for the site container
* @param folderName the name of the folder
* @return a node object for the newly created node, contains the ID,
* such as e859588c-ae81-4c5e-a3b6-4c6109b6c905
*/
private Node createFolder(String parentFolderId,
String folderName) {
NodeBodyCreate nodeBodyCreate = new NodeBodyCreate();
nodeBodyCreate.setName(folderName);
nodeBodyCreate.setNodeType("cm:folder");
Node folderNode = nodesApi.createNode(parentFolderId, nodeBodyCreate, autoRename, majorVersion, versioningEnabled,
include, fields).getBody().getEntry();
LOGGER.info("Created new folder in DocLib: {}", folderNode);
return folderNode;
}
}
Executing this code will create a folder in the passed in site’s document library:
% java -jar target/rest-api-0.0.1-SNAPSHOT.jar add-site-content test
2021-05-05 10:43:34.208 INFO 16095 --- [ main] o.a.tutorial.restapi.RestApiApplication : Started RestApiApplication in 3.019 seconds (JVM running for 3.46)
2021-05-05 10:43:34.210 INFO 16095 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[0]: add-site-content
2021-05-05 10:43:34.211 INFO 16095 --- [ main] o.a.tutorial.restapi.RestApiApplication : args[1]: test
2021-05-05 10:43:34.390 INFO 16095 --- [ main] o.a.tutorial.restapi.AddSiteContentCmd : Listing site containers [test]:
2021-05-05 10:43:34.391 INFO 16095 --- [ main] o.a.tutorial.restapi.AddSiteContentCmd : Site container: class SiteContainer {
id: 605e085c-92ae-4a53-b902-99c7d215f475
folderId: documentLibrary
}
2021-05-05 10:43:34.833 INFO 16095 --- [ main] o.a.tutorial.restapi.AddSiteContentCmd : Created new folder in DocLib: class Node {
id: 6e157336-068a-4384-bc29-e4e1ca09cc6c
name: White papers
nodeType: cm:folder
isFolder: true
isFile: false
isLocked: false
modifiedAt: 2021-05-05T09:43:34.660Z
modifiedByUser: class UserInfo {
displayName: Administrator
id: admin
}
createdAt: 2021-05-05T09:43:34.660Z
createdByUser: class UserInfo {
displayName: Administrator
id: admin
}
parentId: 605e085c-92ae-4a53-b902-99c7d215f475
isLink: null
isFavorite: null
content: null
aspectNames: [cm:auditable]
properties: null
allowableOperations: null
path: null
permissions: null
definition: null
}