In today's tutorial, I'm going to explain how to use the Umbraco API to get the name of the person who created/updated a page you want to query within Umbraco. Some examples of why you might want the author's name might include when you're creating a blog and you want to display the name of the author, or if you want to set-up a content approval workflow in the Umbraco backend, so let's begin.
What's The Difference Between The Umbraco WriterId and the CreatorId?
In Umbraco, the first thing that might confuse is the different Name properties that you have available to you, namely the WriterId and the CreatorId. 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.
IPublishedContent or IContent
Let's get down to business. I won't get into the pro's and con's for using the different Umbraco API, as I've done that already in, UmbracoHelper Vs ContentTypeService. In most situations, you will want to use the Umbraco helper for speed.
var id = 1; var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); IPublishedContent content1 = umbracoHelper.TypedContent(id); var publishedBy = ApplicationContext.Current.Services.UserService.GetProfileById(content1.WriterId).Name; var createdBy = ApplicationContext.Current.Services.UserService.GetProfileById(content1.CreatorId).Name;
Let's talk through the code quickly. I'm using the Umbraco Helper to get a reference to the IPublishedContent item we want to query. The IPublishedContent doesn't have the publisher, or creator name available directly. Instead, it was a reference to the user ID instead. You can access this via the WriterId and CreatorId properties. You can then use the ApplicationContext.Current.Services.UserService.GetProfileById() to get the full profile information for the published. I pass in the WriterID and use the 'Name' property to get the publishers name... it's as simple as that. Enjoy!