Skip to content

Instantly share code, notes, and snippets.

@wvpv
Created April 4, 2023 16:48
Show Gist options
  • Save wvpv/e9769274115ea8290dddceced8005e88 to your computer and use it in GitHub Desktop.
Save wvpv/e9769274115ea8290dddceced8005e88 to your computer and use it in GitHub Desktop.
Content Builder Asset Inventory
<script runat="server" language="JavaScript">
Platform.Load("core","1");
var debug = false;
try {
var prox = new Script.Util.WSProxy();
var startTime = Now();
var timeoutMilliseconds = 1500000; // 25 minutes
var tokenExpireMilliseconds = 1020000; // 17 minutes
// API CONFIGURATION
var clientId = "YOURCLIENTIDHERE";
var clientSecret = "YOURCLIENTSECRETHERE";
var tenant = "YOURTENANTHERE";
var mid = Platform.Function.AuthenticatedMemberID();
var authURL = "https://" + tenant + ".auth.marketingcloudapis.com/v2/token";
var endPoint = "https://" + tenant + ".rest.marketingcloudapis.com/";
var TargetDECustomerKey1 = 'Assets';
var TargetDECategoryID = 111111; // YOUR DE FOLDER'S CATEGORYID HERE
// CREATE DE
try {
var result=prox.createItem("DataExtension",{"CustomerKey":TargetDECustomerKey1,"Name":TargetDECustomerKey1,"CategoryID":TargetDECategoryID,"Fields":[
{"CustomerKey":"folderPath","Name":"folderPath","FieldType":"Text","MaxLength":"1000","IsPrimaryKey":false,"IsRequired":false},
{"CustomerKey":"Name","Name":"Name","FieldType":"Text","MaxLength":"300","IsPrimaryKey":false,"IsRequired":false},
{"CustomerKey":"CustomerKey","Name":"CustomerKey","FieldType":"Text","MaxLength":"36","IsPrimaryKey":false,"IsRequired":false},
{"CustomerKey":"AssetType_Id","Name":"AssetType_Id","FieldType":"Number","IsPrimaryKey":false,"IsRequired":false},
{"CustomerKey":"AssetType_Name","Name":"AssetType_Name","FieldType":"Text","MaxLength":"100","IsPrimaryKey":false,"IsRequired":false},
{"CustomerKey":"CreatedDate","Name":"CreatedDate","FieldType":"Date","IsPrimaryKey":false,"IsRequired":false},
{"CustomerKey":"ModifiedDate","Name":"ModifiedDate","FieldType":"Date","IsPrimaryKey":false,"IsRequired":false},
{"CustomerKey":"Id","Name":"Id","FieldType":"Number","IsPrimaryKey":true,"IsRequired":true},
{"CustomerKey":"ObjectId","Name":"ObjectId","FieldType":"Text","MaxLength":"36","IsPrimaryKey":false,"IsRequired":false},
{"CustomerKey":"Category_Id","Name":"Category_Id","FieldType":"Number","IsPrimaryKey":false,"IsRequired":false},
]});
if (debug) {
//Write("<br>add DE1 result:"+ Stringify(result))
}
} catch (e) {
//if (debug) { Write("<br>e: " + Stringify(e)); }
}
var accessToken = "";
var pageSize = 20;
var totalPages = 0;
var pageNum = 1;
if (debug) {
Write("<br><br>pageSize: " + pageSize);
}
do {
if (debug) { Write("<br>pageNum: " + pageNum); }
// check current time against timeout threshold
if ((Now() - startTime) >= timeoutMilliseconds) {
break;
}
// check access token
if (accessToken == "" || (Now() - startTime) >= tokenExpireMilliseconds) {
accessToken = getAccessToken(clientId, clientSecret, mid, authURL);
if (debug) { Write("<br>accessToken: " + accessToken); }
}
if (accessToken != "") {
var url = endPoint + "asset/v1/content/assets/query";
var headerNames = ["Authorization"];
var headerValues = ["Bearer " + accessToken];
var contentType = 'application/json';
// NOTE: AssetTypes can be found here: https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/base-asset-types.html
// NOTE: Adjust the query criteria as needed
var payload = {
"page": {
"page": pageNum,
"pageSize": pageSize
},
"query": {
"leftOperand": {
"property": "assetType.id",
"simpleOperator": "IN",
"value": [3,4,5,6,195,196,197,198,199,200,201,202,203,207,208,209,210,211,212,213,214]
},
"logicalOperator":"OR",
"rightOperand": {
"property": "assetType.name",
"simpleOperator": "in",
"value": ["templatebasedemail","htmlemail","code","template","message"]
}
},
"sort": [
{
"property": "category.parentId",
"direction": "ASC"
},
{
"property": "category.id",
"direction": "ASC"
},
{
"property": "name",
"direction": "ASC"
}
]
};
var assets0 = HTTP.Post(url, contentType, Stringify(payload), headerNames, headerValues);
var assets = Platform.Function.ParseJSON(assets0["Response"][0]);
if (debug) { Write("<br>assets.count: " + assets.count); }
if (debug) { Write("<br>assets.items.length: " + assets.items.length); }
if (totalPages == 0) {
totalPages = Math.ceil(assets.count / pageSize) + 1;
if (debug) { Write("<br>totalPages: " + totalPages); }
}
if (assets.items && assets.items.length > 0) {
var de = DataExtension.Init(TargetDECustomerKey1);
var rowsAddedUpdated = 0;
for (i = 0; i < assets.items.length; i++) {
var row = {};
row.Id = assets.items[i].id;
row.CustomerKey = assets.items[i].customerKey;
row.ObjectId = assets.items[i].objectID;
row.AssetType_Id = assets.items[i].assetType.id;
row.AssetType_Name = assets.items[i].assetType.name;
row.Name = assets.items[i].name;
row.Category_Id = assets.items[i].category.id;
row.CreatedDate = assets.items[i].createdDate;
row.ModifiedDate = assets.items[i].modifiedDate;
row.folderPath = getFolderPath(assets.items[i].category.id);
try {
rowsAddedUpdated = de.Rows.Add(row);
} catch (e) {
if (debug) { Write("<br>e: " + Stringify(e)); }
try {
rowsAddedUpdated = de.Rows.Update(row, ['id'], [row.id]);
} catch(e2) {
if (debug) { Write("<br>e2: " + Stringify(e2)); }
}
}
if (debug) { Write("<br>rowsAddedUpdated: " + rowsAddedUpdated); }
}
}
pageNum++;
}
} while (pageNum <= totalPages)
function getAccessToken(clientId, clientSecret, mid, authURL) {
var accessToken = "";
if (clientId != "" && clientSecret != "" && mid != "" && authURL !="") {
var payload = {
"grant_type": "client_credentials",
"client_id": clientId,
"client_secret": clientSecret,
"account_id": mid
};
var contentType = "application/json";
var result = HTTP.Post(authURL, contentType, Stringify(payload), null, null);
accessToken = Platform.Function.ParseJSON(result["Response"][0]).access_token;
}
return accessToken
}
function getFolderPath(categoryID) {
var list = "";
var path = function(id) {
if (id > 0) {
var results = Folder.Retrieve({Property:"ID",SimpleOperator:"equals",Value:id});
list = results[0].Name + " > " + list;
return path(results[0].ParentFolder.ID);
} else {
return id;
}
};
path(categoryID);
return list;
}
} catch (e) {
if (debug) {
Platform.Response.Write("<br><br>e: " + Stringify(e));
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment