Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • ivan 35 posts 176 karma points
    Apr 29, 2021 @ 10:06
    ivan
    0

    Get All published content by culture - Umbraco 8

    Hello everyone, I'm trying to get all the published content by culture/language in umbraco 8 but still I'm not getting it the right way.

    I am using this code:

            var allPublishedContent = new List<IPublishedContent>();
    
            foreach (var publishedContentRoot in Umbraco.ContentAtRoot().Where(e => e.Cultures.ContainsKey("en-us")))
            {
                allPublishedContent.AddRange(publishedContentRoot.DescendantsOrSelf());
            }
    

    Thanks and have a great day everyone!

  • Daniel Pazos 12 posts 123 karma points
    Apr 29, 2021 @ 19:21
    Daniel Pazos
    0

    Hi,

    I've tried you code in my project and, at least in my case, Cultures is a Two-Letter Code.

    Have you tried this?

    foreach (var publishedContentRoot in Umbraco.ContentAtRoot().Where(e => e.Cultures.ContainsKey("en")))
    

    If it still not working try to debug your code and set a breakpoint to check what are you culture keys.

    In case you don't know how to debug your code in Visual Studio, run your project as usual and in the menu option Debug -> Attach to process look for the IISexpress process.

    enter image description here

    Add this line and set a breakpoint on it:

    var contents = Umbraco.ContentAtRoot();
    

    And see what values you have on the Cultures property.

    Hope this helps.

  • ivan 35 posts 176 karma points
    Apr 30, 2021 @ 17:13
    ivan
    101

    Hello Daniel,

    Appreciate your reply. Already resolved my problem by doing this

    private readonly IVariationContextAccessor _variationContextAccessor;
    
    public test(IVariationContextAccessor variationContextAccessor)
    {       
        _variationContextAccessor = variationContextAccessor;
    }
    
    foreach (var clture in Umbraco.ContentAtRoot().First().Cultures)
    {   
    
        _variationContextAccessor.VariationContext = new VariationContext(clture.Key);
    
        var allPublishedContent = new List<IPublishedContent>();        
    
        foreach (var publishedContentRoot in Umbraco.ContentAtRoot())
        {
            allPublishedContent.AddRange(publishedContentRoot.DescendantsOrSelf());
        }
    }
    

    Reference Link here: https://our.umbraco.com/Documentation/Reference/Language-Variation/

Please Sign in or register to post replies

Write your reply to:

Draft