This is the second of a four-part series of blog entries in which I will describe how to read and modify SharePoint UserProfile using the SharePoint Object Model.
The series will consist of the following:
- Part 1: How to Check Admin Rights and Get All UserProfile Properties
- Part 2: How to Get the UserProfile Object
- Part 3: How to Update UserProfile Properties
- Part 4: How to Push Changes in Active Directory to the SharePoint User Profile
Both Microsoft Office SharePoint Server 2007 and Microsoft SharePoint Server 2010 offer a rich object model for programmatically manipulating user profiles, but what are the differences between the two versions, and how does one accomplish user profile manipulation in 2010?
What's the relevant differences between SharePoint 2007 and SharePoint 2010?
In MOSS 2007, you use the Microsoft.Office.Server.UserProfiles namespace in Microsoft.Office.Server.dll to work with a user's profile. In SharePoint Server 2010, however, you should use the Microsoft.Office.Server.UserProfiles namespace in Microsoft.Office.Server.UserProfiles.dll (the same namespace but with a different assembly). The Microsoft.Office.Server.UserProfiles namespace contains two new types, and classes for creating and managing UserProfile. Note: The OrganizationProfile class is equivalent to UserProfile. This class enables you to group and distinguish users by membership in organizations that have properties equivalent to those that a UserProfile object would have. You manage this class by using the OrganizationProfileManager class. Another new class, ProfileSubtype, enables you to create role-specific properties for any type of profile. For example, this class can distinguish users who are customers from those who are employees, or between employees who perform different tasks in an organization. Use the ProfileSubtypeManager class to manage ProfileSubtypes, and use the ProfileSubtypePropertyManager class to manage properties for ProfileSubtypes. Although the Property class is still available for backward compatibility, you can now set profile properties by using additional classes, such as CoreProperty, ProfileTypeProperty, and ProfileSubtypeProperty.
How to retrieve UserProfiles
First, make sure that you have the admin right to retrieve UserProfiles, please refer to part one of this series, How to Check Admin Rights and Get All UserProfile Properties, for more details.
1. Getting the UserProfile Object in Office SharePoint Server 2007
You should add the following references to your Microsoft Visual Studio project before running the code example:
2. Getting the UserProfile Object in Sharepoint Server 2010
Here is the new way to write code by using the User Profiles object model in SharePoint Server 2010. You should add the following references to your Microsoft Visual Studio project before running the code example:
3. How to retrieve a user profile property value in Office SharePoint Server 2007 and SharePoint Server 2010
The common way to retrieve a user profile property value is via the following approach:
But in some cases, such as the multiple value property, we can't get the correct value using that approach. In such cases, the way to retrieve the correct value is:
We can also get the direct reports using the userInfo.GetDirectReports() method, and get manager by using the userInfo.getManager() method.