UmbracoHelper
Using the Umbraco Helper
UmbracoHelper is the unified way to work with published content/media on your website. You can use the UmbracoHelper to query/traverse Umbraco published data.
UmbracoHelper also has a variety of helper methods that are useful when working in your views and controllers.
How to reference UmbracoHelper
If you are using Views or Partial View Macros you can reference UmbracoHelper with the syntax: @Umbraco
If you need an UmbracoHelper
in your own controllers, you need to inject an instance.
Example of getting UmbracoHelper
in a controller:
If you need to use an UmbracoHelper in a service with a singleton lifetime you would instead need to make use of the IUmbracoHelperAccessor interface to obtain a temporary reference to an instance.
IPublishedContent
UmbracoHelper will expose all content in the form of IPublishedContent
. To get a reference to the currently executing content item from the UmbracoHelper, use UmbracoHelper.AssignedContentItem
.
The samples below demonstrate using UmbracoHelper
in Razor. Working with the UmbracoHelper
will be the same in controllers, except for the fact that you must resolve it with IUmbracoHelperAccessor
like shown above.
Working with Content
.Content(Guid id)
Given a node ID, returns a IPublishedContent
.ContentAtRoot()
Returns a collection of IPublishedContent
objects from the Content tree.
.ContentAtXPath(string xpath)
Queries the cache for content matching a given XPath query and returns a collection of IPublishedContent
objects.
.ContentSingleAtXPath(string xpath)
Queries the cache for content matching a given XPath query and returns the first match as an IPublishedContent
object.
Working with Media
.Media(Guid id)
Given a node ID, returns an IPublishedContent
Media entity
.MediaAtRoot()
Returns a collection of IPublishedContent
objects from the Media tree.
Working with Tags
Working with Members
Searching
Fetching Dictionary Values
.GetDictionaryValue(string key)
Returns a dictionary value(string
) for the key specified.
Alternatively, you can also specify an altText
which will be returned if the dictionary value is empty.
Templating Helpers
.RenderMacro(string alias, object parameters)
Renders a macro in the current page content, given the macro's alias, and parameters required by the macro.
.RenderTemplateAsync(int contentId, int? altTemplateId)
Renders a template asynchronously, as if a page with the given contentId was requested, optionally with an alternative template ID passed in.
Last updated