Copied to clipboard

Flag this post as spam?

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


  • Henrik Vincent 122 posts 616 karma points
    Nov 03, 2017 @ 12:05
    Henrik Vincent
    0

    Fetching custom Member properties in view

    Hi guys

    I've been working on a blog on our company website.

    When adding a blogpost, I want to print the name and image (custom property) from the member who wrote the post.

    I can print the name, but the custom image property (upload) doesn't do anything.

    My code so far looks like this

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        var blogPosts = Umbraco.TypedContentAtXPath("//blogpost").OrderBy("UpdateDate desc").Take(5);
    }
    @{
        <ul class="blogCats">
            @foreach (var item in blogPosts)
            {
                var teaserBanner = Umbraco.TypedMedia(item.GetPropertyValue("blogTeaserBanner"));
                var member = Umbraco.TypedMember(item.GetPropertyValue<int>("author"));
    
    
    
                if((teaserBanner != null) && (member != null))
                {
                                <li class="blogPost">
                                    <div class="blogPostImg">
                                        <img src="@teaserBanner.Url" alt="" />
                                        <p class='postLabel @item.Parent.Name.ToString().ToLower().Replace("æ", "ae").Replace("ø", "oe").Replace("å", "aa").Replace(" ", "")'>@item.Parent.Name</p>
                                    </div>
                                    <div class="postedByWrapper">
                                        <div class="postedBy">
                                            <img src="@member.memberImage.Url" />
                                            <p>@member.Name | <span><i class="fa fa-calendar"></i></span><span class="date">@item.UpdateDate.ToString("dd/MM/yyyy")</span></p>
                                        </div>
                                    </div>
                                    <h2 class="postHeader">@item.GetPropertyValue("blogOverskrift")</h2>
                                    <div class="postTeaser">@item.GetPropertyValue("blogPostTeaser")</div>
                                    <a class="blogReadMore" href="@item.Url">Læs artiklen</a>
                                </li>       
                }
            }
        </ul>
    }
    

    I tried making an extra var for the image and did an if statement check down in the code, but that failed horribly :D

    Hope you guys can give me a push in the right direction.

    Thank you in advance

    Best

    Henrik

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 06, 2017 @ 00:35
    Alex Skrypnyk
    100

    Hi Henrik

    Try this code:

    @member.GetPropertyValue("memberImage")
    

    It should print our member image url

    Thanks,

    Alex

  • Henrik Vincent 122 posts 616 karma points
    Nov 06, 2017 @ 07:17
    Henrik Vincent
    0

    Thank you very much Alex!

    Worked like a charm :)

    Have a great week

    Best

    Henrik

  • Henrik Vincent 122 posts 616 karma points
    Nov 06, 2017 @ 08:42
    Henrik Vincent
    0

    Hi again

    So I got it working with Alex's solution, but I need to call it on specific categories as well, to only call the blogposts of a specific category.

    When doing that I get the following error:

    'Umbraco.Web.PublishedContentModels.Member' does not contain a definition for 'GetPropertyValue'

    My only change from the original code example is the following:

    @{ var selection = CurrentPage.Children.Where("Visible"); }
    

    I'm guessing the error lies in, that it can't connect to the member service now.

    Tried adding

    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    

    but doesnt make any difference.

    Do you have any idea how to solve this, so that I don't have to make a view for each category.

    Thank you in advance

    Best

    Henrik

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 06, 2017 @ 09:17
    Alex Skrypnyk
    0

    Hi Henrik

    This code returns dynamic typed results:

    @{ var selection = CurrentPage.Children.Where("Visible"); }
    

    Do not use dynamic types, try to use this code:

    var selection = Umbraco.AssignedContentItem.Children.Where(x => x.IsVisible());
    

    Thanks,

    Alex

  • Henrik Vincent 122 posts 616 karma points
    Nov 06, 2017 @ 09:22
    Henrik Vincent
    0

    Awesome, Alex!

    You're the man!

    Haven't seen Umbraco.AssignedContentItem before, but worked perfectly.

    Is it correct that CurrentPage is in it's own "scope", and therefor can't access other services?

    Whereas Umbraco. is global and therefor can access the memberservice etc?

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 06, 2017 @ 09:30
    Alex Skrypnyk
    0

    "Umbraco.AssignedContentItem" is a current page, use it everywhere in views.

    Is it correct that CurrentPage is in it's own "scope", and therefor can't access other services?

    • no

    "CurrentPage" is the same as Umbraco.AssignedContentItem, but dynamic type.

    "Umbraco" is a helper for accessing Umbraco data.

    Thanks,

    Alex

  • Henrik Vincent 122 posts 616 karma points
    Nov 06, 2017 @ 09:43
    Henrik Vincent
    0

    Alright.

    Thanks a lot for taking time to clarify :)

    Have a great day.

    Best

    Henrik

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 06, 2017 @ 09:44
    Alex Skrypnyk
    0

    You are welcome, Henrik! Have a nice day too.

    Thanks,

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft