Try our conversational search powered by Generative AI!

Uploading CommerceMedia in assets programatically



In a schedul Job I am downloading images for xyz location and trying to associate with variants. I am trying to do it in 7.5 style.

var contentRepository = ServiceLocator.Current.GetInstance();
var contentLoader = ServiceLocator.Current.GetInstance();
var loader = ServiceLocator.Current.GetInstance();
var referenceConvertor = ServiceLocator.Current.GetInstance();

string variantId = "VariantCode";

var variantLink = referenceConvertor.GetContentLink(variantId);
var variant = (EPiServer.Commerce.Catalog.ContentTypes.VariationContent)contentLoader.Get(variantLink).CreateWritableClone();
var databaseConnectionProvider = ServiceLocator.Current.GetInstance();
databaseConnectionProvider.ConnectionString = this.DatabaseConnectionStrings[SiteConstants.IntegrationDatabaseConnectionName].ConnectionString;
var fileID = new ContentReference(621);
var commerceMedia = new EPiServer.Commerce.SpecializedProperties.CommerceMedia()
AssetLink = fileID,
AssetType = "",
GroupName = "test2"

contentRepository.Save(variant, EPiServer.DataAccess.SaveAction.Publish);

I am using following code to upload an Assets for a variant.
In Debugging mode I can see CommerceMediaCollection increases every time but I can't see those in CMS/Edit/Catalog window

I tried an iis reset also after attaching MediaItem, but cant see those items.

I tried to uplaod a media Item from interface It sucessfull uploaded that content but List was reverted to the visible items there.

Can you spot what I am doing wrong?


Sep 10, 2014 17:08

Your code looks correct, it has some minor issues but they should not affect the result.

Did you try to see if the media was attached to the variant? You can run this sql in context of your Commerce database:

Select * from dbo.CatalogItemAsset where CatalogEntryId = 162 -- given 162 is your entry id


Sep 10, 2014 17:28

0 |162 | | 8fde4a0a-0a73-4588-a5fe-31b32344bb31 | test2 | 9

Yes, record exists

Entries Exist in tblContent, and tblWorkContent table also I compare with other asset set by interface also.



Edited, Sep 11, 2014 9:30

I guess you're running the tool in another context instead of the site? Because with different context, the catalog content events might not fire and catch properly, causing the Catalog content draft to be not updated.

One thing to check is going to Commerce Manager, make some modification to the entry (Name/description/display name ...) then save. Go back to Catalog UI to see if the assets are loaded correctly.

If that's the case then you'll need to configure your tool to be able to fire events, by either adding configuration or let it run in context of your website.



Sep 12, 2014 10:36

Clever thought, I tested in Commerce Manager, after changing a property and saving that in Commerce Manager, New assets were also pouplated.

I am doing this in an EPiServer Scheduled Job. I am wondering how can I triger event from schedule job and what should I do in that Event to make it apear.


Edited, Sep 12, 2014 11:23

To test the theory I created an asmx file (Web service) as it will be running under the site context and try to bind the asset with variant their. Result is same.

Sep 12, 2014 12:30

I'm on opposite site. Tried with this code:

var contentRepo = ServiceLocator.Current.GetInstance();
var referenceConverter = ServiceLocator.Current.GetInstance();

var contentLink = referenceConverter.GetContentLink(100, CatalogContentType.CatalogEntry, 0);

var content = contentRepo.Get(contentLink).CreateWritableClone() as VariationContent;
content.CommerceMediaCollection.Add(new CommerceMedia() { AssetLink = new Core.ContentReference(2549), AssetType ="episerver.core.icontentimage", GroupName = "default", SortOrder = 0 });
contentRepo.Save(content, DataAccess.SaveAction.Publish, Security.AccessLevel.NoAccess);
return "";

And run it in context of a scheduled job, both auto and manually and it works for me.

Don't know about your website version but I'm assuming it's quite new. I would suspect there's a problem with your site - might be in settings. You might want to try with another clean site (Sample Site 7.5 + updated nuget) to see if it works.



Sep 12, 2014 12:59

I am on commerce 7.11, tried exact your code, NO Luck.

Any idea what settings should I look into?

Edited, Sep 12, 2014 15:32

That's strange. Did you try with the normal code in your site? I.e no scheduled job, no webservice?

Currently I can't think of any possible problems. As it works when you update from CM then the event system should be working, and the catalog content draft system should also be working, too. Very weird indeed ...


Edited, Sep 15, 2014 3:54

That Code works under the controller :S.
When I added a new item through controller, it not only added that item but all the items that I have attached through Scheduled Job.


Sep 15, 2014 13:02

Hi Quan,

Its strange but probably you can also replicate that. My site is Multi-lingual, It is showing the assets that I am uploading by Scheduled Job for all other languages except UK. It will make your test more challenging.
Is there any cache key for assets or is their any dependency regarding the languages you can think of to solve this. 


Sep 17, 2014 17:55

CommerceMediaCollection is not marked as "CultureSpecific", so it does not depend on the language of the content (aka all language-version of content will have same asset collection). 

Is en-GB is master language of your site? 


Sep 18, 2014 4:39

Yep, en-GB is a master language.
By the way Happy Bithday.

Sep 19, 2014 10:26

Thanks! (My birthday was some days ago, actually)

Was in a middle of something urgent. I will test with multi-lingual site next week :)


Sep 19, 2014 16:45

Things go bump in the night I guess - Khurram did you ever get to the bottom of this?

Having a similar issue - 

var writableContent = (EntryContentBase)content.CreateWritableClone();
writableContent.CommerceMediaCollection.Add(new CommerceMedia(mediaData.ContentLink, mediaData.GetType().Name, model.GroupName, model.SortOrder ?? 0));
_contentRepository.Save(writableContent, SaveAction.Publish, AccessLevel.NoAccess);

I can see records in the DB - the site sees "commerceMediaCollection" has a list of items, but the catalog manager admin shows no assets...

Oct 24, 2017 16:41


Is this issue is resolved? Because we are facing the same.

Please help us to out.

Apr 19, 2019 19:47


I believe that we've stumbled across this issue also, using Episerver Commerce 13.9.0. Or we're doing something obviously wrong which I can't seem to figure out what it is. 

To start with we have a product in Commerce to which we're trying to attach a second image to. The code (boiled down to it's core) looks like this:

var imageFile = _contentRepository.Get<CommerceImageFile>(new ContentReference(12345678));
var productReference = _referenceConverter.GetContentLink("PRODUCTCODE");
var product = _contentRepository.Get<ProductContent>(productReference);
// product content link id = 51361

product = product.CreateWritableClone<ProductContent>();
// product.CommerceMediaCollection contains 1 item

var commerceMedia = new CommerceMedia(imageFile.ContentLink, string.Empty, string.Empty, 1);
// product.CommerceMediaCollection contains 2 items

_contentRepository.Save(product, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);

var updatedProduct = _contentRepository.Get<ProductContent>(productReference);
// updatedProduct.CommerceMediaCollection contains 1 item

var connectedContentReferences = _contentRepository.GetReferencesToContent(imageFile.ContentLink, true);
// connectedContentReferences contains 0 items

Looking in the table CatalogItemAsset for CatalogEntryId 51361 I only see one line.

What's extra strange though is that when looking under the Resources tab for this product, both images are listed!?

If anyone can help us forward with this problem we'd really appreciate it!

Best regards


Edited, Nov 20, 2019 22:34

For anyone interested, the problem I faced was apparently a bug in Commerce 13.8.0 and 13.9.0 but was fixed in 13.10.0 by this bug fix:


Dec 04, 2019 10:04
* 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.