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

5

11

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.

Kalle answered 26/5, 2013 at 14:24 Comment(0)
C
9

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.

Clarettaclarette answered 27/5, 2013 at 19:20 Comment(2)
Thanks denford, I shall give that a try and get back to you.Kalle
Turns out that solved my problem, although not directly. I kept looking in 'CurrentPage' which is defined in a macro (That is it's not null) however, If I just go directly to the model, everything I needed was in there. All I had to do was check "@Model.NodeTypeAlias" and I was golden. Hat's off to you sir...Kalle
M
21

In Umbraco 7 use currentPageNode.DocumentTypeAlias

Murguia answered 29/7, 2014 at 13:51 Comment(3)
This is true for 6.x as well (i.e. using DocumentTypeAlias instead of NodeTypeAlias).Harar
I need to get my butt into gear and actually upgrade to V7 :-)Kalle
Tested in 7.2.6. Did not work, currentPageNode doesn't exist according to the compiler. CurrentPage.DocumentTypeAlias did work, however.Majors
H
13

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

Hinterland answered 19/12, 2014 at 12:11 Comment(1)
CurrentPage.DocumentTypeAlias worked for me in 7.2.6. Note that Reles' example has an extra @ symbol before CurrentPage.Majors
C
9

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.

Clarettaclarette answered 27/5, 2013 at 19:20 Comment(2)
Thanks denford, I shall give that a try and get back to you.Kalle
Turns out that solved my problem, although not directly. I kept looking in 'CurrentPage' which is defined in a macro (That is it's not null) however, If I just go directly to the model, everything I needed was in there. All I had to do was check "@Model.NodeTypeAlias" and I was golden. Hat's off to you sir...Kalle
C
1

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");
Circumvallate answered 19/1, 2015 at 9:3 Comment(1)
Thanks much appreciated. I've not dug into Umb7 yet, but I understand there are some major changes.Kalle
W
0

If your razor view inherits from Umbraco.Web.Mvc.UmbracoViewPage, you could also use UmbracoHelper:

@if (UmbracoHelper.AssignedContentItem.DocumentTypeAlias.Equals("NewsItem")) { ... }

Querying for a specific DocumentType is also easy:

UmbracoHelper.AssignedContentItem.Descendants("NewsItem")

This code will recursively return the list of IPublishedContent nodes. If you wish to use this list with your specific DocumentType information, these items would have to be mapped to the specific type. Other than that, IPublishedContent gives you the basic information for the nodes.

I've later saw that you have been using an older version of Umbraco. :) This implementation is only for v7.

Welford answered 17/11, 2015 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.