I have added the property "Member ID" with alias "memberID" to a Member Type in umbraco back end.
When a new account is created, I need to validate based on the member id to ensure an account doesn't already exists with that member id.
I am trying to use Services.MemberService in my controller. I have tried the following code:
This one never pulls any records.
var xMemberService = Services.MemberService; if (xMemberService.GetMembersByPropertyValue("memberID", MemberID.ToString()).Count() > 0) { //do not create account }
This one is never null.
var xMemberService = Services.MemberService; if (xMemberService.GetMembersByPropertyValue("memberID", MemberID.ToString()) != null) { //do not create account }
MemberService.GetMembersByPropertyValue() returns an IEnumerable. It's most likely that instead of returning null it is returning an instance of Enumerable.Empty<IMember>
You need to check if there is a matching member within that collection.
IMember member = .Services
.MemberService
.GetMembersByPropertyValue("memberID", MemberID.ToString())
.SingleOrDefault();
if (member != null)
{
// Do not create account.
}
IMember is an interface not a namespace, it's used for defining multiple classes that would share the same properties or methods. It's found in the Umbraco.Core.Models namespace.
The method .GetMembersByPropertyValue("memberID", MemberID.ToString()) that you are calling returns a collection of that interface. SingleOrDefault() returns the IMember if there is only one in the collection or null.
I have verified the value is saved on the member in two ways: 1) when i get by username, i can pull the memberID value and it is correct. 2) I can see it in the cmsPropertyData table in the database.
As to why I am using a custom property, I am migrating an old system into this one, so I have a large number of accounts that already exist with member ids.
I think I'm going to punt and just add two fields in the cmsMember table to store what I need then just create my own class to provide access to that data. Those won't be viewable in the back office, but that won't matter in this situation.
I would like to know what I am doing wrong though. It bothers me to not understand what I am doing.
To be honest with you I'm not sure what you are doing wrong. I've been using the code supplied to query custom properties in a recent project and it all seems to be working for me.
GetMembersByPropertyValue
I have added the property "Member ID" with alias "memberID" to a Member Type in umbraco back end.
When a new account is created, I need to validate based on the member id to ensure an account doesn't already exists with that member id.
I am trying to use Services.MemberService in my controller. I have tried the following code:
This one never pulls any records.
This one is never null.
What am I doing wrong?
Hi Jeremy,
MemberService.GetMembersByPropertyValue()
returns an IEnumerable. It's most likely that instead of returningnull
it is returning an instance ofEnumerable.Empty<IMember>
You need to check if there is a matching member within that collection.
Cheers
James
What is the IMember namespace?
Update: Found it: Umbraco.Core.Models
IMember is an interface not a namespace, it's used for defining multiple classes that would share the same properties or methods. It's found in the
Umbraco.Core.Models
namespace.The method
.GetMembersByPropertyValue("memberID", MemberID.ToString())
that you are calling returns a collection of that interface.SingleOrDefault()
returns theIMember
if there is only one in the collection ornull
.I'm still not getting anything. If I have the below code:
I never pull the account with a memberID of 1.
However, when I use
I do get the account and then can pull the memberID. Unfortunately I need to be able to check by memberID as well.
Hmmmm...
Is that custom property value visible in the back office? Are you ensuring that the value is saved on saving the member.
Are there more than one member already saved with that
memberID
? That would causeSingleOrDefault()
to returnnull
Also, why use a custom property instead of the built in ID?
Yes, I can see the property in the back office.
I have verified the value is saved on the member in two ways: 1) when i get by username, i can pull the memberID value and it is correct. 2) I can see it in the cmsPropertyData table in the database.
As to why I am using a custom property, I am migrating an old system into this one, so I have a large number of accounts that already exist with member ids.
I think I'm going to punt and just add two fields in the cmsMember table to store what I need then just create my own class to provide access to that data. Those won't be viewable in the back office, but that won't matter in this situation.
I would like to know what I am doing wrong though. It bothers me to not understand what I am doing.
To be honest with you I'm not sure what you are doing wrong. I've been using the code supplied to query custom properties in a recent project and it all seems to be working for me.
I know this is pretty old but I came across this same issue so hopefully this will help someone else out.
The key thing here is that you need to make sure that you cast the value you are searching for to the correct type.
MemberId is a numeric data type so you would need to do:
.GetMembersByPropertyValue("memberID", int.parse(MemberID.ToString()))
Thanks Barry for pointing this out!
It has been driving me mad for the last two hours.
Cheers!
Another thanks for you Barry. Had a similar problem until I found your post :)
is working on a reply...