Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sharmarke Hujale 103 posts 346 karma points
    Oct 24, 2017 @ 06:29
    Sharmarke Hujale
    0

    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:

    <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,

    /Sharmarke

  • Matt Darby 28 posts 391 karma points c-trib
    Oct 24, 2017 @ 09:53
    Matt Darby
    0

    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:

    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.

  • Sharmarke Hujale 103 posts 346 karma points
    Oct 25, 2017 @ 17:39
    Sharmarke Hujale
    0

    Hi Matt,

    Thanks for the reply. I have tried it out, but the output I get is nothing. I wrote this:

       //GET AVATAR 
       var Useravatar = ApplicationContext.Current.Services.UserService.GetUserById(Model.Content.CreatorId).Avatar;
    
    
      //Code
        <div class="blog_content_meta">
                <div class="blog_meta_items">@CurrentPage.Useravatar</div>
        </div>
    
  • Sharmarke Hujale 103 posts 346 karma points
    Oct 29, 2017 @ 23:24
    Sharmarke Hujale
    0

    Is there anyone who can help me with this?

  • John Bergman 483 posts 1132 karma points
    Oct 30, 2017 @ 01:09
    John Bergman
    0

    Are you receiving any errors, or is Avatar just null?

    Also... this might help...

    <img src="@Useravatar.Url">

  • Sharmarke Hujale 103 posts 346 karma points
    Oct 30, 2017 @ 23:09
    Sharmarke Hujale
    0

    I'm not receiving any errors, and I have an avatar on the user.

    I can't see what you have uploaded, John?

  • John Bergman 483 posts 1132 karma points
    Oct 31, 2017 @ 04:33
    John Bergman
    0

    Sorry about that, I didn't escape the angle brackets. I updated the previous post

  • David 14 posts 94 karma points
    Nov 09, 2017 @ 17:13
    David
    0

    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:

    @Umbraco.Media(Model.Content.GetPropertyValue("headerBackgroundImage").ToString()).Url
    

    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.

  • David Parr 48 posts 206 karma points
    Nov 09, 2017 @ 22:18
    David Parr
    0

    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?

  • Ben Palmer 176 posts 842 karma points c-trib
    Nov 11, 2017 @ 12:42
    Ben Palmer
    0

    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 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?

  • David 14 posts 94 karma points
    Nov 13, 2017 @ 14:57
    David
    0

    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 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:

    enter image description here

    and my page code is as follows:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        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">
            <h1 class="white">@CurrentPage.title</h1>
            <div class="author-block">
                <img src="/media/@CurrentPage.userAvatar" />
                <div class="author-block-detail">
                    <p class="author">
                        @CurrentPage.userName
                    </p>
                    <p class="article-date">
                        @CurrentPage.Date
                    </p>
                </div>
            </div>
        </div>
    </div>
    <!-- Main Header Section End -->
    
    <!-- Summary -->
    <div class="container-fluid bg-light-grey">
        <div class="container">
            <section>
                <h4 class="article-summary">@CurrentPage.summary</h4>        
            </section>
        </div>
    </div>
    <!-- End Summary -->
    
    <!-- Article Body -->
    <div class="container-fluid">
        <div class="container">
            <section>
                <div class="row">
                    <div class="col-md-9">
                        <article>
                            <h3>@CurrentPage.strapline</h3>
                            @CurrentPage.content
                        </article>
                    </div>
                    <div class="col-md-3">
                      <!-- related articles -->
                    </div>
                </div>
            </section>
        </div>
    </div>
    <!-- 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?)

  • Ben Palmer 176 posts 842 karma points c-trib
    Nov 13, 2017 @ 23:25
    Ben Palmer
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft