Try our conversational search powered by Generative AI!

Importing assets via ServiceApi and assigning to entry


I am trying to link a newly uploaded asset to an entry using the following endpoints:

ImportAsset; POST /episerverapi/commerce/import/assets

ItemAsset; POST /episerverapi/commerce/entries/{code}/assets

The result of the ImportAsset appears to work, as I get a Guid response I am assuming is the AssetKey of the uploaded asset in Optimizely.

But when I try linking that asset to an entry with the ItemAsset request (obviously using the AssetKet retreived from the ImportAsset response) I get the following error from Commerce:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      System.ComponentModel.DataAnnotations.ValidationException: Media is not found. Navigate to Assets tab and remove it in order to publish.
         at EPiServer.Core.ContentProvider.ThrowValidationException(ICollection`1 errors)
         at EPiServer.Core.Internal.DefaultContentRepository.Save(IContent content, SaveAction action, AccessLevel access)
         at EPiServer.ServiceApi.Commerce.Controllers.Catalog.Persistence.Internal.ItemAssetModelCommitter.SaveCatalogItemAsset[T](T assetContainer, ItemAsset asset)
         at EPiServer.ServiceApi.Commerce.Controllers.Catalog.ItemAssetController.PostCatalogEntryItemAsset(String entryCode, ItemAsset asset)
         at lambda_method1267(Closure , Object , Object[] )

What am I doing wrong?


Edited, Nov 22, 2023 11:23

ImportAsset; POST /episerverapi/commerce/import/assets

This actually returns the task id (guid) for use in status request, so this wouldn't be the assetkey as this endpoint supports zip files, which could have more than one asset.

This is likely the issue when trying to do the asset linking and with it unable to find the media.

ItemAsset; POST /episerverapi/commerce/entries/{code}/assets

The {code} part is actually the entrycode or the "catalog entry code"/sku

Offical docs show this clearly:

var model = new ItemAsset() 
    CatalogEntryCode = "Jackets-Blazers-Wrap",
    AssetKey = "fd761c1d-5692-48b6-b90a-1ddf4c7c5eb9",
    AssetType = "episerver.core.icontentimage",
    SortOrder = 45,
    GroupName = "large"
var json = JsonConvert.SerializeObject(model);
var client = new HttpClient()
    BaseAddress = new Uri(ConfigurationManager.AppSettings["integrationUrl"])
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);		
var result = client.PostAsync("/episerverapi/commerce/entries/{entry code}/assets",
  new StringContent(json, Encoding.UTF8, "application/json")).Result.Content.ReadAsStringAsync().Result
Jan 16, 2024 17:37

Thanks for your input; I neglected to look further than the "swagger"-like overview on the following page:

I assumed the fact that I could include a binary meant that the binary was a single specific asset of which one could then link to an entry. I have since discovered that this endpoint has a much larger function than simply uploading a single asset:

While I would argue that the function of the asset import endpoint diverts away from the RESTful approach of the rest of the API, the important conclusion here is to read the documentation more thouroughly 😃


Edited, Jan 17, 2024 7:58
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.