top of page

Retrieve Marketing Cloud Assets Using SSJS and REST API

Updated: Sep 17, 2021

Imagine that in your marketing cloud instance you have recently purchased SAP, or your Org already have SAP but recently bought an SSL. It's always good to use images with SSL. In these cases, you may need to find the existing images which has portfolio URL but not with SSL or the default exact target URL.


For this use case, we are leveraging the Asset REST API Simple Query.


Below is the Postman REST API GET call:


Drawbacks on Asset REST API GET Call:

  • It can only return 50 items in a single page.

  • You can add only one condition. If you want to add more conditions then use POST Advance query.

Logic:


Having said the drawbacks above, we will be using only one condition here which is Page count. We will iterate the page count every time.


We will Parse the response JSON and get the count & pageSize values. Dividing those will give you the number of times that you need to iterate.


For example, in this case we have count (5553) / page size (50) = 111.06. We will add +1 and use Math.ceil function.


So, 111.06 + 1 = 112.06.

Math.ceil(112.06) = 112.


Let's create a data extension called 'Test_Asset_URL'.

It has 2 attributes:

  • Asset_URL

  • Asset_Name

We will be upserting the data based upon URL.


Code Walk through Video:



Add the Code Snippet in Cloud page or SSJS Script activity:


Decoding the code:

  • Line 5, replace it with your Auth end point.

  • Line 7 & 8, replace it with your client Id & secret.

  • Line 14, making a POST call to get the Access Token.

  • Line 23 to 26, making a GET call to get the 1st page assets

  • Line 27 to 30, parsing the assets and get the count, page size and page count.

  • Line 31, formula to identify how many times we need to iterate and dynamically change the page number.

  • Line 32 to 37, dynamically changing the page count in the GET URL and parsing the JSON.

  • Line 38 to 42, checking if it's an Image and get the Image URL & Image Name.

  • Line 43, upserting the data in Test_Asset_URL data extension.

Output:


As you can see from the below screenshot, it has 407 records with Image URL & Name.

Similarly, you can do with other assets like email as well.


Additional Resources:


Please feel free to let me know if you find this article useful and leave your feedback/comment in case of any queries.


Happy Learning!




6,848 views3 comments

Recent Posts

See All

3 comentarios


Stanley Kudo
Stanley Kudo
26 jun 2023

Hello,

I tried using the above code and when I run the automation it runs successfully but the record count on DE is still 0, I have a lot of assets on the org, tried changing names but not getting the issue, could you please assist on what I could be doing wrong?

Thank you

Me gusta

Hi Team,

I am using this code to get the Name and URL and its working fine. While I am trying to add one more field 'created date', its retreiving the empty(null) values.

The last lines of the code is as below


var publishedURL = finalResponseFinal.items[i].fileProperties.publishedURL;

var fileName = finalResponseFinal.items[i].fileProperties.fileName;

var fileCreatedDate = finalResponseFinal.items[i].fileProperties.fileCreatedDate;

var rows = Platform.Function.UpsertDE("Test_Asset_URL", ["Asset_URL"], [publishedURL], ["Asset_Name"], [fileName], ["AssetCreatedDate"], [fileCreatedDate]);


Could you please help me with what I am missing?


Thank you in advance

Me gusta
Contestando a

Hi Sreenivasa,


have you tried createdDate instead of fileCreatedDate?

Me gusta
bottom of page