Copied to clipboard

Flag this post as spam?

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


  • Jeremy 14 posts 64 karma points
    Jun 03, 2015 @ 21:19
    Jeremy
    0

    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.

    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
    }

     

    What am I doing wrong?

     

  • James Jackson-South 489 posts 1747 karma points c-trib
    Jun 04, 2015 @ 15:46
    James Jackson-South
    0

    Hi Jeremy,

    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.
                }
    

    Cheers

    James

  • Jeremy 14 posts 64 karma points
    Jun 04, 2015 @ 15:56
    Jeremy
    0

    What is the IMember namespace?

     

    Update: Found it: Umbraco.Core.Models

  • James Jackson-South 489 posts 1747 karma points c-trib
    Jun 04, 2015 @ 16:10
    James Jackson-South
    0

    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.

  • Jeremy 14 posts 64 karma points
    Jun 04, 2015 @ 16:36
    Jeremy
    0

    I'm still not getting anything.  If I have the below code:

    IMember member = Services.MemberService.GetMembersByPropertyValue("memberID", "1").SingleOrDefault();
                if (member != null)
                {
    //Do not create account
                }

    I never pull the account with a memberID of 1.

    However, when I use

    IMember member = Services.MemberService.GetByUsername(username);
                if (member != null)
                {
                    //Do not create an account
                }

    I do get the account and then can pull the memberID.  Unfortunately I need to be able to check by memberID as well.

     

  • James Jackson-South 489 posts 1747 karma points c-trib
    Jun 04, 2015 @ 17:20
    James Jackson-South
    0

    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 cause SingleOrDefault() to return null

    Also, why use a custom property instead of the built in ID?

  • Jeremy 14 posts 64 karma points
    Jun 04, 2015 @ 17:35
    Jeremy
    0

    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.

     

  • James Jackson-South 489 posts 1747 karma points c-trib
    Jun 04, 2015 @ 20:49
    James Jackson-South
    0

    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.

  • Barry 15 posts 94 karma points
    Sep 21, 2016 @ 15:19
    Barry
    102

    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()))

  • Rune Antonsen 29 posts 145 karma points
    Nov 16, 2016 @ 15:28
    Rune Antonsen
    0

    Thanks Barry for pointing this out!

    It has been driving me mad for the last two hours.

    Cheers!

  • suzyb 474 posts 932 karma points
    Nov 29, 2016 @ 12:53
    suzyb
    0

    Another thanks for you Barry. Had a similar problem until I found your post :)

Please Sign in or register to post replies

Write your reply to:

Draft