Umbraco Razor Quick Reference

This is a quick reference for the various properties and functions available when developing Razor Views for Umbraco. A very good way to use this, is to get the Dash app and add this as a docset (using the Dash Feed link below).

Every property/function can be linked to directly, as they're just anchorlinks, which is super-handy when trying to explain something on a forum or similar…

Dash Feed

IPublishedContent

These are all the "native" node properties

CreateDate

.CreateDateDateTime

CreatorID

.CreatorIDint

CreatorName

.CreatorNamestring

DocumentTypeAlias

.DocumentTypeAliasstring

Returns the alias of the Document Type used by this content item.

GetPropertyValue()

.GetPropertyValue(string propertyAlias)object
.GetPropertyValue(string propertyAlias, bool recursive)object
.GetPropertyValue(string propertyAlias, string defaultValue)object
.GetPropertyValue(string propertyAlias, object defaultValue)object
.GetPropertyValue(string propertyAlias, bool recursive, object defaultValue)object

Outputs the value of the specified property. If the recursive argument is true, the value is taken from the first ancestor (or the node itself) that has a value for the property.

Example


<title>@Model.GetPropertyValue("pageTitle", Model.Name)</title>

GetPropertyValue<T>()

.GetPropertyValue<T>(string propertyAlias)T
.GetPropertyValue<T>(string propertyAlias, T defaultValue)T
.GetPropertyValue<T>(string propertyAlias, bool recursive)T
.GetPropertyValue<T>(string propertyAlias, bool recursive, T defaultValue)T

Returns the value of the specified property as the type T.

Example


var picker = Model.GetPropertyValue<IEnumerable<IPublishedContent>>("panels");

GetTemplateAlias()

.GetTemplateAlias()string

The alias of the assigned template (if any).

Id

.Idint

The id of the node in Umbraco

Level

.Levelint

Name

.Namestring

Parent

.Parentnode

A reference to the node's parent

Path

.Pathstring

A CSV string of the ids of the nodes in the ancestor axis, e.g.: "-1,1060,2590"

SortOrder

.SortOrderint

TemplateId

.TemplateIdint

If a template has been assigned, its id is returned here.

UpdateDate

.UpdateDateDateTime

Url

.Urlstring

The absolute URL (e.g. /my/page/about-url/) of the content item. Whenever you need to link to a page, this is what you should use.

Example


@foreach (var item in Model.Children) {
	<a href="@item.Url">@item.Name</a>
}

If the config setting <useDomainPrefixes> in umbracoSetting.config is true, this will return a full URL with protocol, hostname & port etc.

UrlName

.UrlNamestring

A URL-safe version of the node's name, e.g. for a document named About Us the UrlName property would be generated as about-us.

Also good for anchor links to various sections on a page (see code example)

Example


<!-- Get a meaningful ID that can be linked to -->
<section id="@Model.UrlName">
	<h2>@Model.Name</h2>
	...
</section>
...
<a href="#@Model.UrlName">See the @Model.Name section above</a>

WriterID

.WriterIDint

WriterName

.WriterNamestring

Collections

Ancestors()

.Ancestors()collection
.Ancestors(int level)collection
.Ancestors(string nodeTypeAlias)collection

AncestorsOrSelf()

.AncestorsOrSelf()collection
.AncestorsOrSelf(int level)collection
.AncestorsOrSelf(string nodeTypeAlias)collection

Children

.Childrencollection

Gets the children of the node, sorted by their SortOrder.

Children()

.Children()collection
.Children(alias, alias [, alias …])collection
.Children(lambda predicate)collection

Gets the children of the node, sorted by their SortOrder.

Using this with no arguments is the same as using the Children property. Just included for consistency.

Children<T>()

.Children<T>()collection

Returns the children of the specific type T.

Example


var newsItems = Model.Children<NewsItem>();

ChildrenAsList

.ChildrenAsList

Descendants()

.Descendants()
.Descendants(int level)
.Descendants(string nodeTypeAlias)

DescendantsOrSelf()

.DescendantsOrSelf()
.DescendantsOrSelf(int level)
.DescendantsOrSelf(string nodeTypeAlias)

IsHelpers

The IsHelper methods are a set of extension methods for IPublishedContent to help perform quick conditional queries against IPublishedContent nodes in a collection. They can all return a simple boolean, or one of two supplied strings, depending on the result of the comparison. Leaving the valueIfFalse argument out is equivalent to supplying an empty string, which is what will be returned if the condition evaluates to false.

IsAncestor()

.IsAncestor(node otherNode)bool
.IsAncestor(node otherNode, string valueIfTrue, [string valueIfFalse])string

Test if the current node in the iteration is an ancestor of another node

IsAncestorOrSelf()

.IsAncestorOrSelf(node otherNode)bool
.IsAncestorOrSelf(node otherNode, string valueIfTrue, [string valueIfFalse])string

Test if the current node in the iteration is the same as, or an ancestor of, another node

IsDescendant()

.IsDescendant(node otherNode)bool
.IsDescendant(node otherNode, string valueIfTrue, [string valueIfFalse])string

Test if the current node in the iteration is a descendant of another node

IsDescendantOrSelf()

.IsDescendantOrSelf(node otherNode)bool
.IsDescendantOrSelf(node otherNode, string valueIfTrue, [string valueIfFalse])string

Test if the current node in the iteration is the same as, or a descendant of, another node

IsEqual()

.IsEqual(node otherNode)bool
.IsEqual(node otherNode, string valueIfTrue, [string valueIfFalse])string

Test if the current node in the iteration is equal (by Id) to another node

IsEven()

.IsEven()bool
.IsEven(string valueIfTrue, [string valueIfFalse])string

Test if the current node's position is even

IsFirst()

.IsFirst()bool
.IsFirst(string valueIfTrue, [string valueIfFalse])string

Test if the current node is first in the collection.

IsLast()

.IsLast()bool
.IsLast(string valueIfTrue, [string valueIfFalse])string

Test if the current node is last in the collection

IsModZero()

.IsModZero(int modulus)bool
.IsModZero(int modulus, string valueIfTrue, [string valueIfFalse])string

Test if the current node's position is evenly divisible by a given number

IsNotFirst()

.IsNotFirst()bool
.IsNotFirst(string valueIfTrue, [string valueIfFalse])string

Test if the current node is not the first in the collection

IsNotLast()

.IsNotLast()bool
.IsNotLast(string valueIfTrue, [string valueIfFalse])string

Test if the current node is not the last item in the collection

IsNotModZero()

.IsNotModZero(int modulus)bool
.IsNotModZero(int modulus, string valueIfTrue, [string valueIfFalse])string

Test if the current node's position is not evenly divisible by a given number

IsNotPosition()

.IsNotPosition(int index)
.IsNotPosition(int index, string valueIfTrue, [string valueIfFalse])

Test if the current node is not at the specified index in the collection

IsOdd()

.IsOdd()bool
.IsOdd(string valueIfTrue, [string valueIfFalse])string

Test if the current node's position is odd

IsPosition()

.IsPosition(int index)bool
.IsPosition(int index, string valueIfTrue, [string valueIfFalse])string

Test if the current node is at the specified index in the collection

Traversing

These are used for walking up/down the tree from any other node you might be on. They all return a single IPublishedContent node.

Parent

.Parentnode

A reference to the node's parent

AncestorOrSelf()

.AncestorOrSelf()node
.AncestorOrSelf(lambda func)node
.AncestorOrSelf(int level)node
.AncestorOrSelf(string nodeTypeAlias)node

Down()

.Down()node
.Down(int number)node

Obsolete - use Descendants() instead.

First()

.First()node

FirstChild()

.FirstChild([string alias])
.FirstChild(lambda predicate)

FirstChild<T>()

.FirstChild<T>([lambda func])

FirstOrDefault()

.FirstOrDefault()node

FollowingSibling()

.FollowingSibling()node
.FollowingSibling(int number)node
.FollowingSibling(string nodeTypeAlias)node

Last()

.Last()node

LastOrDefault()

.LastOrDefault()node

Next()

.Next()node
.Next(int number)node

Obsolete - use FollowingSibling() instead.

Parent<T>()

.Parent<T>()T

Returns the parent if it has the specific type T, otherwise null.

PrecedingSibling()

.PrecedingSibling()node
.PrecedingSibling(int number)node
.PrecedingSibling(string nodeTypeAlias)node

Previous()

.Previous()node
.Previous(int number)node

Obsolete - use PrecedingSibling() instead.

Sibling()

.Sibling(int number)node
.Sibling(string nodeTypeAlias)node

Obsolete - use FollowingSibling() or PrecedingSibling() instead.

Single()

.Single()node

SingleOrDefault()

.SingleOrDefault()node

Up()

.Up()node
.Up(int number)node

Obsolete - use Ancestors() instead.

Miscellaneous

GetCulture()v7.2.7+

.GetCulture([uri current])System.Globalization.CultureInfo

Gets the culture that would be used to render the content, within the context of a specified current request.

Example


@{
	var siteRoot = Model.Site();
}
<html lang="@siteRoot.GetCulture()">
...
</html>

HasProperty()

.HasProperty(string propertyAlias)bool

Returns a boolean value representing if the IPublishedContent has a property with the specified alias.

HasValue()

.HasValue(string propertyAlias)bool
.HasValue(string propertyAlias, bool recursive)bool
.HasValue(string propertyAlias, bool recursive, string valueIfTrue, [string valueIfFalse])html

Returns a boolean value representing if the IPublishedContent property has had a value set.

IsNull()

.IsNull(string propertyAlias)bool
.IsNull(string propertyAlias, bool recursive)bool

Returns a boolean value representing if the IPublishedContent property is Null.

IsVisible()

.IsVisible()bool

Example


@foreach (var item in Model.Children.Where(x => x.IsVisible())) {
	...
}

This function requires a custom property with the alias umbracoNaviHide to work!

Site()

.Site()node

Gets the ancestor at level 1

Example


var siteRoot = Model.Site();
// Same as:
var siteRoot = Model.AncestorOrSelf(1);

In v8 this will be renamed to .Root()

Filter & Order

When collecting nodes for a navigation or a listview, these come in very handy. They all work on and return a collection, so can can be chained for very effective results.

DistinctBy()

.DistinctBy(lambda func)collection

GroupBy()

.GroupBy(lambda func)collection

InGroupsOf()v7.2.1+

.InGroupsOf(int number)collection

OrderBy()

.OrderBy(lambda func)collection

OrderByDescending()

.OrderByDescending(lambda func)collection

RandomOrder()

.RandomOrder()collection

Randomizes a collection's items. Combine with Take() or FirstOrDefault() to get a random piece of content to display somewhere.

Example


// Pick a random widget to display
var widgets = Model.Children<Widget>().RandomOrder().FirstOrDefault();

Skip()

.Skip(int number)collection

SkipWhile()

.SkipWhile(lambda func)collection

Take()

.Take(int number)collection

TakeWhile()

.TakeWhile(lambda func)collection

Where()

.Where(lambda func)collection

Provides a way to filter a collection of nodes, by using a lambda function that returns true if the node should be included and false if not.

Example


var breaking = Model.Children<NewsItem>().Where(x => x.Category == "breaking");

Aggregate Functions

Average()

.Average(lambda func)number

Count()

.Count()int

Max()

.Max(lambda func)number

Min()

.Min(lambda func)number

Sum()

.Sum(lambda func)number

UmbracoHelper

These are available on the @Umbraco object inside the template.

Coalesce()

@Umbraco.Coalesce(value, value [, value …])string

Returns the first non-null value in the list of arguments.

Concatenate()

@Umbraco.Concatenate(value, value [, value …])string

Returns all of its arguments concatenated into a single string.

CreateHash()

@Umbraco.CreateHash(string text)string

Generates a hash based on the text string passed in. This method will detect the security requirements (is FIPS enabled) and return an appropriate hash.

GetDictionaryValue()

@Umbraco.GetDictionaryValue(string key, [string altText])string

Returns the dictionary value for the specified key. You can specify an altText fallback value to be returned if the dictionary has no value for key.

Example


<a href="#top">@Umbraco.GetDictionaryValue("TopLabel")</a>

If()

@Umbraco.If(bool test, string valueIfTrue, [string valueIfFalse])string

Returns one of two values depending on the result of a test.

Join()

@Umbraco.Join(string seperator, value, value [, value …])string

Returns all of its arguments joined with a specific separator between each of them.

ReplaceLineBreaksForHtml()

@Umbraco.ReplaceLineBreaksForHtml(string text)string

Replace linebreaks with HTML <br /> tags.

StripHTML()

@Umbraco.StripHTML(string html, [tag, tag [, tag …]])html

Strips HTML tags from a given string, leaving the contents of the tags in place. If no tags are specified, all HTML tags are stripped.

Truncate()

@Umbraco.Truncate(string html, int length, [bool addEllipsis], [bool treatTagsAsContent])

Truncates a string to a given length and can add an ellipsis (…) at the end. The method checks for open HTML tags, and makes sure to close them.

TruncateByWords()

@Umbraco.TruncateByWords(string html, int words, [bool addEllipsis])html

Truncates a string to a given amount of words and can add an ellipsis (…) at the end. The method checks for open HTML tags, and makes sure to close them.

Fork me on GitHub