Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I want to know if there's a way to call the user's avatar/image for a blog post just like the way you can call the user's name or the date that the blog post was written:
<div class="blog_meta_items"><i class="glyphicon glyphicon-time"></i>@CurrentPage.CreateDate.ToString("%d MMM, yyyy")</div>
<div class="blog_meta_items"><i class="glyphicon glyphicon-user"></i>@CurrentPage.CreatorName</div>
Thanks in advance,
You can access user properties with the UserService. There is a new property "Avatar" which will hold the media file system relative path of the users custom avatar if they uploaded one.
var userAvatar = ApplicationContext.Current.Services.UserService.GetUserById(Model.Content.CreatorId).Avatar;
However this string will be empty if they are using Gravatar, or will be set to "none" if they have deleted all photos from their user profile, so you will need to check for these scenarios.
Thanks for the reply. I have tried it out, but the output I get is nothing. I wrote this:
var Useravatar = ApplicationContext.Current.Services.UserService.GetUserById(Model.Content.CreatorId).Avatar;
Is there anyone who can help me with this?
Are you receiving any errors, or is Avatar just null?
Also... this might help...
I'm not receiving any errors, and I have an avatar on the user.
I can't see what you have uploaded, John?
Sorry about that, I didn't escape the angle brackets. I updated the previous post
Did anyone have a fix for this - I've been having the same issue.
I can see that there is an id being passed but I'm not getting anything back from that line
var userAvatar = ApplicationContext.Current.Services.UserService.GetUserById(CurrentPage.author).Avatar;
var userName = ApplicationContext.Current.Services.UserService.GetUserById(CurrentPage.author).Name;
None of these work for me - well they did but some reason they stopped working.
also this has stopped working for me today:
Not sure whats going on but I've been banging my head about it all day. everything worked until I tried adding some inline css - then everything decided to break.
Well that serves you right for adding inline styles. :)
Just kidding. Sounds like it could be a caching issue, have you tried republishing your site?
You've got the properties a little wrong I think, instead of CurrentPage.author try, CurrentPage.CreatorId.
On an unrelated point, I'd suggest a little tidy of your code as well. First of all, try to avoid using dynamics (such as CurrentPage) - they make your code harder to read and I believe they will be deprecated in Umbraco 8. Not a major worry now but might ease your upgrade path in the future :) Instead of CurrentPage, you might simply make use of Model or Model.Content depending on where you're calling this.
Secondly, you're calling the user service twice which potentially hits the database twice as well. You can get the user once by sticking it in a variable of it's own. Example below (this would be how I'd do it in a UmbracoTemplatePage):
IUser user = ApplicationContext.Current.Services.UserService.GetUserById(Model.Content.CreatorId);
Then you can simply use user.Avatar and user.Name when you need those values.
For your second problem, what version of Umbraco are you on? What does Model.Content.GetPropertyValue("headerBackgroundImage") return?
Thank you for your help, here is a little of what I have tried and more context:
CurrentPage.author is a drop down where a user can select another user as the author of the content, however that being said I did try CurrentPage.CreatorId and got the same result (being no errors but no content displayed).
I also tried Model.Content but was getting errors about referencing, when trying IUser method it didn't like that either just shouted about not knowing what IUser was all about. The background image also doesn't pull a value, however strap line and content does.
Umbraco version 7.7.4 assembly: 1.0.6505.22489
My current set up is I have a doc type which is set up like such:
and my page code is as follows:
Layout = "Master.cshtml";
var userAvatar = ApplicationContext.Current.Services.UserService.GetUserById(CurrentPage.CreatorId).Avatar;
var userName = ApplicationContext.Current.Services.UserService.GetUserById(CurrentPage.CreatorId).Name;
<!-- Main Header Section -->
<div id="page-header" class="section" style="background-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.8) 0%, rgba(0, 0, 0, 0.3) 100%), url(@Umbraco.Media(Model.Content.GetPropertyValue("headerBackgroundImage")).Url)">
<div class="container align-middle">
<img src="/media/@CurrentPage.userAvatar" />
<!-- Main Header Section End -->
<!-- Summary -->
<div class="container-fluid bg-light-grey">
<!-- End Summary -->
<!-- Article Body -->
<!-- related articles -->
<!-- Article Body -->
The only additional thing I could tell you that could help is that the author drop down selects a user that is classed as an author (I believe that's a default Umbraco user group?)
IUser needs @using Umbraco.Core.Models.Membership at the top of your file to work. It's also not essential, you can carry on to use var, I just like to see the type of variable being used really easy :)
For your background image - do you have more fields in your document type? I can't see headerBackgroundImage in there.
For your users, I notice in your code that you're using @CurrentPage.userName and @CurrentPage.userAvatar these should just be userName and userAvatar.
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted