Blog about tips & tricks for CMS enhancement

eric.petersson

Member picker in Umbraco 8 code behind with default and custom properties


Making a Member Picker in Umbraco is a great way of displaying your client's members or employees for various article information relations. Today we will take a  look on how to retrieve members with default and custom properties set on the default Member Type, shipped in Umbraco 8. We will to this in code behind.

Start of by implementing a Member Picker property on your desired Document Type in the Umbraco back office. I will in this example make use a Document Type called ArticlePage since that is were my client would like to have the ability to set and display the member as a "content owner" of the page.

Set up the following in your Controller, in my case, ArticlePageController:

 public class ArticlePageController : RenderMvcController
    {
        private readonly IMemberService _memberService;

        public ArticlePageController(IMemberService memberService)
        {
            _memberService = memberService;
        }

        public override ActionResult Index(ContentModel model)
        {
            var articlePage = model.Content as ArticlePage;

            IMember contentOwner = null;

            var contentOwnerId = articlePage.ContentOwner?.Id;
            if (contentOwnerId.HasValue)
            {
                contentOwner = _memberService.GetById(contentOwnerId.Value);
            }
          
            var memberViewModel = new ExtendedMemberViewModel(contentOwner);
            var viewModel = new ArticlePageViewModel(articlePage, memberViewModel);

            return CurrentTemplate(viewModel);
        }
    }

In addition to retrieve the default shipped values of Umbraco's Member Type, we just inject the IMemberService for our RenderMvcController inheritance and retrieve our selected member from the User interface in Umbraco with the GetById(contentOwnerId), since the default implementation of Member Picker returns an integer of the member.

Our content owner as a Member Picker property on ArticlePage Document Type

This is nice since we by default retrieve membership properties such as email and name to display to our front end.

But what if the set up some custom properties on our Member Type? Well, then we need to retrieve them the dirty way like the example below:

Two custom properties set on default Member Type, Occupation and Image.
 private readonly IMediaService _mediaService;
 
 public ArticlePageController(IMediaService mediaService)
        {
            _mediaService = mediaService;
        }

var contentOwnerId = articlePage.ContentOwner?.Id;
if (contentOwnerId.HasValue)
{
    contentOwner = _memberService.GetById(contentOwnerId.Value);
    memberImage = GetMemberImageUrl(contentOwner);
    memberOccupation = contentOwner.Properties["occupation"]?.GetValue()?.ToString();
}

private string GetMemberImageUrl(IMember contentOwner)
{
    var image = contentOwner.Properties["image"].GetValue()?.ToString();
    var imageGuidUdi = Udi.Parse(image);
    var imageNodeId = Current.Services.EntityService.GetId(imageGuidUdi);

    if (imageNodeId.Success)
    {
        var mediaItem = Current.Services.MediaService.GetById(imageNodeId.Result);

        return $"://{HttpContext.Request.Url.Host}{mediaItem.GetUrl("umbracoFile", Current.Logger)}";
    }

    return string.Empty;
}

Here we extend the property retrieval of image (Media) and occupation (string), our extended member properties and retrieve the hard typed. To retrieve the url of the proper placement of the image, which is stored in the Umbraco Media folder, we send the Udi from the Member Picker and retrieve with the help of the MediaService injected its proper url.

All together we will end up with something like below:

public class ArticlePageController : RenderMvcController
    {
        private readonly IMemberService _memberService;
        private readonly IMediaService _mediaService;

        public ArticlePageController(IMemberService memberService, IMediaService mediaService)
        {
            _memberService = memberService;
            _mediaService = mediaService;
        }

        public override ActionResult Index(ContentModel model)
        {
            var articlePage = model.Content as ArticlePage;

            IMember contentOwner = null;
            string memberImage = string.Empty;
            string memberOccupation = string.Empty;

            var contentOwnerId = articlePage.Informationsagare?.Id;
            if (contentOwnerId.HasValue)
            {
                contentOwner = _memberService.GetById(contentOwnerId.Value);
                memberImage = GetMemberImageUrl(contentOwner);
                memberOccupation = contentOwner.Properties["occupation"]?.GetValue()?.ToString();
            }
          
            var memberViewModel = new ExtendedMemberViewModel(contentOwner, memberImage, memberOccupation);
            var viewModel = new ArticlePageViewModel(articlePage, memberViewModel);

            return CurrentTemplate(viewModel);
        }

        private string GetMemberImageUrl(IMember contentOwner)
        {
            var image = contentOwner.Properties["image"].GetValue()?.ToString();
            var imageGuidUdi = Udi.Parse(image);
            var imageNodeId = Current.Services.EntityService.GetId(imageGuidUdi);

            if (imageNodeId.Success)
            {
                var mediaItem = Current.Services.MediaService.GetById(imageNodeId.Result);

                return $"https://{HttpContext.Request.Url.Host}{mediaItem.GetUrl("umbracoFile", Current.Logger)}";
            }

            return string.Empty;
        }
    }
}

Hopefully you followed along on how to retrieve values from the Member Picker in Umbraco 8, both with default type values as well as an example of custom properties.

Hit that comment section if you would end up with questions!