Using LINQ with the MOSS UserProfileManager
Ok, I know… it’s waaaaay to long since my last blogpost. I could tell you about how busy I’ve been, what projects I did, etc… But I just want to blog cool stuff
Lately I’m working a lot with LINQ and al types like LINQ to XML, LINQ to XSD, LINQ to objects, etc. Today I had to build some functionality where I had to split users in 2 groups based on a profile property.
The users I have to query are stored in a DataView (represented by the variable webUsers) so that’s my starting point. First I have to create an instance of the UserProfileManager. This is my connection with the profile database.
I iterate the DataView and Cast that to the row variable. In the DataRowView the AccountName column contains domain\user value. Because it’s an object and I need it multiple times I store it in a locale variable using the let keyword so I only have to cast it one time.
One of the great things is that you are allowed to combine multiple let’s and where statements. So every AccountName that’s null or empty will be filtered out. If I would user thet GetUserProfile method and pass an account which is not (yet) present in the profile database this would result in an exception, that’s why I first use a where query in combination with the UserExists method.
For readability I use another let to store the value I need in the comparison. Now I can create an anonymous object with a boolean if the title was equal to “student”, and some other profile properties.
var profileManager = new UserProfileManager(); var users = (from DataRowView row in webUsers let accountName = (string)row["AccountName"] where !string.IsNullOrEmpty(accountName) where profileManager.UserExists(accountName) let userProfile = profileManager.GetUserProfile(accountName) let title = (userProfile["Title"] != null) ? (string)userProfile["Title"].Value : string.Empty select new { IsStudent = title == "student", Initials = (userProfile["Initials"] != null) ? (string)userProfile["Initials"].Value : string.Empty, LastName = (userProfile["LastName"] != null) ? (string)userProfile["LastName"].Value : string.Empty, }).AsEnumerable(); var employees = from employee in users where !employee.IsStudent select employee; var students = from student in users where student.IsStudent select student;
Now I can easily get all students and employees in different collections based on that boolean property. And never call the ProfileManager more then necessary.




Hello there!
That is a nice post!
But I have a question. I need to do a search into my UserProfiles to find whom i want, but this in my SSP i have more than 9thousand users sincronized with AD.
how could i make this search into these all users from SSP without crash my connection. lol
see my code (very simple):
UserProfileManager allUsers = new UserProfileManager(context);
List fullSet = new List();
foreach (UserProfile user in allUsers)
{
fullSet.Add(user);
}
return from user in fullSet
where (user["FirstName"].Value != null) ? user["FirstName"].Value.ToString().ToLower().Contains(Nome.ToLower()) : user["AccountName"].Value.ToString().ToLower().Contains(Nome.ToLower())
select user;
Thx anyway!
(sorry about my english… just learning yet! =D)