[SalesForce] Use AMPScript to upload an image into Portfolio in Marketing Cloud

We are trying to find a way to upload an image with AMPScript into Portfolio of the Email Studio (Classic Content) in the Salesforce Marketing Cloud.

I know this is possible with SOAP or REST APIs, but we need it to be done with AMPScript. SOAP API AMPscript Functions can be used though, and this is the only way as far as I understand. But when this code is executed, we get an error:

SET @portfolio = CreateObject('Portfolio')
SetObjectProperty(@portfolio, "DisplayName", "API Uploaded2")
SetObjectProperty(@portfolio, "CustomerKey", "css-grid-vs-flexbox")
SetObjectProperty(@portfolio, "FileName", "css-grid-vs-flexbox.png")

SET @resourceSpecification = CreateObject('ResourceSpecification')

SetObjectProperty(@resourceSpecification, "URN", "http://cdn.tutorialzine.com/wp-content/uploads/2017/03/css-grid-vs-flexbox.png")
SetObjectProperty(@portfolio, "Source", @resourceSpecification)

/* Add data */
SET @deStatusCode = InvokeCreate(@portfolio, @deStatusMsg, @deErrorCode)

IF @deStatusCode != "OK" THEN
    RaiseError(@deStatusMsg, 0, @deStatusCode, @deErrorCode)
ENDIF

Error Details are below:

ErrorCode=2

StatusCode=Error

Message=Exception occurred during [CreateMedia] ErrorID: 1308368347

Unfortunately, Salesforce support does not provide any support on this, and they do not have any example code in AMPScript.

I found related questions here, but none got an answer, only references to https://developer.salesforce.com which is not helpful in this case:

Best Answer

The only problem with your code is that the ClientID field is missing, it will be the MID of the business unit where you need to upload your image to portfolio. I have updated this code and tested it worked fine for me.

Updated code:

    %%[

SET @portfolio = CreateObject("Portfolio")
SetObjectProperty(@portfolio, "DisplayName", "API Uploaded2")
SetObjectProperty(@portfolio, "CustomerKey", "css-grid-vs-flexbox")
SetObjectProperty(@portfolio, "FileName", "css-grid-vs-flexbox.png")

SET @ClientID = CreateObject("ClientID")
SetObjectProperty(@ClientID, "ID", "MID goes here")
SetObjectProperty(@portfolio, "Client", @ClientID )

SET @resourceSpecification = CreateObject("ResourceSpecification")

SetObjectProperty(@resourceSpecification, "URN", "http://cdn.tutorialzine.com/wp-content/uploads/2017/03/css-grid-vs-flexbox.png")
SetObjectProperty(@portfolio, "Source", @resourceSpecification)

/* Add data */
SET @deStatusCode = InvokeCreate(@portfolio, @deStatusMsg, @deErrorCode)

IF @deStatusCode != "OK" THEN
    RaiseError(@deStatusMsg, 0, @deStatusCode, @deErrorCode)
ENDIF
]%%
Related Topic