Category Archives: API Editing content classes

Add a new content class as preassigned everywhere

In my projects I often use a blocks container at the end of all pages to offer the possiblity to enhance the page layout, in an optional way. Therefore I usually add a container element on each page content class called blocks.

After creating a new content class, which should be available in all these “extension points” I need to change the preassignments of all blocks elements in all page content classes.

Doing this via SmartTree is, depending on the number of content classes, a tedious and error-prone job.

Get rid of it and find below the jRQL code needed to achieve this easily.

String logonGuid="0904ABF0E43443D2881FE7481339650E";
String sessionKey="490EC675042F4C5A8A6DF1ED63ADD7A6";
String projectGuid="268F46EF5EB74A75824856D3DA1C6597";

CmsClient client = new CmsClient(logonGuid);
Project project = client.getProject(sessionKey, projectGuid);

Template newTemplate = project.getTemplateByName(“content_templates”, “text_block”);

for (TemplateFolder folder : project.getAllTemplateFolders()) {
for (Template template : folder.getTemplates()) {
// skip not page templates
if (!template.getName().endsWith(“_page”)) {
continue;
}
if (template.contains(“blocks”)) {
TemplateElement blocksElem = template.getTemplateElementByName(“blocks”);
if (blocksElem.isTemplatePreassigned(newTemplate)) {
blocksElem.addPreassignedTemplate(newTemplate);
System.out.println(folder.getName() +”;”+ template.getName() +”;”+ blocksElem.getName());
}
}
}
}

First I get the new content class I want to preassign on all blocks elements. The double for loops simply go through all content classes in your project.

Because in my project (see it as an example) I skip all content classes which might have a container element named blocks, but are not a physical page. (I name a physical page a page in RedDot CMS which determine the filename of the published file.)

To leave out content classes for physical pages which did not have a container element called blocks (preventing an error trying to access it) I check with contains() method (available on a page too).

The isTemplatePreassigned() if condition should show you how to check a preassignment. In this context it triggers an addPreassignedTemplate(newTemplate) only, if it’s not preassigned already.

In this context maybe the method hasPreassignedTemplates() is of interest.   It delivers true, if at least one content class is preassigned and false if no preassigments are used.

Updating the fixed stylesheet on all HTML text elements

I use a fixed stylesheet to customize the text editor’s wysiwyg. RedDot CMS only offer a possibility on the content class text element. A change on the fixed stylesheet to leads to tons of clicks needed to open all content class folders, content classes and content elements to check all.

This was a perfect motivation to automate this. Please see the complete code first and find below the explanations.

String logonGuid="6E5AC86AEA484CEAA1240C59BDEB7265";
String sessionKey="84818066A2BF4D51A9551BBDE3DF51AF";
String projectGuid="73671509FA5C43ED8FC4171AD0298AD2";

// get fixed text styles
String fixedStyles = IOUtils.toString(new FileInputStream(“d:\\texteditor_styles.txt”));

// open project
CmsClient client = new CmsClient(logonGuid);
Project project = client.getProject(sessionKey, projectGuid);

for (TemplateFolder folder : project.getAllTemplateFolders()) {
for (Template template : folder.getTemplates()) {
for (TemplateElement textElem : template.getTextTemplateElements()) {
if (textElem.isHtmlText()) {
textElem.setTextFixedStylesheet(fixedStyles);
System.out.println(folder.getName() +”;”+ template.getName() +”;”+ textElem.getName());
}
}
}
}

After the usual prologue (replace you login GUIDs!) you see 3 loops finding all content class text elements in all content classes of all folders.

The if checks the found text element to update only the HTML text elements, all ASCII text elements will be unchanged.

The benefit of such a helper is huge. Imagine how many clicks you need to achieve this manually in SmartTree! And, not less important, all text editor behaves definitely in the same way.

In my case I had to ran this code several times until the text editor wysigwyg was perfect.

Now you should be able for similar mass updates on content class elements.  See the javadoc for all set methods on class TemplateElement.