Copied to clipboard

Flag this post as spam?

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


  • Karsten Grombach 16 posts 34 karma points
    Apr 30, 2010 @ 08:01
    Karsten Grombach
    0

    Get Properties from Member in Umbraco programmatically

    I thought this would be really simple but ..

    We've create a user and a member type with various properties When we try to access the properties via the member object we got nothing.

    //Member m is current User

    eg. Property s = m.getProperty("PreferdUserName"); is null

    m.getProperties has a count of Zero..

    have we missed something obvious?

     

     

  • Neil Campbell 58 posts 182 karma points
    Apr 30, 2010 @ 08:30
    Neil Campbell
    0

    Hi Karsten,
    I think you have missed the .value property on the end.
    So basically your code should read:

    string username = m.getProperty("PreferdUserName").Value.ToString();

    Hope this helps.

    Cheers,
    Neil

  • Karsten Grombach 16 posts 34 karma points
    Apr 30, 2010 @ 09:43
    Karsten Grombach
    0

    Neil,

    thanks for your reply. I've tried that as well. Wenn accessing the the property that way I get a "object not set to a .. " error...

    Karsten

     

  • Neil Campbell 58 posts 182 karma points
    Apr 30, 2010 @ 10:20
    Neil Campbell
    0

    Try hard coding the memberId in(just as a test) and getting the member that way i.e Member m = new Member(1234);

    Then test to see if you can access the specific member property.

    It could be that the member is coming back null, when you are calling Member m = Member.GetCurrent();

    Cheers,
    Neil

  • Masood Afzal 176 posts 522 karma points
    Apr 30, 2010 @ 10:37
    Masood Afzal
    0
               string pValue = string.Empty; 
                if (!((m.getProperty(propertyAlias) == null) || string.IsNullOrEmpty(m.getProperty(propertyAlias).Value.ToString())))
                {
                    pValue  =  m.getProperty(propertyAlias).Value.ToString();
                }
  • Julius Bartkus 51 posts 76 karma points
    Apr 30, 2010 @ 11:02
    Julius Bartkus
    0

    You should check if the property is not DBNull.Value

    Property localeProperty = m.getProperty("memberProfileCustomerLocale");
    String locale = (localeProperty.Value != DBNull.Value && (String)localeProperty.Value != "") ? (String)localeProperty.Value : "en-GB";

  • Karsten Grombach 16 posts 34 karma points
    Apr 30, 2010 @ 11:43
    Karsten Grombach
    0

    thanks everyone.. I should have been more precice in my problem description. Sorry!

    I can debug in Visual Studio right upto the the Member m = Member.GetCurrent(); and can see that member object has been loaded.

    I know which user I am testing and can see the Property PreferdUserName with a value in the admin / members dialog. So this is what is really confusing

    Here the code I've been using to narrow down the problem. (Member has definetly  been loaded..)

     

             public override void Transfer(umbraco.cms.businesslogic.member.Member m)
            {
                //PropertyType.GetPropertyType();
                this.UserName = m.LoginName;
                //PropertyType    pt = new PropertyType();
                try
                {
                    Property displayNameProp = m.getProperties.First(p => p.PropertyType.Alias == "PreferdUserName");
                    string bla = "123";
                }
                catch (Exception ex) // no occurence found exception (because getProperties has a count of zero)
                {
                    string e = ex.ToString();
                }
                try
                {
                    Property  s = m.getProperty("PreferdUserName");
                    string bla = s.Value.toString();
                }
                catch (Exception ex) // Null reference exception
                {
                    string e = ex.ToString();
                }
            }
  • Tobias Neugebauer 52 posts 93 karma points
    Apr 30, 2010 @ 12:05
    Tobias Neugebauer
    0

    Hello Karsten,

    did you try using the GenericProperties Proterty? Like this:

    Property property = m.GenericProperties.Where(p => p.PropertyType.Alias == "PreferedUserName").FirstOrDefault();

    this worked for me :)

    Toby

  • Karsten Grombach 16 posts 34 karma points
    Apr 30, 2010 @ 12:50
    Karsten Grombach
    0

    When accessing GenericProperties I get:

    "The type initializer for 'umbraco.cms.businesslogic.datatype.controls.Factory' threw an exception."

    ?

  • Tobias Neugebauer 52 posts 93 karma points
    Apr 30, 2010 @ 13:36
    Tobias Neugebauer
    0

    This is totally strang!

    I tried your code and mine and bahm ran into the same error. After a littel bit of debugging moving the code to other places it suddenly worked even in the place where i got the error!

    So there are two mor questions for me: 1. are you running in MediumTrust Environment? 2. Where does the call happen (some kind of handler/module, aspx page or usercontrol)?

    Maybe this could get us closer to an answer...

  • Karsten Grombach 16 posts 34 karma points
    Apr 30, 2010 @ 17:23
    Karsten Grombach
    0

    I'm running the code from a REST WCF service which is running in the same website as umbraco. As mentioned above I can use other Umbraco API objects perfectly well. But that could be the issue.. I'll try and move  the code into a "normal" user and report

  • Karsten Grombach 16 posts 34 karma points
    Apr 30, 2010 @ 18:46
    Karsten Grombach
    0

    I moved the code into a user control and debugged it and it is still giving me errors. Strangely enough it worked once in the immeadiate window:

    ? m.getProperty("PreferdUserName")
    w3wp.exe Information: 0 : 122. RecordsReader created by CMSNode.setupNode. Open Data Readers: 2
    w3wp.exe Information: 0 : 122. RecordsReader closed. Open Data Readers: 1
    w3wp.exe Information: 0 : 123. RecordsReader created by ContentType.setupContentType. Open Data Readers: 2
    w3wp.exe Information: 0 : 123. RecordsReader closed. Open Data Readers: 1
    w3wp.exe Information: 0 : 124. RecordsReader created by ContentType.<get_PropertyTypes>b__0. Open Data Readers: 2
    w3wp.exe Information: 0 : 124. RecordsReader closed. Open Data Readers: 1
    {umbraco.cms.businesslogic.property.Property}
        _data: {umbraco.cms.businesslogic.datatype.DefaultData}
        _id: 169
        _pt: {umbraco.cms.businesslogic.propertytype.PropertyType}
        Id: 169
        PropertyType: {umbraco.cms.businesslogic.propertytype.PropertyType}
        Value: "kg123"
        VersionId: {2f038821-cf46-49d0-9397-2147ee1c462c}

    Later attempts even after an IISreset only produce this in the immediate window:

    ? m.getProperty("PreferdUserName")
    w3wp.exe Information: 0 : 75. RecordsReader created by Content.get_Version. Open Data Readers: 1
    w3wp.exe Information: 0 : 75. RecordsReader closed. Open Data Readers: 0
    w3wp.exe Information: 0 : 76. RecordsReader created by CMSNode.setupNode. Open Data Readers: 1
    w3wp.exe Information: 0 : 76. RecordsReader closed. Open Data Readers: 0
    w3wp.exe Information: 0 : 77. RecordsReader created by DataTypeDefinition.setupDataTypeDefinition. Open Data Readers: 1
    w3wp.exe Information: 0 : 77. RecordsReader closed. Open Data Readers: 0
    w3wp.exe Information: 0 : 78. RecordsReader created by Document.GetDocumentsForRelease. Open Data Readers: 1
    w3wp.exe Information: 0 : 78. RecordsReader closed. Open Data Readers: 0
    w3wp.exe Information: 0 : 79. RecordsReader created by Document.GetDocumentsForExpiration. Open Data Readers: 1
    w3wp.exe Information: 0 : 79. RecordsReader closed. Open Data Readers: 0
    w3wp.exe Information: 0 : 80. RecordsReader created by Document.GetDocumentsForRelease. Open Data Readers: 1
    w3wp.exe Information: 0 : 80. RecordsReader closed. Open Data Readers: 0
    w3wp.exe Information: 0 : 81. RecordsReader created by Document.GetDocumentsForExpiration. Open Data Readers: 1
    w3wp.exe Information: 0 : 81. RecordsReader closed. Open Data Readers: 0

     

    I read somewhere else that this getProperty Method will deprecated in the near future, (can't find the link now). Is there an alternative of doing this.. It should be so simple..

     

Please Sign in or register to post replies

Write your reply to:

Draft