The IPortalContext has an IContentManagerFactory which can be used to create an IContentManager. To retrieve an IContentManager based on a connection, see the following code snippet for an example:
String connectionName = "connection"; IConnection connection = context.getConnectionManager().getConnection(connectionName); IContentManager contentManager = context.getContentManagerFactory().create(connection); |
Which connectionName to use depends on your project's configuration.
The following code snippet shows how to create new content based on a given string.
InputStream stream = new ByteArrayInputStream(contentString.getBytes("UTF-8")); CustomContentProperties properties = new CustomContentProperties(); properties.setProperty("customProperty", new StringValue("value")); String contentName = "content name"; String contentType = "text/plain"; GUID contentId = contentManager.create(stream, contentName, contentType, properties, context.getSessionScope().getActiveUser()); |
The properties object may be used to store additional properties with the content. The contentId will be required to read, update or delete this content at a later time.
The following code snippet shows how to retrieve the string that was created in the previous example.
InputStream stream = contentManager.readData(contentId, context.getSessionScope().getActiveUser()); String contentString = new String(StreamUtil.getBytes(stream), "UTF-8"); |
To retrieve the custom property you may use the readMetadata method, for example:
IContentMetadata metadata = contentManager.readMetadata(contentId, context.getSessionScope().getActiveUser()); StringValue propertyValue = metadata.getProperties().getProperty("customProperty"); |
It is possible to update only the data, only properties or both. The following code snippet shows how to update all at once, overloads exists which do not require all arguments:
InputStream stream = new ByteArrayInputStream(newContentString.getBytes("UTF-8")); CustomContentProperties properties = new CustomContentProperties(); properties.setProperty("customProperty", new StringValue("newValue")); String contentName = "new content name"; String contentType = "text/plain"; contentManager.update(contentId, stream, contentName, contentType, properties, context.getSessionScope().getActiveUser()); |
The following example shows how to delete the content made in the previous example:
contentManager.delete(contentId); |