Uploading CommerceMedia in assets programatically

Vote:
 

Hi,

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 = "site.businessmodel.models.media.imagefile",
GroupName = "test2"
};

variant.CommerceMediaCollection.Add(commerceMedia);
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
http://site.development.local/cm/UI/Commerce/Catalog#context=epi.cms.contentdata:///162__CatalogContent

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?

Regards
Khurram

#90494
Sep 10, 2014 17:08
Vote:
 

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

/Q

#90496
Sep 10, 2014 17:28
Vote:
 

0 |162 | dk.businessmodel.models.media.imagefile | 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.

Regards

/K

#90515
Edited, Sep 11, 2014 9:30
Vote:
 

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.

Regards.

/Q

#90567
Sep 12, 2014 10:36
Vote:
 

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.

Regards
/K

#90569
Edited, Sep 12, 2014 11:23
Vote:
 

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.

#90574
Sep 12, 2014 12:30
Vote:
 

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.

Regards.

/Q

#90575
Sep 12, 2014 12:59
Vote:
 

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

Any idea what settings should I look into?

#90589
Edited, Sep 12, 2014 15:32
Vote:
 

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 ...

/Q

#90604
Edited, Sep 15, 2014 3:54
Vote:
 

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.

/K

#90638
Sep 15, 2014 13:02
Vote:
 

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. 

Regards
/K

#90783
Sep 17, 2014 17:55
Vote:
 

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? 

/Q

#90786
Sep 18, 2014 4:39
Vote:
 

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

#90857
Sep 19, 2014 10:26
Vote:
 

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

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

/Q

#90875
Sep 19, 2014 16:45
Vote:
 

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...

#183927
Oct 24, 2017 16:41
Vote:
 

Hi,

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

Please help us to out.

#203386
Apr 19, 2019 19:47