Copied to clipboard

Flag this post as spam?

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


  • Michael Beever 74 posts 155 karma points
    Oct 12, 2020 @ 09:20
    Michael Beever
    0

    Umbraco 8 Content from other pages

    Hello,

    I am in the process of upgrading from Umbraco 7 to 8.

    In 7 i am using

    var home = Umbraco.Content(1057);
    

    To bring content throughout the whole site, like footer details.

    For some reason this process does not seem to work as expected in Umbraco 8.

    What is the best way to do this now?

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Oct 12, 2020 @ 20:03
    Marc Goodson
    0

    Hi Michael

    I'm wondering what bits are not working for you and I'm thinking that In Umbraco 7

    var home = Umbraco.Content(1057);
    

    will return a 'dynamic' object representation of your homepage with id 1057

    which allows you to read property values like home.pageTitle etc

    However in Umbraco 8 'dynamics' have been removed, and now Umbraco.Content will return a strongly typed IPublishedContent object representing the homepage

    eg

    IPublishedContent home = Umbraco.Content(1057); // and you can use the Value property to read the value of a property! var pageTitle = home.Value

    You can also run Umbraco using Modelsbuilder, https://our.umbraco.com/documentation/reference/templating/modelsbuilder/ and then you can have doc type specific models generated and can write something like

    HomePage home = Umbraco.Content(1057) as HomePage;
    @home.PageTitle
    

    regards

    marc

  • Michael Beever 74 posts 155 karma points
    Oct 13, 2020 @ 06:50
    Michael Beever
    0

    Hi Marc,

    Thank you for the reply.

    I will give it a try.

  • Michael Beever 74 posts 155 karma points
    Oct 13, 2020 @ 08:59
    Michael Beever
    0

    Hi Marc,

    Still not doing what I need.

    Here is my master page code.

     @using Umbraco.Core.PropertyEditors
    @inherits UmbracoViewPage
    
    @{
        Layout = null;
    
        // Get basic design settings from the homepage
    
    //var home = Model.Content.Site().Children.FirstOrDefault();
    
    //var home = (TeesPenHome)Model.Content.Site();
    
    //var home = Umbraco.Content(Guid.Parse("86b2db02-b076-430c-b944-da31dad66a78"));
    
    //var home = Umbraco.Content(1057);
    
    //var page = Umbraco.Content(Guid.Parse("86b2db02-b076-430c-b944-da31dad66a78"));
    
    //var home = Model.Content.AncestorOrSelf("TeesPenHome");
    
    IPublishedContent home = Umbraco.Content(1057);
    
    }
    @* the fun starts here *@
    <!DOCTYPE html>
    <html lang="en">
        <head>
    
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=11">
            <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
            <meta name="description" content="Teesside Pension Fund information on the Local Government Pension schemes">
            <meta name="keywords" content="HTML,CSS,XML,JavaScript">
            <meta name="author" content="XPS Administration">
    
            <title>Teesside Pension Fund</title>
    
        @Html.Partial("TeesPen/TeesHeader") 
    
    <!-- THIS NEEDS TO STAY HERE search box on HOMEPAGE ONLY -->
        <style>
    
        .form-control {
    
        background-color:rgba(248,249,250, 0.6);
        border-top-left-radius:5px;
        border-top-right-radius:5px;
    
    }
    
        .form-control:focus {
        background-color:rgba(248,249,250, 0.8);
            border-top-left-radius:5px;
        border-top-right-radius:5px;
    }
    
        </style>
    <!-- THIS NEEDS TO STAY HERE search box on HOMEPAGE ONLY -->
    
        </head>
        <body>
    
    
    
    @Html.Partial("TeesPen/TopNav") 
    
    
    @RenderBody()
    
                      <footer class="pt-4 my-md-5 pt-md-5 border-top">
                      <div class="container">
        <div class="row">
    
    
         <style>
    
            .hangingindent {
              padding-left: 14px ;
              text-indent: -8px ;
            }
    
         </style>
    
    
        <div class="col-6 col-md">
        <a href="index.html"><img src="/media/1440/xps_logo.png" alt="" width="50%" ></a>
    
                <ul style="list-style:none;padding-inline-start: 0px;padding-top:10px;padding-left:10px;">
                  <li class="text-muted hangingindent"><i class="fas fa-map-marked-alt fa-1x"></i> @home.FooterAddress
     </li>
    
    
    
                 <li class="text-muted" ><i class="fas fa-phone-alt fa-1x"></i><a class="text-muted" href="tel:@home.FooterPhone"> @home.FooterPhone</a></li>
                 <li class="text-muted" ><i class="far fa-envelope fa-1x"></i><a class="text-muted" href="mailto:@home.FooterEmail"> @home.FooterEmail</a></li>
                </ul>
    
                            <div class="text-center">
    
    
                            <a href="https://www.facebook.com/TeessidePensionFund" class="farCircle"><span class="fa-stack" style="vertical-align: top;">
                              <i class="far fa-circle fa-stack-2x" ></i>
                              <i class="fab fa-facebook-f fa-stack-1x" style="color:#3B5998;"></i>
                            </span></a>
                            <a href="https://www.youtube.com/channel/UCxLigi_nbLGis3AkQOC_ttw" class="farCircle"><span class="fa-stack" style="vertical-align: top;">
                              <i class="far fa-circle fa-stack-2x"></i>
                              <i class="fab fa-youtube fa-stack-1x" style="color:#bb0000;"></i>
                            </span></a>
                            <a href="#" class="farCircle"><span class="fa-stack" style="vertical-align: top;">
                              <i class="far fa-circle fa-stack-2x" ></i>
                              <i class="fab fa-linkedin fa-stack-1x" style="color:#0077B5;"></i>
                            </span></a>
    
    
                            </div>
    
              </div>
    
    
    
          <div class="col-6 col-md">
            <h5>Most Visited Pages</h5>
            <ul class="list-unstyled text-small" style="padding-left:10px;">
    @{
    
            var typedMultiMediaPicker1 = home.PopularLinks;
            foreach (var item1 in typedMultiMediaPicker1)
            {
    
                if(item1.Target == "_blank")
                {
                    <li><a href="@item1.Url" class="text-muted" target="@item1.Target">@item1.Name <i class="fas fa-external-link-alt"></i></a></li>
                }
                else
                {
                    <li><a href="@item1.Url" class="text-muted" target="@item1.Target">@item1.Name</a></li>
                }
    
    
    
            }
    
    
                }
            </ul>
          </div>
          <div class="col-6 col-md">
            <h5>Resources</h5>
    <ul class="list-unstyled text-small" style="padding-left:10px;">
    
    
    @{
    
            var typedMultiMediaPicker2 = home.ResourcesLinks;
            foreach (var item2 in typedMultiMediaPicker2)
            {
    
                if(item2.Target == "_blank")
                {
                    <li><a href="@item2.Url" class="text-muted" target="@item2.Target">@item2.Name <i class="fas fa-external-link-alt"></i></a></li>
                }
                else
                {
                    <li><a href="@item2.Url" class="text-muted" target="@item2.Target">@item2.Name</a></li>
                }
    
    
    
            }
    
    
                }</ul>
          </div>
          <div class="col-6 col-md">
            <h5>About</h5>
            <ul class="list-unstyled text-small" style="padding-left:10px;">
    @{
    
            var typedMultiMediaPicker3 = home.AboutUsLinks;
            foreach (var item3 in typedMultiMediaPicker3)
            {
    
                if(item3.Target == "_blank")
                {
                    <li><a href="@item3.Url" class="text-muted" target="@item3.Target">@item3.Name <i class="fas fa-external-link-alt"></i></a></li>
                }
                else
                {
                    <li><a href="@item3.Url" class="text-muted" target="@item3.Target">@item3.Name</a></li>
                }
    
    
    
            }
    
    
                }
            </ul>
          </div>
    
          </div>
    
          <div class="col-md text-center">
    
                    <small class="d-block mb-3 text-muted">&copy; XPS <script>new Date().getFullYear()>2010&&document.write(" "+new Date().getFullYear());</script></small>
    
    
          </div>
    
        </div>
      </footer>
    
    
    
        </div>
    @{
    
        if(home.CookieConsent == true)
        {
    
        @Html.Partial("TeesPen/CookieConsent")
    
        }
    
    }
    
            @{
    
                if(@home.FloatingMenuButton == true)
                {
                    @Html.Partial("TeesPen/FloatingMenu")
                }
    
            }
    
    </body>
    
    </html>
    

    Thanks

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Oct 13, 2020 @ 13:04
    Marc Goodson
    0

    Hi Michael

    You are writing out properties from the 'home' node you are retrieving using 'dynamic' eg

    @home.FooterAddress

    But dynamics has been removed in V8

    Now Umbraco.Content returns an instances of IPublishedContent

    To read a property from IPublishedContent you would need to do this instead:

    @home.Value("footerAddress")
    

    Instead of @home.FooterAddress

    So I suspect the errors you are seeing are not connected to retrieving the homepage via Umbraco.Content but instead trying to write out these properties in a dynamic format.

    If you update all of these instances that use dynamic approach to use .Value("alias") approach, do you find that your template renders correctly?

    If you are not keen on the .Value approach then the alternative is to setup ModelsBuilder on your site to generate a poco c# class representation of the DocumentType that would then allow you to have

    @inherits UmbracoViewPage<MyHomePageDocType>
    

    and allow you to write

    @Model.FooterAddress
    

    but you'll need to configure Modelsbuilder, as per the documentation I linked to in my first reply!

    regards

    marc

Please Sign in or register to post replies

Write your reply to:

Draft