Get the avatar image of an user to display on a blog post
Hi guys!
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:
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.
Quick example:
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.
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.
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.
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.
Version:
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:
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.
Get the avatar image of an user to display on a blog post
Hi guys!
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:
Thanks in advance,
/Sharmarke
Hi Sharmarke,
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.
Quick example:
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.
Hi Matt,
Thanks for the reply. I have tried it out, but the output I get is nothing. I wrote this:
Is there anyone who can help me with this?
Are you receiving any errors, or is Avatar just null?
Also... this might help...
<img src="@Useravatar.Url">
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
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?
Hi David,
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 ofCurrentPage
, you might simply make use ofModel
orModel.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
):Then you can simply use
user.Avatar
anduser.Name
when you need those values.For your second problem, what version of Umbraco are you on? What does
Model.Content.GetPropertyValue("headerBackgroundImage")
return?Hi Ben,
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 tryCurrentPage.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.Version: 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:
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 usevar
, 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 beuserName
anduserAvatar
.is working on a reply...