Umbraco 7 API Basics Introduction

In today’s guide, I’m going to cover some of the basic Umbraco APi calls that you will need when you start working with Umbraco. This post is meant to be a quick reference guide to frequently used Umbraco queries. If you have any to add please leave a comment.

How To Get The Current Page Id

There are several ways to get the current page ID, directly from the context:

HttpContext.Current.Request["pageid"];

In Razor

@CurrentPage.Parent.Id
Node.GetCurrent().Id);

After you get the current page Id, you will probably want to get a copy of the current document. To remind you, the Document type is used when you want to get the data stored against a document type inside the database. A node on the other hand will return the data which has been published and cached. SO to get the current document we use:

var pageId = ConvertToInt32(HttpContext.Current.Request["pageid"]);
var doc = new Document(pageId)

To get the current Node, we use:

var helper = new UmbracoHelper(UmbracoContext.Current);
IPublishedContent content = umbracoHelper.TypedContent(Node.getCurrentNodeId());

Now if you are using a Surface Controller, you can make life a little easier, by using:

IPublishedContent currentNode = Umbraco.TypedContent(CurrentPage.Id)

How To Get The Current Pages Document Type in Umbraco

currentPageNode.DocumentTypeAlias

How To Get All Siblings from the Current Page

If you want to get all of the current page siblings in Razor we can use:

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

How To Get All Children from the Current Page

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

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

3 replies
      • Jon D Jones
        Jon D Jones says:

        You probably could by hooking into the pipeline at some stage and changing things, however, it doesn’t sound like a good approach. it would probably be better to add the things you need in the context yourself via an attribute on a controller or something like that. Trying to change default Umbraco behaviour normal ends up in creating more bugs than your trying to fix, what are you trying to achieve?

        Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *