This project is read-only.

Dynamically select User information

Topics: General, Writing modules
Mar 29, 2013 at 5:02 AM
To find a users email, I would usually do this:
var user = _authenticationService.GetAuthenticatedUser();
var pop = user.As<UserPart>();
var email = pop.Email;
This works great but I was wondering if there was some way of doing this without explicitly in the code writing .Email; So I would like to be able to write pop.Select("Email"); allowing me to take anything from the user. Is this possible? Or should I be doing something else/not doing this at all?

The reason I am trying to do this is for producing registered user reports, where I won't necessarily know what fields/parts have been added to registration. I do have a working version, but it is so slow and horrible it makes me want to die.
Mar 29, 2013 at 5:08 AM
If you put the content item into a dynamic variable, then you can do userContentItem.WhateverPart.WhateverProperty. You can also use reflection.
Mar 29, 2013 at 5:23 AM
Edited Mar 29, 2013 at 5:34 AM
Hey, thanks for the reply. Yeah that is how I have been doing it, with the dynamic variable. So I have a list of all the properties of the user (name, telephone etc.). And I can use item.TelephonePart.Number, but I was wondering if there was another way I could do it so that if I added a new part to the registration, LocationPart for example, I wouldn't need to write another line in the module item.LocationPart.Country. If that makes sense...

And sorry to sound like such a retard, but reflection? I quickly googled it, not quite sure how I can use the type to help me here.

EDIT: Sorry I don't think I am being clear at all.

We have quite a few websites. They all have different registration fields. These are attached as parts to the user content type. I want to list registered users and all the details they have entered so evil PR people can get excited over all the user information.

At the moment, it goes and finds a list of all parts and fields associated with the user type. I now would like to get all the information from these parts without having to explicitly write
I think I have the fields working, it is just the Parts I am struggling with.

Sorry again for any confusion
Mar 29, 2013 at 5:49 AM
Okay so I think I was attempting what you suggested about reflection, I just didn't know that was what it was called. I was testing using the UserName variable. Turns out I was spelling UserName as Username and that was why it was failing. /facepalm

I have some other problems now with dynamic types but I don't deserve any help after that dreadful failure on my part. I will suffer dynamic types a while longer.

Cheers for the help!