In today's tutorial, you will learn how you can use the Umbraco API to get the name of the person who created or last updated a page. An example of why you might want the author's name might occur when you're creating a blog and you want to display the name of the author!
Sound good, let us begin 🔥🔥🔥
What's The Difference Between The Umbraco WriterId and the CreatorId?
The first thing to cover is the properties that you can use. If you look at the properties that are exposed on the IPublishedContent
you will notice the WriterId
and the CreatorId
properties. What is the difference between them? The WriterId
is the Id
of the person who last updated the content and the CreatorId
is the Id of the person who originally wrote the article. With that in mind, let's look at some code.
I won't get into the pro's and con's for using the different Umbraco API in this tutorial, as I've done that already in, UmbracoHelper Vs ContentTypeService. In most situations, you will want to use the Umbraco helper for speed.
Using the Umbraco helper, you cam use this code to access either property:
Let's talk through the code quickly. I'm using the Umbraco Helper to get a reference to the page I want to query. The type of this page is of the IPublishedContent
. The IPublishedContent
doesn't expose the publisher, or creator name directly. Instead, it exposes the ID of a user. You can then use that ID to look up the name
One way of doing this is using the ApplicationContext.Current.Services.UserService.GetProfileById()
to get the full profile detail of the editor. In Lin5 and Line5, the WriterID
is passed in. This will then return the profile information of the content editor. You can then access the name using the Name
property. It's as simple as that.
This code is using ApplicationContext
. One word of caution is that ApplicationContext
will query the database to get the name. This may create performance issues, so you may need to change the name, or, you may even want to hook into the event pipeline and store the properties name directly on the page instead! Read this to learn how to do that. Happy Coding 🤘