List all asset children using SSJS


(Nick Papadatos) #1

Matrix Version: 5.4

Hello I have the following structure

>Standard page 
     >folder[year](type_1)
        >data_record(type_2)
        >data_record(type_2)
        >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.

example:
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:

Cheers
N


(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”>’);

assets.forEach(function(asset){

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

});

print(’</ul>’);

}

</script>


(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:[]%;
returns

2023 - 0
2022 - 1 etc

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

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…

Cheers
N


(Nick Papadatos) #4

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

Cheers
N


(Chris Mackay) #5

%globals_asset_children:123456^as_asset:asset_name,asset_assetid,asset_data_metadata%;

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:
%asset_children_link_type_1^json_decode^as_asset:{asset_children_link_type_2^json_decode^as_asset:asset_data}^empty:[]%

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.

Cheers
N


(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.

Example:

<script>
   
     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);
     
</script>

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>`;
});

print(output)

Let me know how you go