Copied to clipboard

Flag this post as spam?

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


  • Thomas Hansen 96 posts 299 karma points
    Jan 24, 2019 @ 10:43
    Thomas Hansen
    0

    Multi language page - controller

    I have a site with a

    • en
      • widget
    • dk
      • widget

    I have a controller that pulls data from the data node. How do I specify that on .dk it should pull from

    • dk
      • widget??

    right now im using

    var root = contentService.GetById(Umbraco.TypedContentAtRoot().First().Id);
    
    var widgetPage = root.Descendants().Where(x => x.ContentType.Alias.Equals("widgetsPage")).FirstOrDefault();
    

    but that will of course only get data from the first root. I need some how to find the current root i'm on..

    Any suggestions? :)

    Thanks!

  • Garðar Þorsteinsson 92 posts 463 karma points
    Jan 24, 2019 @ 11:28
    Garðar Þorsteinsson
    0

    Hi Thomas.

    Its very important that you never use the Content Service where the end user will access it. It has a terrible performance (always sql, not caching) and you don't need it.

    You would instead do:

    var rootNode = UmbracoContext.PublishedContentRequest.PublishedContent.Site();
    var widgetNode = rootNode.Children.FirstOrDefault(x => x.DocumentTypeAlias == "widgetsPage");
    

    .Site() will always fetch the root node of the page dependent on the node you are on.

    Recommend you check the common pitfalls for Umbraco here: https://our.umbraco.com/documentation/reference/Common-Pitfalls/

  • Thomas Hansen 96 posts 299 karma points
    Jan 24, 2019 @ 11:30
    Thomas Hansen
    0

    Hey :)

    This is in my controller

  • Garðar Þorsteinsson 92 posts 463 karma points
    Jan 24, 2019 @ 11:35
    Garðar Þorsteinsson
    0

    Yes but I guess the controller is accessed from the frontend and it will return data to the site.

    In that case never use the content service unless you know exactly what you are doing.

    You have much better access to the nodes with Umbraco.TypedContent or UmbracoContext.PublishedContentRequest.PublishedContent.

    var root = contentService.GetById(Umbraco.TypedContentAtRoot().First().Id);
    

    For example in this line you already have access to the IPublishedContent version of the node with this:

    Umbraco.TypedContentAtRoot().First()
    

    There is no reason to fetch it again with the content service.

  • Thomas Hansen 96 posts 299 karma points
    Jan 24, 2019 @ 11:38
    Thomas Hansen
    0

    Are okay thanks :)!

    Do you know how to get the root of the specific culture page?

  • Garðar Þorsteinsson 92 posts 463 karma points
    Jan 24, 2019 @ 11:42
    Garðar Þorsteinsson
    0

    Depends on where you are trying to access it.

    Do you for example only have the culture name?

    Or are you on a sub page and need to get the correct root page ?

    UmbracoContext.PublishedContentRequest.PublishedContent.Site();
    

    This code will fetch the correct root page.

    • DK
      • Page you are on
    • EN
      • Another Page

    Will fetch DK

  • Thomas Hansen 96 posts 299 karma points
    Jan 24, 2019 @ 11:48
    Thomas Hansen
    0

    from www.test.dk I would like to fetch data from

    • dk
      • widget

    and from www.test.com I would like to fetch data from

    • en
      • widget

    from the controller :)

    with that code you sent i'm getting 'Object reference not set to an instance of an object.'

  • Garðar Þorsteinsson 92 posts 463 karma points
    Jan 24, 2019 @ 11:55
    Garðar Þorsteinsson
    0

    How are you calling the action in the controller ?

  • Thomas Hansen 96 posts 299 karma points
    Jan 24, 2019 @ 11:58
    Thomas Hansen
    0

    Right now from postman.

    But will eventually use ajax call to the controller

  • Garðar Þorsteinsson 92 posts 463 karma points
    Jan 24, 2019 @ 12:06
    Garðar Þorsteinsson
    0

    You dont have access to the UmbracoContext when using Postman.

    Could send the current page id you are on to the action.

    var currentNode = Umbraco.TypedContent(nodeId);
    var rootNode = currentNode.Site();
    

    If you dont have access to Umbraco.TypedContent Then you can use

    var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    

    https://our.umbraco.com/documentation/Reference/Querying/UmbracoHelper/

Please Sign in or register to post replies

Write your reply to:

Draft