How do I determine the current pages document type in umbraco?

29,173

Solution 1

think you do actually need to create a node each time when you are on the page to access the pages properties like nodetypealias and stuff, try this i have the same kind of functionality on my site, http://rdmonline.co.uk/ but in the side menu where depending on the page/section it shows a diff menu links.

    @{
        var currentPageID = Model.Id;
        var currentPageNode = Library.NodeById(currentPageID);

        if (currentPageNode.NodeTypeAlias == "someDocTypeAliasHere")
          {
             //Render the macro
          }
          else
          {
             // Render the macro only if the tick box is checked
          }
     }

Let me know if this works for you.

Solution 2

In Umbraco 7 use currentPageNode.DocumentTypeAlias

Solution 3

In Umbraco 7.1 I use: @if (@CurrentPage.DocumentTypeAlias == "NewsItem")

Solution 4

This is a bit unrelated to this post, but searching Google brought me to this post, so I thought I'd share in case anoyne else is dealing with this issue: In Umbraco 7, to get all content in the site for a specific type:

var articles = CurrentPage.AncestorOrSelf(1).Descendants()
                   .Where("DocumentTypeAlias == \"BlogPost\"").OrderBy("CreateDate desc");
Share:
29,173
shawty
Author by

shawty

Whats to say... well I'm a geek, I've always been a geek and I'll always be a geek. I love writing software and tinkering with computer related stuff in general. I run my own I.T consultancy company called "Digital Solutions Computer Software UK", I'm a Pluralsight author and the UK & Europe group manager for Lidnug (One of the largest .NET related user groups on the Linked-In network) I'm a Microsoft Community Leader, presenter & lecturer of development related topics, oh and a Write a few computer books here and there for things like the Syncfusion Succinctly range. I work across Linux, Unix, Windows and use lots of different languages in order to ply my trade, I've been working in I.T now for the best part of 30 years and will probably be here for at least another 30. If you want to find out more about me, you can visit my Linked Profile here: http://uk.linkedin.com/in/petershaw08/ Or my personal blog here: http://shawtyds.wordpress.com/

Updated on July 09, 2022

Comments

  • shawty
    shawty almost 2 years

    I have what I feel is a very simple question about Umbraco, but one that has as of yet no apparent answer.

    I have a razor template, standard stuff, with @ displaying variables and some inline C# code.

    At one point in the template I use:

    @Umbraco.RenderMacro("myCustomMacro");
    

    no problems there, everything works as expected.

    Now, this macro is inserted on every page (it's in the master template) but I have a page property that allows the content authors to turn it on and off via a check box in the page properties, again so far so good everything works perfectly.

    However I now find that for a certain "document type" this component MUST be displayed, so I've been trying to find a way to perform that check.

    Now in my mind, this should be as simple as doing something like this:

    @{
      if(CurrentPage.documentType == "someDocTypeAliasHere")
      {
         //Render the macro
      }
      else
      {
         // Render the macro only if the tick box is checked
      }
     }
    

    as I say, this is (or I believe it should be anyway) a very simple operation, but one that so far does not seem to have a result.

    What Have I tried so far?

    Well apart from reading every page on our-umbraco that mentions anything to do with razor & the @CurrentPage variable, Iv'e been through the razor properties cheat sheet, and tried what would appear to be the most common properties including (In no specific order):

    @CurrentPage.NodeTypeAlias
    @CurrentPage.NodeType
    @CurrentPage.ContentType
    @CurrentPage.DocumentType
    

    and various letter case combinations of those, plus some others that looked like they might fit the bill.

    Consistently the properties either don't exist or are empty so have no useable information in them to help determine the result.

    So now after a couple of days of going round in circles, and not getting anywhere I find myself here..

    (Please note: this is not a search the XSLT question, or iterate a child collection or anything like that, so any requests to post XSLT, Macros, Page templates or anything like that will be refused, all I need to do is find a way to determine the Document Type of the current page being rendered.)

    Cheers

    Shawty

    PS: Forgot to mention, I'm using

    umbraco v 4.11.8 (Assembly version: 1.0.4869.17899)

    Just in case anyone asks.