dakaspbe
New Creator

Edit content page with API

Hi,

I want to edit a content page within a Beanshell script using the API.

This is the script part I tried:

 if (!page.isLocked()) {
   page.setLock(true, true);
}
 
...
page.setFormData(formData);
page.save();
page.release();
page.setLock(false, false);
 

Currently I get the following Error when I try to lock the page in the first step:

Target exception: java.lang.UnsupportedOperationException: lock is not supported for release store elements

Does anybody why this Exception occurs and how I can avoid it.

Or is there another way to edit the page?

Thanks in Advanced

0 Kudos
17 Replies
Peter_Jodeleit
Crownpeak employee

The page element is from the release store, which is read-only. Only elements from the current store can be edited.

Peter
0 Kudos
dakaspbe
New Creator

Hi,

thanks for the quick answer. How can I select the element of the correct current store?

Currently my workflow is as follows:

1. First find a specific template:

StoreElementAgent sea = context.requireSpecialist(StoreElementAgent.TYPE);
template = sea.loadStoreElement(TEMPLATE_UID, SectionTemplate.UID_TYPE, false);

2. Find all usages of this template:

template.getIncomingReferences()

3. select the parent-page for each reference

section = ref.getReferencedElement();
page = section.getParent().getParent();

 

Unfortunately these pages are always in the release store (page.isInReleaseStore() returns true).

How can I edit the pages? What am I doing wrong?

Kind regards

Bernadette

0 Kudos
mbergmann
Crownpeak employee

Hi,

the „isInReleaseStore()“ does not tell if the element is FROM the release store but (just) if that node (also) exists there. AFAIR It will only return false for never released elements.

It should help to check the ReferenceEntry itself whether it points to an element from the releaseStore and ignore those:

ref.getRelease()

Michael

0 Kudos
dakaspbe
New Creator

Hi Michael,

thanks for the hint. Unfortunately the error just changed.

Now I see the following Exception in the fs-server.log:

ERROR 13.12.2023 07:13:52.501 {uID=0,g-node=2412686,seID=2417967,pID=2312121,g-sec=2412681} (de.espirit.firstspirit.generate.SiteProduction): could not change page due to java.lang.SecurityException: read only store
inside of: Template 'Produkt Seite' (id=2312649)
inside of: $CMS_TRIM(level:2)$ - at 3, 1
inside of: $CMS_IF( #global.page.body("content").childCount > 0 )$ - at 22, 3
inside of: $CMS_FOR( for_section, #global.page.body("content").children )$ - at 23, 4
inside of: $CMS_VALUE(for_section)$ - at 28, 6
inside of: Template 'Grid Container' (id=2312652)
inside of: $CMS_VALUE(st_elements)$ - at 3, 3
inside of: Template 'Column' (id=2312653)
inside of: $CMS_VALUE(st_cards)$ - at 15, 2
inside of: Template 'Card' (id=2312654)
inside of: $CMS_TRIM(level:3)$ - at 32, 1
inside of: $CMS_VALUE(st_content)$ - at 60, 31
inside of: Template 'Accordion' (id=2312660)
inside of: $CMS_VALUE(st_content)$ - at 22, 3
inside of: Template 'Card Text' (id=2312655)
inside of: $CMS_VALUE(st_content)$ - at 1, 1
inside of: Format Template 'unformatted_text' (id=2412496)
inside of: $CMS_VALUE(#content)$ - at 1, 41
inside of: Link Template 'bnp_default_link' (id=2412511)
inside of: $CMS_TRIM(level:2)$ - at 1, 1
inside of: $CMS_IF( ("internal".equals(lt_type.value) && ! lt_ref.isEmpty) || ("external".equals(lt_type.value) && ! lt_url.isEmpty) || ("mail".equals(lt_type.value) && ! lt_mail.isEmpty) )$ - at 9, 2
inside of: $CMS_SWITCH( lt_type.value )$ - at 15, 3
inside of: $CMS_IF( if(isSet(lt_use_disclaimer), lt_use_disclaimer, false) )$ - at 29, 6
inside of: $CMS_RENDER(script:"add_host_to_whitelist", extUrl:lt_url)$ - at 30, 7
inside of: Script 'add_host_to_whitelist' (id=2430683) - at 0, 0

 

Here's the used code:

StoreElementAgent sea = context.requireSpecialist(StoreElementAgent.TYPE);
 
template = sea.loadStoreElement(TEMPLATE_UID, SectionTemplate.UID_TYPE, false);
  if (null != template) {
  references = template.getIncomingReferences();
    for (ref : references) {
          if(!ref.getRelease()) {
      section = ref.getReferencedElement();
        if (section instanceof Section) {
          page = section.getParent().getParent();
 
          if (page.isLockSupported()) {
          try {
if (!page.isLocked()) {
          context.logInfo("setLocked =" + page.getReleaseStatus()); // the releaseStatus = 0 (RELEASED)
          page.setLock(true,true);
      }
} catch(e) {
context.logError("could not change page due to " + e);
}
 
It's the same exception for elements from release store. 
How do I get the element writeable?
 
Kind regards
Bernadette
0 Kudos
Peter_Jodeleit
Crownpeak employee

This is a different error 😉

Inside a generation task you are in a read only environment, so it's intentional that you cannot alter data.

Could you describe your use case? There may be an alternative solution available.

 

Peter
0 Kudos
dakaspbe
New Creator

Hi Peter,

thanks for the answer.

We want to create a list (whitelist) with all used external links within our website.

I thought that I just can add a new domain to the whitelist when I generate the corresponding link within the page. 

 

some years ago we had a script which created the whitelist by using FindExternalHosts.find().

As this object doesn't exisits anymore I thought it is a good idea to just add new hosts everytime we create an external link. In this case I can easily pass the current link to the script.

Kind regards

Bernadette

0 Kudos
dakaspbe
New Creator

Hi, 

is it possible to get all references/usages of a linkTemplate referenced by referencename?

I mean something similar to 

context.requireSpecialist(StoreElementAgent.TYPE).loadStoreElement(TEMPLATE_UID, SectionTemplate.UID_TYPE, false);

 

In this case I can create the whitelist independent from the generation process by concating all used Urls.

Kind regards

Bernadette

0 Kudos
Peter_Jodeleit
Crownpeak employee

Yes, you can use the getIncomingReferences() method of the LinkTemplate. Each incoming reference is a usage. It sounds like you are interested in usages from the PageStore...

Peter
0 Kudos
mbergmann
Crownpeak employee

Not sure if that fits your use case but have you seen ProjectReferencesAgent.getExternalReferences(…)?

Just a hint - might be useful depending on your case.