How to get a list of users with their profile information

Sep 8, 2011 at 7:43 AM

Hello,

I just downloaded profile module and played with it for a while. I like this little module. It basically fits my needs. But I have a specific requirement which is to generate an excel file contains all the registered users with their profile information.

I know I can use the following line to get all the users. But how can I get all the profile information of these users?

IContentQuery<UserPart, UserPartRecord> users = orchardServices.ContentManager
                .Query<UserPart, UserPartRecord>();

Thanks in advance.

Oct 19, 2011 at 7:59 PM
Edited Oct 19, 2011 at 8:00 PM

Paul,

One solution to access the data is to cast the returned records to type dynamic and access the properties you appended to the profile from there. As an example:

            var userProfiles = _contentManager.Query<UserPart, UserPartRecord>()
                .List()
                .Select(user => ((dynamic) user).ProfilePart)
                .Select(user => new {user.FirstName, user.LastName});

What this is doing is:
1. creating a query for users in the system
2. executing the query
3. taking the query results (UserPart) and converting it to dynamic, and further accessing the part ProfilePart
4. then projecting an anonymous type which includes two fields from the profile First and Last names

At the point you have an anonymous type you can use the data to export OR you could skip the anonymous type and use a declared class as a DTO and project from the ProfilePart into that object directly.

In the above scenario I have added fields directly to the ProfilePart in my migration. As an example:
            ContentDefinitionManager.AlterPartDefinition("ProfilePart", builder => builder
                .WithField("FirstName", cfg => cfg.OfType("TextField"))
                .WithField("LastName", cfg => cfg.OfType("TextField"))
                .WithField("Telephone", cfg => cfg.OfType("TextField"))
                .WithField("Company", cfg => cfg.OfType("TextField"))
                );
Dec 9, 2011 at 6:53 AM
jhersh wrote:

Paul,

One solution to access the data is to cast the returned records to type dynamic and access the properties you appended to the profile from there. As an example:

var userProfiles = _contentManager.Query<UserPart, UserPartRecord>() .List() .Select(user => ((dynamic) user).ProfilePart) .Select(user => new {user.FirstName, user.LastName}); 
 

I have tried this, give me error, it looks like user.FirstName returns filed rather than the value

Apr 24, 2013 at 12:46 PM
Edited Apr 24, 2013 at 12:48 PM
Hi, I use your recommendation to get all user profile data, it works flawlessly. I have a problem to show this data in my view:
Image
Image

I tried to send a viewmodel instead userProfiles var but i dont know resolve this issue. I only know it is a C# or autofac issue.

Thanks in advance.
May 14, 2015 at 11:32 AM
I have 6000+ users and im getting an exception
Too many parameters were provided...2100