Saving MetaStringDictionary in EPiServer Commerce 9

Vote:
 

In EPiServer Commerce 8 we had been using the following code to update a MetaStringDictionary:

metaObj.Modified = DateTime.UtcNow;
metaObj.ModifierId = "CustomerSkuNumbers job";
                        
MetaHelper.SetMetaFieldValue(metaContext, metaObj, MetaDataPlusConfigurator.CustomerSkuNumbers, new object[] { customerItemSkus });
mLogger.Debug("MetaObject AcceptChanges for [CustomerSkuNumbers] for Item [{0}]", itemSkusNavicoGroup.Key);
metaObj.AcceptChanges(metaContext);

catalogNodes.Items.Add(data.Data.ItemSkus.First(i => i.NavicoSKU == itemSkusNavicoGroup.Key));
mLogger.Debug("MyEditScope.SaveChanges for [CustomerSkuNumbers] for Item [{0}]", itemSkusNavicoGroup.Key);
myEditScope.SaveChanges();

var serialized = new MetaObjectSerialized();
mLogger.Debug("SerializedData MetaObject for Item [{0}] under MetaContext language [{1}]", item.Key, metaContext.Language);
serialized.AddMetaObject(metaContext.Language, metaObj);
entry.CatalogEntry[0].SerializedData = serialized.BinaryValue;

mLogger.Debug("SaveCatalogEntry for [CustomerSkuNumbers] for Item [{0}]", itemSkusNavicoGroup.Key);
CatalogContext.Current.SaveCatalogEntry(entry);
mLogger.Debug("Done SaveCatalogEntry for [CustomerSkuNumbers] for Item [{0}]", itemSkusNavicoGroup.Key);

But since upgrading to EPiServer Commerce 9 the .SerializedData property on the entry is deprecated and removed. I still haven't figured out how to make this data persist. I've tried a variety of code changes but think the core change is indicated in Quan's post "Moving away from ICatalogSystem, part 0" but I don't yet understand how to incorporate this with saving a MetaStringDictionary via the IContentRepository (simply getting this variant and saving it via the contentRepo doesn't help).

I know I'm missing something, is this enough information for someone else to help me figure out what to do?

#144116
Feb 08, 2016 7:49
Vote:
 

Hi,

To save a metaobject, just call metaObject.AcceptChanges(), you don't have to care about the serialized data.

IContentRepository (to be exact, CatalogContentProvider) still does not support StringDictionary. You'll have to save it via MetaObject. 

Regards,

/Q

#144119
Feb 08, 2016 9:40
Vote:
 

Hi Quan

Thanks for the prompt reply!

Well, I experiemented with just using the metaObject.AcceptChanges(metaContext) as seen on Line 6, but the data doesn't persist. It's strange though because it will change the metaObject data for the duration of the AppPool's life - once the app pool has been recycled it reverts back to the original data - also, another telling sign, the commerce manager UI never shows the update data.

So you believe all I need is what I've got on Line 6 to persist the data properly?

Let me know if a larger snippet of code if it would help check what I'm doing.

Kind regards, Matt

#144120
Feb 08, 2016 10:13
Vote:
 

Hi,

That's very strange - your code looks correct. Was you able to check if the data was saved to database or not?

- The revert to original data can be result of some initialization step. Do you have any custom initialization code?

- If you change other data in Catalog UI, does the change get reflected in Commerce Manager? I think there might be an issue with cache synchronization here.

/Q

#144121
Feb 08, 2016 10:39
Vote:
 

Hi Quan

I can confirm that the dictionary values do not change in the database.

Definitely no initialization code that is messing with meta fields or commerce stuff.

Really don't know how to move forward on this - are MetaStringDictionary a lost cause? should I look at storing this data in a different way?

Matt

#144186
Feb 09, 2016 7:31
Vote:
 

Hmm,

What is your customerItemSks?

Does it work if you simply do this:

var stringDict = new MetaStringDictionary();

stringDict.Add(<key>,<value>),

...

metaObj[<fieldNam>] = stringDict;

metaObj.AcceptChanges(metaDataContext);



/Q

#144190
Feb 09, 2016 8:39
Vote:
 

Hi Quan

Interesting, I tried the following crude MetaStringDictionary like you suggested (see below) - and the data persists to the database!

var customerItemSkus = new MetaStringDictionary();
customerItemSkus.Add("amer_c009204_test1", "TEST1");
metaObj[MetaDataPlusConfigurator.CustomerSkuNumbers] = customerItemSkus;
metaObj.AcceptChanges(metaContext);

So then I tried the following crude approach to add one but to my existing dictionary (see below) - and again the data doesn't persist :-/

var customerItemSkus = metaObj.GetStringDictionary(MetaDataPlusConfigurator.CustomerSkuNumbers);
customerItemSkus.Add("amer_c009204_test2", "TEST2");
metaObj[MetaDataPlusConfigurator.CustomerSkuNumbers] = customerItemSkus;
metaObj.AcceptChanges(metaContext);

It's promising that I can get it working with a new MetaStringDictionary but I need it to work with existing dictionaries. Maybe I will need to do something ugly like replace the full dictionary each time rather than try change the existing one?

I just can't figure how the first code above works whilst the second code doesn't..?

Matt

#144224
Edited, Feb 09, 2016 13:46
Vote:
 

Hi,

Did you try to debug and see the value of customerItemSkus after:

var customerItemSkus = metaObj.GetStringDictionary(MetaDataPlusConfigurator.CustomerSkuNumbers);

/Q

#144239
Feb 09, 2016 17:30
Vote:
 

Yes, and it looks correct to me - it has all the existing key/values in it..

#144251
Feb 09, 2016 23:22
Vote:
 

To get past this problem I'm simply going to create a new MetaStringDictionary every time - at least it works this way!

Let me know if you have any other ideas but I'm happy with this anyway - thanks for all of your help!

#144252
Feb 09, 2016 23:32
* 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.