Copied to clipboard

Flag this post as spam?

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


  • blackhawk 313 posts 1368 karma points
    Jan 25, 2018 @ 16:53
    blackhawk
    0

    How can we access document type properties directly on Master.cshtml?

    This is probably a simple no answer, but I'm curious anyway....

    Under Settings in Backoffice, I created a document type called 'Site Settings'. It is not tied to a template. I have several properties applied to it. Is it possible to access properties directly on Master.cshtml?

    For example, I would like to do something like this directly on Master.cshtml...

    if (Model.HasValue("globalHeadScripts"))
      { 
      <script>
           @Html.Raw(Model.GlobalHeadScripts);
      </script>
      }
    

    globalHeadScripts is a property on my Site Settings document type. Is this process possible?

    My end goal is to do something like this....

    + My site: (this root node is tied to a document type called 'My Site', which is inheriting a composition called 'Site Settings'. I want Master.cshtml to access 'Site Settings' properties. )
        + Home: (child content node of My Site. I will modify its 'Cultures and Hostnames' to be available as the default site when Umbraco loads.)
        + About:
        + Contact us
        + etc....
    

    Thanks!

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Jan 25, 2018 @ 17:05
    Alex Skrypnyk
    100

    Hi Blackhawk

    Yes, it's possible

    You have to get Settings node and then something like that:

    var settingsNode = Umbraco.TypedContent(settingsNodeId);
    
    if (settingsNode.HasValue("globalHeadScripts"))
    { 
        <script>
            @Html.Raw(settingsNode.GetPropertyValue("GlobalHeadScripts"));
        </script>
    }
    
  • Nigel Wilson 944 posts 2076 karma points
    Jan 25, 2018 @ 17:14
    Nigel Wilson
    0

    Hey

    Probably should also include "HasProperty" before checking for a value.

    if (Model.HasProperty("globalHeadScripts") && Model.HasValue("globalHeadScripts")) {
    //do stuff
    }
    

    Cheers

    Nigel

  • blackhawk 313 posts 1368 karma points
    Jan 25, 2018 @ 17:29
    blackhawk
    0

    I'm doing the following...

    @{
    
     @*The global script for all pages.*@
    var settingsNode = Umbraco.TypedContent(1080);
    if (settingsNode.HasProperty("globalHeadScripts") && settingsNode.HasValue("globalHeadScripts"))
    {   
    <script>
         @Html.Raw(settingsNode.GetPropertyValue("globalHeadScripts"));
    </script>
        }
    }
    

    ...But I'm getting an error...

    Object reference not set to an instance of an object.
    
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
        Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    

    There are no errors when I build my project out of visual studio.

    My using statements at the top of my page looks like this...

    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @using Umbraco.Core
    @using Umbraco.Core.Models
    @using Umbraco.Core.Services
    @inherits UmbracoViewPage
    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    and my document type looks like this...

    enter image description here

  • Nigel Wilson 944 posts 2076 karma points
    Jan 25, 2018 @ 17:34
    Nigel Wilson
    0

    If debugging, do you get a node object when setting settingsNode ?

  • blackhawk 313 posts 1368 karma points
    Jan 25, 2018 @ 18:03
    blackhawk
    0

    I see what I was doing wrong.. I was trying to get the document ID into Umbraco.TypedContent(), when I should of been referencing an id from the content node that is using 'Site Settings' All is good now!

    Many many thanks!

Please Sign in or register to post replies

Write your reply to:

Draft