0

I wanted to use the marketing cloud REST API using script activity to perform various tasks such as uploading content in content builder, changing the customer key for that content based on values in a Data extension and so on.

Is this possible using the script activity in Automation Studio? If yes, could you also provide some good documentation using which I can build the required functionality.

Thank you.

TAG - scriptactivity

Edit: Do not want to do this in cloudpages.

  • there is nice blog written by Zuzanna (sfmarketing.cloud/2020/02/29/…) This will help you to upload content into content builder. Hope this helps! To the other question am not following. Can you please explain how do you want to change the customer key based upon data extension? – Naveen VM Apr 27 at 13:13
  • So we have an external source which provides the content_id for each content, we use this in customer_key so that we can then use contentBlockByKey() function to call these specific content blocks that the external system recognizes. – Moraish Kapoor Apr 27 at 13:19
1

The only actual documentation on the Content Builder API calls is the official REST API docs (and their 'in development version') and places like this. As far as making API calls inside of SFMC via SSJS, there are quite a few resources out there to assist there.

As far as script activity, yes it is 100% possible. Also, as a note, what works on a cloud page will usually work identically inside of a script activity (as long as it is not dependent on server request values or display - e.g. Write). So the below should work in both locations to help you create and update your Assets:

Create

var subDomain = 'myTenantSubDomain' // can be found in API package
var restBase = 'https://' + subDomain + '.rest.marketingcloudapis.com/'
var authToken = 'oAuth token collected from Authentication call'

var blockName = "myHTMLBlock";
var blockKey = "myHTMLBlock";
var assetTypeName = "htmlblock";
var assetTypeId = 197
var myContent = "This is my HTML content"

var createPayload = {
    "name": blockName,
    "customerKey": blockKey
    "description": blockDesc,
    "assetType": {
        "name": "htmlblock",
        "id": 197
    },
    "content": myContent
}

var resp = createHTMLBlock(restBase,authToken,createPayload);

function createHtmlBlock(restBase,authToken,payload) {
  var url = restBase + '/asset/v1/content/assets'
  var req = new Script.Util.HttpRequest(url);
  req.emptyContentHandling = 0;
  req.retries = 2;
  req.continueOnError = true;
  req.contentType = "application/json"
  req.setHeader("Authorization", authToken);
  req.method = "POST";
  req.postData = Stringify(Payload);

    try {
        var resp = req.send();

        var resultStr = String(resp.content);
        var resultJSON = Platform.Function.ParseJSON(resultStr);

        return resultJSON;
    } catch(e) {
        var resultJSON = {"errorcode": 1}
        return resultJSON

    }

    return resultJSON
}

Update

var subDomain = 'myTenantSubDomain' // can be found in API package
var restBase = 'https://' + subDomain + '.rest.marketingcloudapis.com/'
var authToken = 'oAuth token collected from Authentication call'

var blockName = "myUpdatedHTMLBlock";
var blockKey = "myUpdatedHTMLBlock";
var assetTypeName = "htmlblock";
var assetTypeId = 197
var myContent = "This is my Updated HTML content"
var assetid = "the asset id of the block you want to update"

var updatePayload = {
    "name": blockName,
    "customerKey": blockKey
    "description": blockDesc,
    "assetType": {
        "name": "htmlblock",
        "id": 197
    },
    "content": myContent
}

var resp = updateHtmlBlock(restBase,authToken,assetid,updatePayload);


function updateHtmlBlock(restBase,authToken,assetid,payload) {
  var url = restBase + '/asset/v1/content/assets/' + assetid
  var req = new Script.Util.HttpRequest(url);
  req.emptyContentHandling = 0;
  req.retries = 2;
  req.continueOnError = true;
  req.contentType = "application/json"
  req.setHeader("Authorization", authToken);
  req.method = "PATCH";
  req.postData = Stringify(Payload);

    try {
        var resp = req.send();

        var resultStr = String(resp.content);
        var resultJSON = Platform.Function.ParseJSON(resultStr);

        return resultJSON;
    } catch(e) {
        var resultJSON = {"errorcode": 1}
        return resultJSON

    }

    return resultJSON
}

You will notice I used Script.Util to make these calls to keep the process consistent between both calls. This is because there is no native function to utilize the PATCH method, like HTTPPost or similar functions for POST and GET.

| improve this answer | |
0

Moving from comment.

If you follow @Zuzanna blog (https://sfmarketing.cloud/2020/02/29/create-a-cloudpages-form-with-an-image-file-upload-option/)

There will be a section called '//make api call to create asset'

If you would like to add the customer key, then add the customer key from the 3rd party system below in the same code:

 if (base64enc != null) {
        var headerNames = ["Authorization"];
        var headerValues = ["Bearer " + accessToken];
        var jsonBody = {
            "name": fileName,
            "assetType": {
                "name": assetName,
                "id": assetTypeID
            },
            "customerKey": "**Customer Key**"
            "file": base64enc
        };

        var requestUrl = rest_instance_url + "asset/v1/content/assets"

        var createAsset = HTTP.Post(requestUrl, contentType, Stringify(jsonBody), headerNames, headerValues);
    }

Let me know if this helps!

| improve this answer | |
  • I tried running this script and it does run successfully, but the content block isn't created. – Moraish Kapoor Apr 27 at 14:10
  • @MoraishKapoor can you please put the code. Are you trying to insert via cloud page? – Naveen VM Apr 27 at 16:25

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.