Quan Mai
Oct 15, 2019
  12261
(6 votes)

New simple batch saving API for Commerce

In Commerce 13.8, we introduced a new, simple batch saving API for catalog content. If you want to try it out, look into new extension method of IContentRepository, resides in 

EPiServer.Commerce.Extensions. There is a new method called "Publish", which allows you to publish multiple catalog content at once. This is an example of updating all language versions of a content.

            var languageBranches = ContentRepository.GetLanguageBranches<VariationContent>(content.ContentLink);
            var list = new List<VariationContent>();
            foreach (var languageBranch in languageBranches)
            {
                var writable = languageBranch.CreateWritableClone<VariationContent>();
                writable.Name += "updated";
                writable.DisplayName += "updated";
                list.Add(writable);
            }

            ContentRepository.Publish(list);

Publish can take language versions of a content, multiple content with same language, or a combination of both.

It's important to keep this mind that this method skips certain things for the optimal speed. As in the remarks, "This method bypasses the content publishing pipeline and therefore does not validate for error, nor fire content level events, nor handle versions. It does not create new versions, but commits data in the contents to the published versions.". The low level events, however will still be fired.

You might ask what would be the use cases of the new API. Well, this new API is simple, and it acts as a shell for Catalog import export. You can achive almost the same result (with somewhat better performance) with CatalogImportExport. However, the big drawback of CatalogImportExport is that you need to have the XML - either from a PIM connector, or build it yourself, which is not the most hassle-free task in the world. If you already have code to build your catalog content instances, you can use this API to "publish" your change.

A new improvement which allows you to sync draft is coming in Commerce 13.9. A new overload which takes a PublishAction (new enum) parameter is added, if you choose PublishAction.SyncDraft, it will sync the changes to the versions, so you can see changes there as well.

Note that this API is still marked as BETA. As usual, BETA means tested, but we reserve the right to make changes to the APIs without notice/major versions. That also means your feedback and suggestions can be implemented sooner than usual.

Oct 15, 2019

Comments

Mari Jørgensen
Mari Jørgensen Oct 15, 2019 12:24 PM

Nice! Looking forward to test this :)

Raed Aljawad
Raed Aljawad Oct 15, 2019 07:49 PM

It will be great if that supports import digital assets too

Johan Petersson
Johan Petersson Oct 15, 2019 08:21 PM

I'm receiving this exception when trying to modify assets, so I second what Raed just wrote.

System.Data.DeletedRowInaccessibleException: Deleted row information cannot be accessed through the row. at System.Data.DataRow.GetDefaultRecord() at System.Data.DataRow.get_Item(DataColumn column) at Mediachase.Commerce.Catalog.Dto.CatalogEntryDto.CatalogItemAssetRow.get_CatalogEntryId() at Mediachase.Commerce.Assets.Database.AssetServiceDatabase.<>c__DisplayClass4_0.<CommitAssets>b__0(CatalogItemAssetRow x) at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext() at Mediachase.Commerce.Assets.Database.AssetServiceDatabase.CommitAssets(IEnumerable`1 updatingRows, CatalogEntryDto entryDto, Int32 entryId) at EPiServer.Commerce.Catalog.Provider.Persistence.EntryContentBaseCommitter.CommitAssets(EntryContentBase content, CatalogEntryDto entry, Int32 entryId) at EPiServer.Commerce.Catalog.Provider.Persistence.EntryContentBaseCommitter.UpdateExisting(IEnumerable`1 contents, Boolean syncDraft) at EPiServer.Commerce.Catalog.Provider.Persistence.EntryContentBaseCommitter.Save(IEnumerable`1 contents) at EPiServer.Commerce.Extensions.IContentRepositoryExtensions.Publish(IContentRepository contentRepository, IEnumerable`1 contents)

Quan Mai
Quan Mai Oct 15, 2019 09:05 PM

@Raed @Johan I will look into that.

Are you trying to delete the asset? What code did you use?

Raed Aljawad
Raed Aljawad Oct 15, 2019 09:17 PM

@Quan I will let @Johan answer above question regarding the error. From my end currently we are using EPI Service API to sync catalog and assets and my point was does the new API support importing assets as we need both typically when we push data into Episerver (i.e. from a PIM)

Johan Petersson
Johan Petersson Oct 15, 2019 09:19 PM

Yes, just did a simple writeable.CommerceMediaCollection.RemoveAt(i);.

Quan Mai
Quan Mai Oct 15, 2019 09:55 PM

Thanks. I will add a fix for that in 13.9

Tien Quach
Tien Quach Oct 17, 2019 07:45 AM

Nice, this would simplify our import process.

Mari Jørgensen
Mari Jørgensen Nov 6, 2019 12:58 PM

@Quan, is there a recommended max nr of items for a batch? 

Quan Mai
Quan Mai Nov 6, 2019 02:45 PM

@Mari: I did some test and 200 per batch is marginally faster than 100 per batch. But YMMV. You can always start with 20. 

Mari Jørgensen
Mari Jørgensen Nov 7, 2019 09:39 AM

Ok, so I have played around with this for a while.

After spending 2-3 hours thinking I had a bug in my code, I realized the issue was the following:

Changes applied on objects using .Publish() are not reflected in the Catalog UI. If I query the database, I can see my changes.

So, is this a bug or "by design" (i.e. cached for a while)? 

Quan Mai
Quan Mai Nov 11, 2019 07:57 AM

@Mari you probably are not using 13.10 and the Publish(contents, PublishAction.SyncDraft)?

Mari Jørgensen
Mari Jørgensen Nov 11, 2019 07:59 AM

I am on version 13.9, so that is correct.

Quan Mai
Quan Mai Nov 11, 2019 09:01 AM

Just to be clear you can use the overload on 13.9, however we recommend to use 13.10 as it's already out and has some bugs fixed.

Mari Jørgensen
Mari Jørgensen Dec 4, 2019 07:17 PM

Finally got around to upgrade to 13.10 and test the api again. I can confirm that bug is fixed, and it seems a bit faster than 13.9. Great stuff!

Mari Jørgensen
Mari Jørgensen Jan 20, 2020 11:11 AM

Update: there are 2 bugs in play related to batch api. Could be nice to know about:

https://world.episerver.com/support/bug-list/bug/COM-10634 and
https://world.episerver.com/support/Bug-list/bug/COM-10609

Gregoire Bodson
Gregoire Bodson Mar 2, 2020 12:43 PM

Hi,

"This method bypasses the content publishing pipeline and therefore does not validate for error, nor fire content level events, nor handle versions. It does not create new versions, but commits data in the contents to the published versions"

Does it invalidate native related cache entries and triggers a find index of said changed item?

Quan Mai
Quan Mai Mar 2, 2020 01:01 PM

The low level events are still fired, and the cache are still be validated properly. If you don't disable Find Commerce event indexing then everything should work as usual 

Please login to comment.
Latest blogs
SaaS CMS - Pages and Blocks get the Visual Builder Treatment

I’m thrilled to see that Optimizely has now enabled Visual Builder for OG Pages and Blocks within SaaS CMS, and I’m guessing this will become...

Minesh Shah (Netcel) | Dec 17, 2024

Optimizely’s Sustainability Journey: Creating a Future That’s Built to Last

  For me, sustainability isn’t just a company priority; it’s a personal one. I’m especially mindful of the role that content management plays at th...

Joey Moore | Dec 13, 2024

How I Fixed DLL Conflicts During EPiServer CMS Upgrade to .NET Framework 4.8.1

We had a CMS solution of EPiServer 11.26.0, which was built on .NET Framework 4.7.1. We needed to update the target framework from .NET Framework...

calimat | Dec 12, 2024

Custom form element view in Optimizely CMS 12

Do you want full control over the form element markup? Create your own views!

Tomas Hensrud Gulla | Dec 11, 2024 | Syndicated blog

How to Elevate Your Experimentation - Opticon workshop experience

As a non-expert in the field of experimentation, I’d like to share my feedback on the recent Opticon San Antonio workshop session titled "How to...

David Ortiz | Dec 11, 2024

Persisting a Strawberry Shake GraphQL Client for Optimizely's Content Graph

A recent CMS project used Strawberry Shake to generate an up-to-date C# GraphQL client at each build. But what happens to the build if the GraphQL...

Nicholas Sideras | Dec 11, 2024 | Syndicated blog