1

I have tried with below AMPscript code to fetch all the email names with the subject line from our account. However, the below code is fetching only classic content emails and it's not pulling the content builder email.

Salesforce support also confirmed that AMPscript is pulling only classic emails. It does not include Content Builder emails.

Kindly let me know if there is any other way to fetch all the emails including content builder email.

%%[ 
SET @rr = CreateObject("RetrieveRequest") 
SetObjectProperty(@rr, "ObjectType", "Email") 
AddObjectArrayItem(@rr,"Properties","Name") 
AddObjectArrayItem(@rr,"Properties","Subject") 

SET @rows = InvokeRetrieve(@rr) 

RowCount(@rows)

FOR @i = 1 to RowCount(@rows) DO 

  SET @row = Row(@rows ,@i) 
  SET @emailName = Field(@row ,'Name') 
  SET @emailSubject = Field(@row ,'Subject')
  outputline(concat("<br>EmailName:", @emailName)) 
  outputline(concat("<br>EmailSubject:", @emailSubject)) 
  InsertData("CheckEmail",'EmailName', @emailName,"EmailSubject", @emailSubject) 

NEXT @i
]%%

EDIT
This is current code based on Gortonington's answer. I receive an empty response though:

var restBase = "url"; 
var queryStr = '?$page=1&$pagesize=100&$orderBy=name asc&$filter=data.email.legacy.legacyType=email&$fields=name,views'; 
var authToken = "genertaed"; 
var Result = getCBEmails(restBase,queryStr,authToken) 
Write("<br/><br/>Results: " + Result) 

function getCBEmails(restBase,queryStr,authToken) { 
  var headerNames = ["Accept", "Authorization"]; 
  var headerValues = ["application/json", "Bearer "+authToken]; 
  var url = restBase + '/asset/v1/content/assets' + queryStr; 
  var response = HTTP.Get(url, headerNames, headerValues); 
  return response; 
} 

1 Answer 1

4

I find this kind of stuff easier to do via SSJS than AMPscript (plus with SSJS you can run it in a script activity instead of a CloudPage).

As a note, Content Builder emails can only be retrieved via REST. This will require a package to be created in your BU to be able to gather a token for REST calls.

Here are two functions I use when retrieving emails from Classic and from Content Builder:

function getClassicEmails(mid) {

  if(mid) {
    prox.setClientId({ "ID": mid }); //Impersonates the BU
  }

  var cols = ["Name","Subject"];
  var desc = prox.retrieve("Email", cols);
  return desc;
}

and for Content Builder:

function getCBEmails(restBase,queryStr,authToken) {

  var url = restBase + '/asset/v1/content/assets' + queryStr;
  var res = HTTP.Get(url, ["Authorization"], [authToken]);
  return deResult;

}

For the CB function, you need to get an auth token first, which I use the below function:

function generateToken(clientId, clientSecret, mid, authURL, version) {

  if (version == 2) {
    var versionEndpoint = '/v2/token'

    var authJSON = {
  "grant_type": "client_credentials",
  "client_id": clientId,
  "client_secret": clientSecret,
  "account_id": mid
  }
  } else {
    var versionEndpoint = '/v1/requestToken'

 //URL changes for V1 endpoints

      var authJSON = {
    "clientId": clientId,
    "clientSecret": clientSecret
  }
  }

  var authUrl = authURL + versionEndpoint;
  var contentType = 'application/json';
  var authPayload = Platform.Function.Stringify(authJSON);

  var accessTokenResult = HTTP.Post(authUrl, contentType, authPayload);

  var statusCode = accessTokenResult["StatusCode"];
  var response = accessTokenResult["Response"][0];

  if(version == 2) {
      var accessToken = Platform.Function.ParseJSON(response).access_token;
  } else {
      var accessToken = Platform.Function.ParseJSON(response).accessToken;
  }

  return accessToken;
}

below are the variables needed for the token call:

var clientId = 'yourClientID';
var clientSecret = 'yourClientSecret';
var subDomain = 'tenantspecificsubdomain';
var authURL = 'https://' + subDomain + '.auth.marketingcloudapis.com/'
var restBase = 'https://' + subDomain + '.rest.marketingcloudapis.com/'
var version = 2 //if v2 or legacy(1)

if (version == 1) {
    var authURL = "https://auth.exacttargetapis.com";
    var restBase = "https://www.exacttargetapis.com";
}

I then would recommend a 'queryStr' of something like:

//I wouldn't go too far above 100 a call or it chances being 'timed out'
var queryStr = '?$page=1&$pagesize=100&$orderBy=name asc&$filter=data.email.legacy.legacyType=email&$fields=name,views'

This sets page (1), max return (100 records) orders by name ascending, filters to only return emails and then only returns name and 'views' object, which contains the subjectline in it. (views.subjectline.content)

If the number of emails in your BU exceed the max page size in the Classic (2500) or ContentBuilder functions (query string limits it to 100), you will need to create a loop or something to continue the request and get the next records.

5
  • var restBase = "url"; var queryStr = '?$page=1&$pagesize=100&$orderBy=name asc&$filter=data.email.legacy.legacyType=email&$fields=name,views'; var authToken = "genertaed"; var Result = getCBEmails(restBase,queryStr,authToken) Write("<br/><br/>Results: " + Result) function getCBEmails(restBase,queryStr,authToken) { var headerNames = ["Accept", "Authorization"]; var headerValues = ["application/json", "Bearer "+authToken]; var url = restBase + '/asset/v1/content/assets' + queryStr; var response = HTTP.Get(url, headerNames, headerValues); return response; }
    – Ranjan
    Commented May 26, 2020 at 10:11
  • Thank you @Gortonington for your update. I am able to generate the token, however I am unable to generate the response for content builder emails. It displaying the empty value. I am not that confortable with SSJS , may be I am doing some wrong. I have used the above code
    – Ranjan
    Commented May 26, 2020 at 10:16
  • Try using Write("<br/><br/>Results: " + Stringify(Result)) - If you do not have the Core platform loaded, you will need to change Stringify() to Platform.Function.Stringify(). Result is returned as an object, which cannot be displayed on the browser, you need to turn it into a string so it can be displayed. Commented May 26, 2020 at 12:08
  • Thank you @gortonington. It's working now and its display the complete string. However, can we write a for loop to pull only template name and subject line? I wrote var stuff = Stringify(Result); var a = stuff.length and run the for loop but no luck.
    – Ranjan
    Commented May 26, 2020 at 16:20
  • @Ranjan - you would want the loop to be something like: for (var i=0; i < Result.length; i++) { } the Stringify is only to have it displayed on the page. You would need to utilize the actual JSON in order to utilize length and parsing. Commented May 26, 2020 at 17:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .