Copied to clipboard

Flag this post as spam?

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


  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Nov 04, 2015 @ 16:55
    Jeroen Breuer
    0

    Get member email from IPublishedContent

    Hello,

    I'm using the Umbraco MemberShipHelper. The helper has 2 methods:

    GetCurrentMember() GetCurrentMemberProfileModel()

    The first method returns an IPublishedContent. How can I get the email of a member from that? The property is available on the ProfileModel that GetCurrentMemberProfileModel() returns, but I prefer not to use 2 methods to get the current member.

    Jeroen

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Nov 04, 2015 @ 17:02
    Kevin Jump
    1

    Hi,

    I had to write some code to do this a few versions back (v7.2) and then you couldn't get it from the IPublishedContent model.

    there is an issue somewhere so i am not sure if it got fixed in later versions - but i ended up making the member dynamic

    var email member.AsDynamic().Email;
    

    bit of a hack but it works.

  • Ravi Motha 290 posts 500 karma points MVP 8x c-trib
    Nov 04, 2015 @ 17:13
    Ravi Motha
    105

    Jeroen,

    I've used the membership helper in this way and can return any of the properties

    var memberShipHelper = new Umbraco.Web.Security.MembershipHelper(Umbraco.Web.UmbracoContext.Current);
    var myUser = memberShipHelper.GetCurrentMember();
    

    @myUser.GetProperty("Email").Value

    I'm using a 7.2.x and I've had no problems with it

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Nov 04, 2015 @ 18:19
    Jeroen Breuer
    0

    Thanks for the tips. Will try myUser.GetProperty("Email").Value. The reason I didn't try it is because email isn't a property set on the member type. When the models builder generated a strongly typed model it also didn't contain an email property.

    Jeroen

  • Anders Bjerner 487 posts 2990 karma points MVP 8x admin c-trib
    Nov 04, 2015 @ 20:24
    Anders Bjerner
    8

    Hi Jeroen,

    When you reference a member by IPublishedContent it is really an instance of MemberPublishedContent. If you cast to this type, you will have acces to an email property (as well as other member-only properties):

    @{
    
        IPublishedContent content = Umbraco.TypedMember(memberId);
    
        MemberPublishedContent member = (MemberPublishedContent) content;
    
        <p>@member.Email</p>
    
    }
    
  • Tobbe 81 posts 387 karma points c-trib
    Jan 05, 2018 @ 09:14
    Tobbe
    1

    When I try to do this I get this error (using umbraco 7.7.2):

    Unable to cast object of type 'Umbraco.Web.PublishedContentModels.Member' to type 'Umbraco.Web.PublishedCache.MemberPublishedContent'.
    

    Getting the same error if I try to use ApplicationContext.Current.Services.MemberService.GetById(memberId)

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Nov 05, 2015 @ 10:38
    Jeroen Breuer
    7

    Since I'm using the ModelsBuilder I don't get a MemberPublishedContent back, but the generated model which doesn't have the Email property. I fixed it with this code:

    public partial class Member
    {
        public string Email
        {
            get
            {
                return this.GetPropertyValue<string>("Email");
            }
        }
    }
    

    Jeroen

  • Sean Håkansson 67 posts 193 karma points
    Dec 11, 2017 @ 14:14
    Sean HÃ¥kansson
    0

    Sorry for the newb question and more than a year later :) But where would I put this? (Not in the auto-generated file I'm guessing)

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Dec 11, 2017 @ 14:23
    Kevin Jump
    1

    Hi

    You can put this in it's own file in somewhere such as the App_Code folder (you might need to create this).

    the generated classes are marked as partial, which means you can create other partial classes with the same name across your project and they are turned into one class as compile/run time.

  • Richard Eyres 98 posts 580 karma points
    Oct 25, 2016 @ 14:05
    Richard Eyres
    1

    Thanks Jeroen. This was the exact same situation i found myself in. Your reply just saved me a little time. Thank you.

  • Chris 12 posts 84 karma points
    Oct 24, 2018 @ 14:39
    Chris
    0

    I have found you can get the email dynamically, but not typed:

    .GetPropertyValue("Email").ToString();
    
Please Sign in or register to post replies

Write your reply to:

Draft