Copied to clipboard

Flag this post as spam?

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


  • Sam 22 posts 113 karma points
    Sep 26, 2018 @ 14:58
    Sam
    0

    Hi,

    I am trying to get a property from the members area of the backend and cannot get it out easily!

    I have added a member picker to the members tab to assign a 'business'

    When in the view i ask for the name of the assigned business i only get an ID or a guid.

    How do i get the text name/value of this property?

    Images and code below.

    Any help much appreciated or suggestions :)

    Many thanks, Sam

    @{
        var memberDetails = Members.GetCurrentMember();
        var businessName = memberDetails.GetPropertyValue("business");
    }
    
    <p>@businessName</p>
    

    Member picker

    Member Picker

    Member Details

  • Marc Goodson 2128 posts 14220 karma points MVP 8x c-trib
    Sep 26, 2018 @ 18:22
    Marc Goodson
    0

    Hi Sam

    If you have the Id/Guid that represents the Member you can get the Member object as IPublishedContent by writing

    var pickedMemberId = "yourguididfromthepicker";
    var pickedMember = Umbraco.TypedMember(pickedMemberId);
    if (pickedMember!=null){
    var memberName = pickedMember.Name;
    var someProperty = pickedMember.GetPropertyValue<string>("someProperty");
    
    }
    

    if I've understood correctly!

    regards

    Marc

  • Sam 22 posts 113 karma points
    Sep 27, 2018 @ 08:20
    Sam
    0

    Hi Marc,

    Thank you for your reply.

    Unfortunately this didn't work for me, The solution that did i've marked below :)

    Many thanks again!

    Sam

    The code:

    //This is the Business Guid
    var businessGuid = member.GetValue<string>("business");
    
    //Marc Forum solution
    var pickedMemberId = businessGuid;
    var pickedMember = Umbraco.TypedMember(pickedMemberId);
    
    if (pickedMember != null)
    {
        var memberName = pickedMember.Name;
        var someProperty = pickedMember.GetPropertyValue<string>("business");
    
        <p>@memberName</p>
        <p>@someProperty</p>
    }
    
  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Sep 26, 2018 @ 21:29
    Bjarne Fyrstenborg
    100

    Hi Sam

    It seems you are using on of the latest Umbraco versions (since the green toogle button (checkbox) has been modified recently).

    If you have PropertyValueConverters enabled, which it should be by default in new projects, I think you can get the

    @{
        var currentMember = Members.GetCurrentMember();
        var business = currentMember.GetPropertyValue<IEnumerable<IPublishedContent>>("business");
    
        if (business != null)
        {
            var name = business.FirstOrDefault().Name;
        }
    }
    

    I am not sure which property editor (datatype) you are using for the property "business" - if it is Multi Node Tree Picker (MNTP) you need to cast it to IEnumerable<IPublishedContent>> for other property editors just IPublishedContent.

    /Bjarne

  • Sam 22 posts 113 karma points
    Sep 27, 2018 @ 08:17
    Sam
    0

    Hi Bjarne,

    The below code worked for me!

    As you suggested i just needed to use the IPublishedContent.

    Below is the code that worked:

    @{
    var currentMember = Members.GetCurrentMember();
    var business = currentMember.GetPropertyValue<IPublishedContent>("business");
    
    if (business != null)
    {
        var name = business.Name;
        <p>@name</p>
    }
    

    --Side note-- If i wanted to get the same values in a controller to pass to a model, how would i do this?

    Currently i am

    1. Getting the memberId.
    2. Then getting the Guid.
    3. Stripping off 'umb://member/' from the Guid
    4. Writing a SQL query to get the name of the business from the text column in umbracoNode table WHERE Guid = Guid.
    5. Then saving that string value for business name.

    Is there an easier way?

    Many thanks,

    Sam.

  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Oct 01, 2018 @ 07:35
    Bjarne Fyrstenborg
    0

    Hi Sam

    I you have either a custom controller as a RenderMvcController for your page https://our.umbraco.com/documentation/reference/routing/custom-controllers or a SurfaceController which is a part of the page https://our.umbraco.com/documentation/reference/routing/surface-controllers for part of your page e.g. with a form or another component, then you should be able to either get the property on the current member or getting a specific member and map these values to your custom model.

    If you are using a Member Picker (Umbraco.MemberPicker2), you should be able to get it the selected member by using either .GetPropertyValue<IPublishedContent>("business") or .GetPropertyValue<IEnumerable<IPublishedContent>>("business"), but first you need to get the member (or content node) with this property.

    If you are using ModelsBuilder Dll mode, you can use something like this if you deal with a specific member type:

    var member = Members.GetCurrentMember();
    var customer = member.OfType<Umbraco.Web.PublishedContentModels.Customer>();
    if (customer != null)
    {
        var business = customer.Business;
    }
    

    /Bjarne

  • Sam 22 posts 113 karma points
    Oct 01, 2018 @ 07:45
    Sam
    0

    Hi Bjarne,

    I will give this a try and let you know how i get on :)

    Many thanks, Sam

  • Sam 22 posts 113 karma points
    Oct 01, 2018 @ 13:25
    Sam
    0

    Hi Bjarne,

    This solution worked perfectly!

    Thank you

    Sam

Please Sign in or register to post replies

Write your reply to:

Draft