Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Get content that is available in the currently selected language?

Vote:
 

We have a block that pulls in content and displays it on our page. Right now, no matter what language I select in our language switcher, this is not being honored by our block.

As I am new to EpiServer and C# I think I have identified the following line of code as being the one that pulls in the data?

ServiceLocator.Current.GetInstance<IContentLoader>();

Can I rewrite this to only get content in the language that is currently selected by the user?

Thank you for any help.

#201188
Feb 08, 2019 11:15
Vote:
 

Check out this

https://world.episerver.com/documentation/Class-library/?documentId=cms/11/A8B05F15 

T Get<T>(
	ContentReference contentLink,
	CultureInfo language
)
#201190
Feb 08, 2019 11:41
Vote:
 

Thanks I found that earlier while searching, but I am unsure with how to write the code.

#201191
Feb 08, 2019 11:44
Vote:
 

You can start with this https://world.episerver.com/documentation/developer-guides/CMS/Content/icontentrepository-and-datafactory/

Even better, take a Developer course with Episerver training 

#201192
Feb 08, 2019 11:50
Vote:
 

That's so unhelpful. Thank you.

#201193
Feb 08, 2019 11:51
Vote:
 

epiNew,

Why is it unhelpful to suggest that you take developer training course?

#201210
Feb 08, 2019 17:58
Vote:
 

TBH what Quan provided is the best that can be found around internet.

#201615
Feb 25, 2019 3:55
Vote:
 

I would like to provide feedback to the community:

We solved it by adding a filter to the find function. The code included a seach carried out by Episerver Find which was pulling in content of a specific category. But instead of honoring the current language it was pulling in everything.

To fix it, we changed this:

search = search.Filter(sp => sp.IsDeleted.Match(false))
                .OrderByDescending(sp => sp.PublishingDate, SortMissing.Last)
                .Take(1000);

To this:

search = search.Filter(sp => sp.IsDeleted.Match(false))
                .OrderByDescending(sp => sp.PublishingDate, SortMissing.Last)
                .Filter(sp => sp.Language.Name.Match(ContentLanguage.PreferredCulture.Name))
                .Take(1000);

So now we are pulling only content that matches the preferred culture.

#201814
Mar 04, 2019 11:02
Vote:
 

If you are searching for IContentData you should only have to apply the .FilterForVisitor() filter. It's doing all filtering necessary for current visitor, eg access rights, published content, current language.

search = search.FilterForVisitor()
               .OrderByDescending(sp => sp.PublishingDate, SortMissing.Last)
               .Take(1000);
#201820
Mar 04, 2019 14:09
Vote:
 

Thanks for the tip, .FilterForVisitor() has exactly what's needed and would make the code far leaner. 

#201822
Mar 04, 2019 16:11
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.