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.
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:
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.
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.
REST API for Marketing Cloud Asset - GET /asset/v1/content/assets | Marketing Cloud API | Salesforce Developers
Please feel free to let me know if you find this article useful and leave your feedback/comment in case of any queries.