How to get custom BlockList data from an IMember

Hi all,

I’m currently working on a site where we use a custom block list on a member type. The problem is that it seems that there is not a good way to get the BlockList value when working with a member in code.

In content nodes everything works perfectly fine. You only have to use content.GetValue<BlockListModel>(“blockList”) or content.GetValue<IEnumerable<BlockListItem>>(“blockList”)and it will parse correctly. However, when trying this from an IMember class (acquired through the IMemberService), these calls will return null. Strange, as the JSON of the value can be acquired by getting the value as a string. See the following code:

var member = _memberService.GetByKey(key);
var test = member as Member; // Returns null
var test2 = member.GetValue<string?>("invoiceAddresses"); // Returns the JSON as a string
var test3 = member.GetValue<IEnumerable<BlockListItem>>("invoiceAddresses"); // Returns null
var test3 = member.GetValue<BlockListModel>("invoiceAddresses"); // Returns null

Currently we manually extract the data from the JSON by parsing to a JObject, but surely there is a better way to get the data?

Cheers,

Richard

Hey Richard,

IMember & IContent are the database models of the data - which means the raw json, etc that is stored in the DB.

When you get a blocklist from content you are probably getting it from an IPublishedContent which is the cached version of an IContent node.

When converting from IContent to IPublishedContent Umbraco runs through PropertyValueConverters for all properties which converts the raw data into models such as the BlockListModel.

Similarly for members you can convert the IMember to a cached version of the member, which will also make it possible to get the properties as their converted models instead of raw data by using the IMemberManager.

var member = _memberService.GetByKey(key);
var publishedMember = _memberManager.AsPublishedMember(member);
var block = publishedMember.Value<BlockListModel>("blockListAlias");

One important thing to note though - members do not perform nearly as well as content, and while you can access content directly via the cache the members are not cached and goes to the DB every time you request it.
This can easily cause performance issues and managing a custom cache could be advised dependant on your setup and needs.

1 Like

Hi Jesper,

Thanks for the quick reply! I should’ve known that IMember was a database model, I’ve been using it to update member values :man_facepalming:

Just one extra thing: _memberManager.AsPublishedMember(member) requires a MemberIdentityUser, not an IMember. That’s why I’ve added one extra line to the solution:

_memberManager.FindByEmailAsync(member.Email)