November Happy Hour will be moved to Thursday December 5th.

how to retrieve categories on view page?

Vote:
 

In CMS 12, how to retrieve categories in CMS 12. 

When I use this existing code:

 var categoryRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<CategoryRepository>();
 var filterBlock = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<FilterBlock>();

It's causing errors..please advise.

Thanks

#294479
Jan 11, 2023 8:09
Vote:
 

What is the error you are facing, I have just tried inside the View for an Article Page on the Alloy Templates and all seems to work 

#294570
Jan 12, 2023 14:36
Vote:
 

I realized the problem was not in the code with categoryRepository but the next line of code:

 var filterBlock = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<FilterBlock>();

This was causing this error: 

Exception thrown: 'System.InvalidOperationException' in Microsoft.Extensions.DependencyInjection.Abstractions.dll

I have removed this in code, and it works fine, but what is the substitute for that in .NET Core

Thanks

#294573
Jan 12, 2023 16:10
Vote:
 

Im not sure if you are in a view or the block itself 

If you are trying to get the Content Type of a particular Block and know its ContentId I would use IContentRepository 

e.g. 

this.contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
// Could cause null reference exception so might be worth going down the .TryGet route 
var filterBlock= contentRepository.Get<FilterBlock>(x.ContentLink);

#294613
Jan 13, 2023 0:48
* 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.