Get all published language variants of a page

Vote:
 

Hey all 

How do i get only a list of published langauges for a page (langauges that are available to front end user). 

var xxxxx = currentPage as ILocalizable;

xxxxx.ExistingLanguages 

Seems to return me all langauges even language's the page has only been created in although not published. 

currentPage.ExistingLanguages.Select(x => x.Name); is the same. 

Thanks

Minesh 

#219687
Apr 01, 2020 15:47
Vote:
 

Hi,

Can you please try this-

var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
var startPage = contentLoader.Get<StartPage>(ContentReference.StartPage);
var published = contentLoader.GetChildren<PageData>(startPage.ContentLink)
                                // skip container pages
                                .Filter(new FilterTemplate())
                                // skip unpublished pages
                                .Filter(new FilterPublished())
                                // ...
                                .ToList();

or 

using EPiServer.Filters;

....

PageDataCollection myPages = GetChildren(myParentPage);
new FilterPublished(PagePublishedStatus.Published).Filter(myPages);

https://kaliko.com/blog/get-only-published-pages/

#219692
Edited, Apr 01, 2020 18:45
valdis - Apr 01, 2020 19:17
GetChildren() might not be the most optimal way to get other language branches for the current page. That method is travelling page tree..
Vote:
 

Ive opted going down this route,

            var availablePageLanguages = FilterForVisitor.Filter(pageLanguagesBranches).OfType<PageData>();
            var currentPageLanguages = availablePageLanguages.Select(page => page.Language.Name).ToList();
#219693
Apr 01, 2020 18:49
Ravindra S. Rathore - Apr 01, 2020 19:03
Glad you found the resolution
Vote:
 

Not sure whether there is ready made method for this, but this could be the closest I could get to:

var pages = currentPage
    .ExistingLanguages
    .Where(l => l != currentPage.Language)
    .Select(l => _loader.Get<StartPage>(currentPage.ContentLink.ToReferenceWithoutVersion(), l))
    .Where(p => p.Status == VersionStatus.Published);
#219695
Apr 01, 2020 19:15
Quan Mai - Apr 02, 2020 6:51
huh, StartPage?
valdis - Apr 02, 2020 8:48
don't copy AS-IS ;) just an example from my experiments with StartPage
Vote:
 

An alternative is to use IContentVersionRepository, pseudo code:

var publishedVersions = currentPage.ExistingLanguages.Select(x => _contentVersionRepo.LoadPublished(currentPage.ContentLink, x.Name));

var published = _contentLoader.GetItems(publishedVersions.Select(x => x.ContentLink), new LoaderOptions()).OfType<YourContentType>();

#220531
Apr 02, 2020 6:53
Vote:
 

and depending on your requirements you might need to exclude page in current language (if you are looking only for published pages in other languages).

#220539
Apr 02, 2020 8:49
Vote:
 

@QUAN would your approach perform better than 

            var pageLanguagesBranches = contentRepository.GetLanguageBranches<PageData>(currentPage.ContentLink).ToList();
            var availablePageLanguages = FilterForVisitor.Filter(pageLanguagesBranches).OfType<PageData>();
            var currentPageLanguages = availablePageLanguages.Select(page => page.Language.Name).ToList();
#220543
Apr 02, 2020 9:06
Vote:
 

There is no way to say for sure, unless you profile it. But 

GetLanguageBranches

gets each language branch individually, so if you have many languages, it looks like it will be slower than my suggestion

#220553
Apr 02, 2020 13:16
Minesh Shah (Netcel) - Apr 02, 2020 13:18
Thank you very much
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.