javier_fernande
Occasional Observer

Get Page from its UID

Jump to solution

Hi Everyone,

I've got these code:

store = context.project.userService.getStore(Store.Type.CONTENTSTORE, false);

serviceDB = store.getContent2ByName("service");

store.setLock(true, false);

session = serviceDB.schema.session;

services = session.executeQuery( session.createSelect(serviceDB.entityType.name) );

for (service : services){

  serviceID = service.getValue("id");

  name = service.getValue("name");

  globalContent = service.getValue("global_content");

  context.logInfo("Service id: " + serviceID);

  context.logInfo("Service GC id: " + globalContent);

}

// End for

store.setLock(false, false);

I receive "globalContent" as a String (something like this "<CMS_VALUE name="dt_global_content" tag="FS_REFERENCE"><LANG id="§" set="1"><UID>0505_UID</UID><UIDTYPE>PAGESTORE</UIDTYPE><REMOTE></REMOTE></LANG></CMS_VALUE>"), how can I use this to get the real Page with that UID?

Thank you!!!

0 Kudos
1 Solution

Accepted Solutions
mbergmann
Crownpeak employee

Hi Javier,

first, it is recommended to use the StoreElementAgent (DEV API) to get objects - using UserService is "the old way" (but not deprecated).

So you should get the Content2 node like this:

StoreElementAgent storeElementAgent = context.requireSpecialist(StoreElementAgent.TYPE);

Content2 serviceDB = (Content2) storeElementAgent.loadStoreElement("service", Content2.UID_TYPE, false);

When working with database entries, you should always "switch up" one abstraction layer. This means using Datasets instead of Entities. Unfortunately, a query returns a List of "low level data" entities, so you have to request a corresponding Dataset. As the column "global_content" maps to a FS_REFERENCE, you can then directly access the iinput component value which is a TargetReference in this case.

For this, you will also have to provide the name of the input component (in the TableTemplate) that maps to the column "global_content", for example "tt_globalContent" (you'll have to adjust that to your situation).

String globalContentFieldName = "tt_globalContent";

for (service : services){

  Dataset serviceDataset = serviceDB.getDataset(service);

  TargetReference targetReference = (TargetReference) serviceDataset.getFormData().get(null, globalContentFieldName).get();

  

  //and here is your page:

  Page page = (Page) targetReference.get();

}

Hope this helps.

Michael

View solution in original post

0 Kudos
2 Replies
mbergmann
Crownpeak employee

Hi Javier,

first, it is recommended to use the StoreElementAgent (DEV API) to get objects - using UserService is "the old way" (but not deprecated).

So you should get the Content2 node like this:

StoreElementAgent storeElementAgent = context.requireSpecialist(StoreElementAgent.TYPE);

Content2 serviceDB = (Content2) storeElementAgent.loadStoreElement("service", Content2.UID_TYPE, false);

When working with database entries, you should always "switch up" one abstraction layer. This means using Datasets instead of Entities. Unfortunately, a query returns a List of "low level data" entities, so you have to request a corresponding Dataset. As the column "global_content" maps to a FS_REFERENCE, you can then directly access the iinput component value which is a TargetReference in this case.

For this, you will also have to provide the name of the input component (in the TableTemplate) that maps to the column "global_content", for example "tt_globalContent" (you'll have to adjust that to your situation).

String globalContentFieldName = "tt_globalContent";

for (service : services){

  Dataset serviceDataset = serviceDB.getDataset(service);

  TargetReference targetReference = (TargetReference) serviceDataset.getFormData().get(null, globalContentFieldName).get();

  

  //and here is your page:

  Page page = (Page) targetReference.get();

}

Hope this helps.

Michael

0 Kudos

Hello Javier,

do you need further assistance or did Michael's reply already help you? If so, it would be great if you marked his reply as "correct answer" so that other community users find the solution easily. If you already have found a solution by yourself, it would be very kind of you, if you posted it here.

Best regards,

Lena

0 Kudos