Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Get ALL commerce catalogs

Vote:
 

Can anyone tell me how to retrieve all catalogs out of commerce?  I'm not sure if I am missing something simple or if this is a bug. I am creating a scheduled task where I need to loop through our catalogs and perform some tasks on them.  I thought the following line of code would work, but discovered that one of our catalogs is not coming back in the results.  

var catalogs = _contentLoader.GetChildren(_referenceConverter.GetRootLink());

We have catalog A with languages of en-us, en-ca, and fr-ca.

We have catalog B with language of en-us

We have catalog C with languages of en-ca and fr-ca

When I call the code above, I only get catalog A and B back.  So finally, I went to catalog C and added en-us as an available language to see if I got it back in the call.  When I run the code, I in fact will get it back in the results.  I then removed en-us as an available language for C and made the call again, and C is not in the results. 

Worst case scenario, I loop through the ILanguageBranchRepository.ListEnabled() and add each distinct catalog I find to my own list to loop through, but that just seems dumb that I would have to go down that path.

Thoughts?

#148161
May 05, 2016 4:28
Vote:
 

This is a wild guess but I had a smililar problem a while back and in my case it was due to permissions for the user is being used to run the scheduled job.
Try setting a specific user in the code, preferably an admin account, and see if this solves the issue.

#148164
May 05, 2016 8:16
Vote:
 

I actually went down that path too based on something a co-worker told me based on how the scheduled tasks run, but unfortunately that was not the issue, still do not get the one catalog back.

#148200
May 05, 2016 15:50
Vote:
 

Still was unable to get anything to get it back correctly, so I ended up coding this out to meet my needs:

var languageBranch = _languageBranch.Service.ListEnabled();
var catalogList = new List<CatalogContent>();

foreach (var lang in languageBranch) {
    var catList = _contentLoader.Service.GetChildren<CatalogContent>(_referenceConverter.Service.GetRootLink(), new LanguageSelector(lang.LanguageID))   

    foreach(var catTemp in catList)
   {
        if (!catalogList.Exists(x => x.CatalogId == catTemp.CatalogId))
       {
            catalogList.Add(catTemp);
        }
    }
}

This meets my needs, but it still doesn't make sense as to why my original code wasn't bringing back the one catalog that didn't have en-us as an available language.

#148210
May 05, 2016 22:48
Vote:
 

What is set as the master language?

For making the content loader fall back to a specific language you could do

var fallbackCulture = new CultureInfo("en-US");

_contentLoader.Service.GetChildren<CatalogContent>(_referenceConverter.Service.GetRootLink(), new LoaderOptions() { LanguageLoaderOption.FallbackWithMaster(fallbackCulture) });

Or always fetch from master language

_contentLoader.Service.GetChildren<CatalogContent>(_referenceConverter.Service.GetRootLink(), new LoaderOptions() { LanguageLoaderOption.MasterLanguage() });


#148518
May 16, 2016 15:19
Vote:
 

I guess really the net issue I was trying to solve is how come this:

var catalogs = _contentLoader.GetChildren<CatalogContent>(_referenceConverter.GetRootLink());

Doesn't correctly return me all catalogs as it should.  

That being said, in response to the previouse comment, our master language is en-us, so setting the master language would still not get us back catalog C with languages of en-ca and fr-ca.

#148521
May 16, 2016 16:24
Vote:
 
LanguageSelector.AutoDetect(true)

Try using this as language selector...
#148523
May 16, 2016 17:29
Vote:
 

Is catalog Enabled?

#148587
May 17, 2016 19:07
Vote:
 

It sure is.

#148589
May 17, 2016 20:44
Vote:
 

I was haveing the same issue. 

Instead of using IContentLoader use IContentRepository. 

Not working 

var catalogs1 = _contentLoader.GetChildren<CatalogContent>(catalogRootLink);

Working

var catalogs = _contentRepository.GetChildren<CatalogContent>(catalogRootLink);

#190914
Apr 18, 2018 21:59
Vote:
 

@David I doubt that was the problem. IContentRepository actually extends IContentLoader.

Make sure you don't make the same mistake I did: https://vimvq1987.com/beware-icontentloader-getchildren/ 

#191015
Apr 18, 2018 22:10
Vote:
 

@Quan I can't speak to the problem anymore as my post was from almost 2-years ago. :)

#191017
Apr 18, 2018 22:13
Vote:
 

I know, I was replying to David Fewless :) 

As always, it might benefit the future visitors 

#191018
Apr 18, 2018 22:19
* 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.