Content Element Functions

In This Topic




The functions for the contentelements are separated into two categories: creating an element and deleting an element. To create an element there are several functions available that all have the same name but different in parameters.


Creating an Element

To create an element there is one method available with six different parameters. The method name is createAndInsertElement. It returns an Element and can have up to three parameters:

  • context - context in which the element will be placed (valid contexts: PageVersion, MediaItemVersion, PageModel). This parameter is required.
  • element class - what kind of element will be placed. This parameter is required.
  • position  - at what position will the element be inserted. This parameter is optional and should contain an int with value 0 or higher. If the number is larger than the position of the last element, the new element will be created at the bottom of the page. The first element on the page has position 0. If this parameter is omitted, then the new element will also be created at the bottom..

The following code sample shows how to add a Text element to the homepage:


Page homepage = mySession.getXperienCentralApplication().getWebsites()[0].getHomePage();
myElementService.createAndInsertElement(homepage, TextElement.class);


The following code sample shows how to add an image element at the top (position 0) of the homepage:


Page homepage = mySession.getXperienCentralApplication().getWebsites()[0].getHomePage();
myElementService.createAndInsertElement(homepage, ImageElement.class, 0);


Back to Top



Deleting an Element

Deleting an element is done using the method deleteElement. It returns a boolean indicating whether the delete action was successful. The method has one parameter: the element to be deleted.

For example:


Page homepage = mySession.getXperienCentralApplication().getWebsites()[0].getHomePage();
Element element = myElementService.createAndInsertElement(homepage, TextElement.class);
if (!myElementService.deleteElement(element)) {
	LOG.severe("Failed to delete element: " + element);
}