List all asset children using SSJS

(Nick Papadatos) #1

Matrix Version: 5.4

Hello I have the following structure

>Standard page 
        >data_record(type_2) etc

Trying to iterate through each folder then display the data records underneath similar to how you would use an asset lister by using group formatting.

I can get to ther data_records but it’s returing as a whole string.

let assets = %asset_children_link_type_1^as_asset{asset_children_link_type_2^as_asset:asset_name}^empty:[]%

Any help would be great and there must be an easy way to access children > siblings.

In the meantime I have used an Asset Lister but I still need to work out the above :slight_smile:


(Cherry) #2

Try something like this

<script runat=“server”>

var assets = %asset_children_link_type_1^as_asset:asset_short_name,asset_url^empty:[]%;

if(assets.length > 0){

print(’<ul class=“menu”>’);


print(’<li class=“menu__item is-leaf leaf”><a class=“menu__link” href="’+ asset.asset_url +’">’+ asset.asset_short_name + ‘</a></li>’);





(Nick Papadatos) #3

This will only returns the folder name because it’s the direct children yes?
var assets = %asset_children_link_type_1^as_asset:asset_short_name,asset_url^empty:[]%;

2023 - 0
2022 - 1 etc

let html = '';
    assets.forEach((asset, index) => {
       if(asset.asset_type_code === 'folder'){
           html +=`<p>${asset.asset_name} - ${index}</p>`;

let assets = %asset_children_link_type_1^as_asset:asset_name,asset_type_code^empty:[]%; Gives me the folder names based on the above

Now I need to get to its children iside the folder
let dataRecords = %I need to chain the above asset_children to get to the childrens children%

What I have that’s not working

let dataRecods = %asset_children_link_type_1^as_asset:asset_children_link_type_2^empty:[]%;

If I try and chain more values I do get an error…


(Nick Papadatos) #4

Help here would be appreciated - trying to avaoid a support ticket for this…


(Chris Mackay) #5


Where 123456 is the parent asset ID.

Once you have the asset ID’s of the children, you can do a forEach loop to return the values you need for the children.

Once you get down to the grandchildren level, it starts to get messy.

I find that it’s easier at this point to just use an asset listing and a REST resource so its cached (asset listing does not have to run every time) to give you the output you want (JSON payload) and use SSJS from there.

Hope this helps.

(Nick Papadatos) #6

Thanks Chris,

What ended up working me was:

You can also then pick these attributes …^as_asset:asset_url,asset_name,asset_url etc

What wasn’t clear to me initially was that this keyword modifier creates a mulitidimensional array, you then need to create a nested for loop to access each column and row in the array.

9 out of 10 times I would use an asset lister but this was more of a discovery process for me but someone else may benefit from this post.


(Heath) #7

Is it possible to get an example of this?

I’m trying to build a SSJS that will list all assets under a specific parent. but I can only go 1 level deep.

Any help would be great.

(Nick Papadatos) #8

What I like to do when working with SSJS and matrix asset attributes is to use client side script to output the data so I can see what is available and also the structure of data.


     let log = console.log;
     let output = '';
     let directChildren  = %asset_children_link_type_1^as_asset:asset_name,asset_type_code^empty:[]%;
     let subPages        = %asset_children_link_type_1^json_decode^as_asset:{asset_children_link_type_2^json_decode^as_asset:asset_data}^empty:[]%;
     let data            = %asset_children_link_type_1^as_asset:asset_children_link_type_2^empty:[]%;

     log('directChildren: ', directChildren);
     log('subPages: ', subPages);
     log('data: ', data);

Use the inspector to review the outputs.

The only way to print the subleves is to nest your for each loop. Please note the subPages Array is now a multidimensional array, read more

   subPages.forEach((record) => {
       output +=`<div class="list">`;
           record.forEach((item, index) => {
           output +=`<div class="heading"><h2>${item} - ${index}</h2></div>`;
  output +=`</div>`;


Let me know how you go