verified
Retrieve all AssetsLearn how to retrieve all assets from Content Builder in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include REST API methods.
Assets
var result = [],
page = 0;
while(++page) {
var endpoint = restInstanceUrl + "asset/v1/content/assets?$page=" + page + "&$pagesize=50";
var request = HTTP.Get(endpoint, ["Authorization"], ["Bearer " + accessToken]);
var content = Platform.Function.ParseJSON(String(request.Content));
if(!content.items || content.items.length == 0) break;
result = result.concat(content.items);
}
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
accessToken = "YOUR_REST_API_TOKEN";
try {
var result = [],
page = 0;
while(++page) {
var endpoint = restInstanceUrl + "asset/v1/content/assets?$page=" + page + "&$pagesize=50";
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "GET";
request.contentType = "application/json";
var results = request.send();
var content = Platform.Function.ParseJSON(String(results.content));
if(!content.items || content.items.length == 0) break;
result = result.concat(content.items);
}
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
[
{
"id": 123456,
"customerKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"assetType": {
"id": 28,
"name": "png",
"displayName": "Image"
},
"fileProperties": {
"fileName": "1.png",
"extension": "png",
"fileSize": 1615,
"fileCreatedDate": "2021-01-19T06:05:16.430124-06:00",
"width": 72,
"height": 72,
"publishedURL": "https://mydomain.com/lib/S0M3-GU1D-K3Y-G03SR1G4T-H3R3/m/1/S0M3-GU1D-K3Y-G03SR1G4T-H3R3.png"
},
"name": "1.png",
"modelVersion": 2
}
]
Categories (folders)
var result = [],
page = 0;
while(++page) {
var endpoint = restInstanceUrl + "asset/v1/content/categories?$page=" + page + "&$pagesize=50";
var request = HTTP.Get(endpoint, ["Authorization"], ["Bearer " + accessToken]);
var content = Platform.Function.ParseJSON(String(request.Content));
if(!content.items || content.items.length == 0) break;
result = result.concat(content.items);
}
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
accessToken = "YOUR_REST_API_TOKEN";
try {
var result = [],
page = 0;
while(++page) {
var endpoint = restInstanceUrl + "asset/v1/content/categories/?$page=" + page + "&$pagesize=50";
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "GET";
request.contentType = "application/json";
var results = request.send();
var content = Platform.Function.ParseJSON(String(results.content));
if(!content.items || content.items.length == 0) break;
result = result.concat(content.items);
}
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
[
{
"id": 12345,
"description": "The root folder for assets",
"enterpriseId": 987654321,
"memberId": 987654321,
"name": "Content Builder",
"parentId": 0,
"categoryType": "asset"
}
]
Reference
Ressources and references related to the current methods.
Official documentation