I need to build a dynamic content with dynamic images with AMPScript. If I use ContentImageByKey(), I need to change the customer key of every picture individually and logically(for example, use the name of the image). And that's a large amount of images.

So, does anyone knows, how can I change the customer keys with a batch processing. Or where can I make a batch uploading and at the same time give each file a reasonable customer key automatically.

  • You could upload everything then use Automation Studio and SQL to generate a PK based on other fields and a date combination or something. Pretty sure there is not GUID() function in SFMC SQL, but you could create a cloud page or microsite with some ampscript to loop through all rows in the DE and then you can use the GUID() function. – garek007 Jan 17 '19 at 14:41

Option 1
Your best bet is to use the API to import your batch of images. That way you can specify programatically the customerkey of each image you upload. The call you would do is Create Asset REST API call (POST /asset/v1/content/assets)

Inside this call you can dynamically set your name/customerkey as you upload the images. The main drawback to this is that

  1. You will need to have the Base64 encoded values of each image you want to import
  2. You will need to have a JSON or similar data rowset for your script to iterate through
  3. I believe that you will need to do each asset as an individual call.

Sample Body of the REST call:

  "Name": {{name}},
  "CustomerKey": {{customerkey}}
  "assetType": {
    "name": "jpg",
    "id": 23
  "category": {
    "id": 123456 (ID of the folder) ,
    "name": "name of folder"
  "file": "xxxxxBase64Encoded-Filexxxxx"

where {{name}} and {{customerkey}} are variables set in your script and inserted into the body.

You can find the list of asset types here in case you want to add gif or png, etc. images.

You then can use the naming convention you set up inside your If conditional to set the appropriate image customerkey.

Option 2
Your other option with API is to do your normal bulk upload via the UI - but include a unique naming convention. For example, "DynamicCampaign_Image201901_{{specific file name}}".

Then do a REST API call to retrieve the assets CustomerKey (using Asset Simple Query GET - GET /asset/v1/content/assets). I would use a simple filter on the name using the like simple operator to get all associated with the upload.

Something like: /asset/v1/content/assets?$filter=Name%20like%20'DynamicCampaign_Image201901_'

This will return a JSON object containing the CustomerKeys (automatically set inside SFMC on upload) of each of your images. I would then take the name and customerkey combos and push them into a DE ('ImageDE').

In your email, you then do a lookup to your 'ImageDE' with the corresponding name to pull up the Key for your ContentImageByKey() function.

  • @TianhengWei - Out of curiousity, why did you remove the 'accepted answer' check mark? If there was some issue, let me know and I can edit my answer to resolve it. – Gortonington Jan 22 '19 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.