Associate Market with Catalog Entry by API Call

Vote:
 

Hi,

We are writing a catalog import tool. Any idea how to associate Market an catalog entry?  

Currently, our import tool will import all other information accept that the entry is not visible in any market. 

Thanks,

Syed

#86940
Jun 04, 2014 19:37
Vote:
 

Any help on this??

#86986
Jun 05, 2014 16:30
Vote:
 

Hi,

Although there is no existing API:s to do so, but it's quite simple to do so.

In eCF system, Markets are represented by _ExcludedCatalogEntryMarkets metafield. It's a Dictionary which contains the "Excluded" market ids - there for if you add a market id to this metafield, then the entry will be unavailable in that market.

You can use the metafield API:s to manipulate this to archive your targets.

Regards.

/Q

#87081
Jun 09, 2014 16:11
Vote:
 

So by default the entry is available to all the markets? If that is the case, I think that is what I want for now... 

#87096
Jun 09, 2014 19:53
Vote:
 

I'm having trouble with this also - see below:

MetaField marketsField = MetaField.Load(metaContext, "_ExcludedCatalogEntryMarkets");
MetaDictionary markets = MetaDictionary.Load(metaContext, marketsField);
foreach (string market in product.MarketExclusions.Split(','))
{
    MetaDictionaryItem mdi = new MetaDictionaryItem(market);
}
MetaHelper.SetMetaFieldValue(metaContext, metaObj, "_ExcludedCatalogEntryMarkets", new object[] { markets });


product.MarketExclusions contains our market IDs, eg. 'AU', 'DE'.  The error I'm receiving implies that market IDs must be numeric, which is contrary to the info in Commerce Manager.  Or do we need to retrieve EPiServer's internal database ID for these markets first?

Thanks,

Jim

#90880
Sep 19, 2014 18:13
Vote:
 

forgot this line:

markets.Add(market);

Still the same error though...any ideas?

#90884
Sep 19, 2014 19:18
Vote:
 

Hi,

What is the error you're getting. MarketID should be the "code" of market when you create it, such as US, UK ... 

/Q

#90888
Sep 21, 2014 7:35
Vote:
 

Hi,

The error is on this line:

MetaHelper.SetMetaFieldValue(metaContext, metaObj, "_ExcludedCatalogEntryMarkets", new object[] { markets });

"Input string was not in a correct format."

   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)\r\n   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)\r\n   at System.Int32.Parse(String s)\r\n   at Mediachase.Commerce.Storage.MetaHelper.b__1(Object v)\r\n   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()\r\n   at System.Collections.Generic.HashSet`1.UnionWith(IEnumerable`1 other)\r\n   at System.Collections.Generic.HashSet`1..ctor(IEnumerable`1 collection, IEqualityComparer`1 comparer)\r\n   at System.Collections.Generic.HashSet`1..ctor(IEnumerable`1 collection)\r\n   at Mediachase.Commerce.Storage.MetaHelper.SetMetaFieldValue(MetaDataContext context, MetaObject obj, String fieldName, Object[] values)\r\n   at MusicSales.Musicroom.Admin.CatalogueUpdater.EPiServerCatalogueManager.AddOrUpdateProduct(CCProduct product) in d:\\Code\\TFS\\Dev\\Non Production\\MusicSales.Musicroom.Admin.CatalogueUpdater\\EPiServerCatalogueManager.cs:line 359

So something is trying to convert from string to number, but I don't know what.  I thought at first the market exclusions were being added without calling this line, but a second look reveals that not to be the case.

For clarity here is the whole snippet again:

                        MetaField marketsField = MetaField.Load(metaContext, "_ExcludedCatalogEntryMarkets");
                        MetaDictionary markets = MetaDictionary.Load(metaContext, marketsField);
                        foreach (string market in product.MarketExclusions.Split(','))
                        {
                            MetaDictionaryItem mdi = new MetaDictionaryItem(market);
                            markets.Add(market);
                        }

                        MetaHelper.SetMetaFieldValue(metaContext, metaObj, "_ExcludedCatalogEntryMarkets", new object[] { markets });

                        metaObj.AcceptChanges(metaContext);



Thanks,

Jim

#90918
Sep 22, 2014 12:54
Vote:
 

OK I see where I was going wrong here...what my code was doing was adding new values to the Markets metafield, rather than for the specific product.

So I've found that once I retrieve the market ID from the system, I can add that to the metafield value successfully.  New code snippet:

                        MetaField marketsField = MetaField.Load(metaContext, "_ExcludedCatalogEntryMarkets");
                        MetaDictionary markets = MetaDictionary.Load(metaContext, marketsField);
                        foreach (string market in product.MarketExclusions.Split(','))
                        {
                            MetaDictionaryItem mdi = markets.GetItem(market);
                            MetaHelper.SetMetaFieldValue(metaContext, metaObj, "_ExcludedCatalogEntryMarkets", new object[] { mdi.Id });
                        }

                        metaObj.AcceptChanges(metaContext);

This appears to work.  But is this the best way to add market exclusion data to a product?  And would the same sort of method be the best way to add any MultiValue meta-data to a product?

Thanks,

Jim

#90928
Sep 22, 2014 14:53
Vote:
 

From my point of view as a developer, I think this is too much work required for this task. Of course this is another metafield, but it's so special that it needs some proper treatment.

I'll discuss with our team to see if we can improve this.

Thanks.

/Q

#90936
Sep 22, 2014 15:56
Vote:
 

OK I still had it wrong, when attempting to add multiple values to this field.  But this works:

                            MetaField marketsField = MetaField.Load(metaContext, "_ExcludedCatalogEntryMarkets");
                            MetaDictionary markets = MetaDictionary.Load(metaContext, marketsField);
                            List marketsToExclude = new List();
                            foreach (string market in variant.MarketExclusions.Split(',').Where(x => x != string.Empty))
                            {
                                MetaDictionaryItem mdi = markets.GetItem(market);
                                marketsToExclude.Add(mdi.Id);
                            }
                            MetaHelper.SetMetaFieldValue(metaContext, metaObj, "_ExcludedCatalogEntryMarkets", marketsToExclude.ToArray());
metaObj.AcceptChanges(metaContext);

I guess I'll be using a similar method to update other multi-valued meta fields.  Is there any existing helper method to do this sort of thing under the hood for multi-value meta fields?

Thanks.

#90939
Sep 22, 2014 16:29
Vote:
 

You should be able to call this

metaObj["_ExcludedCatalogEntryMarkets"] = marketsToExclude.ToArray()

then save metaObj by AcceptChanges. The call to SetMetaFieldValue was not necessary.

Regards.

/Q

#91018
Sep 24, 2014 10:48
Vote:
 

Hi Quan,

Thanks - this looks like it'll be simpler.  Do you know if this call is any more efficient than

MetaHelper.SetMetaFieldValue(metaContext, metaObj, [fieldName], [arrayOfValues]);

?   Or will it do exactly the same thing underneath?

Jim

#91044
Sep 24, 2014 15:30
Vote:
 

Here's the different:

var dictionaryItemKeys = new HashSet<>int>(values.Select(v => int.Parse(v.ToString())));
var selectedItems = new List(dictionaryItemKeys.Count);
foreach (var dictionaryItem in mf.Dictionary.Cast())
{
if (dictionaryItemKeys.Contains(dictionaryItem.Id))
{
selectedItems.Add(dictionaryItem);
}
}

this extra processing seems to be why you're getting the error. You can go with the simpler approach. 

/Q

#91063
Sep 24, 2014 18:02
* 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.