[using commerce 9.24]
A while back, I related products (and variants) to Categories, by doing this:
// Create new Category relation:
var nodeRelation = relations.NewNodeEntryRelationRow();
nodeRelation.CatalogId = _catalogId;
nodeRelation.CatalogEntryId = catalogEntryId;
nodeRelation.CatalogNodeId = targetCategoryNode.CatalogNodeId;
nodeRelation.SortOrder = 0;
if (nodeRelation.RowState == DataRowState.Detached)
Today, I'd like to change those relations -- I'd like to relate those products to a different category (and thus remove the previous relation).
How can I accomplish this programmatically?
In Commerce we use the term "association" when it's entry-entry (think about related products), and "relation" when it's parent-child. Minor, but it would be easier to follow.
To move a product to a new category, you have to option:
If you upgrade to Commerce 11+ then IRelationRepository can be used to Remove/Update relations (yes, it's already in Commerce 9 but the APIs were significantly changed in Commerce 11 so I'd not really recommend to use it unless you upgrade)
(^ fixed the terminology in my first post...)
I used the second option. It SEEMED to work - because now when I retrieve the (only) NodeEntryRelationRow for my product, it correctly shows the new Category.
However - when I view the product in CMS admin screen (in the "Belongs To" tab) it shows BOTH categories. Is there another piece of data I need to clean up somewhere?
No that should be it. It does sound like there was a cache some where. Can you turn on the browser console and disable cache to see if the problem is still there?
You're right Quan, there's some caching happening (Epi's cache?).
The anomalies I'm seeing sometimes persist for up to 10 minutes - but eventually do resolve themselves to the correct data..
Is there a way i can clear the cached data programmatically?
Are you running the code to update the relation in the same site? Or on a different instance? The cache should be invalidated automatically if the remote events are configured correctly.
9.24 is quite old (2 years ago, more or less), so I'm not sure, there might be a bug which was fixed in later version. However that would be quite unlikely.
Huh now that you mention it -- we are running the code in a different Epi instance (but sharing the same databases).
Do you think that is why the cache persists?
Would we have a better experience if we used IContentRepository.Move?
IContentRepository.Move uses same cache mechanism, so if you don't fix the cache invalidation issue, it will be the same.
The cache uses the remote events to broadcast messages - so make sure you have the right configuration https://world.episerver.com/documentation/items/developers-guide/episerver-cms/75/event-management/wcf-event-management/