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
Challenge:
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?
Solution:
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:
- Microsoft.Office.Server
- Microsoft.SharePoint
- System.Web
public UserProfile GetUserByAccountName(string strAccountName)
{
UserProfile upUser = null;
try
{
ServerContext context = ServerContext.Current;
UserProfileManager upm = new UserProfileManager(context);
upUser = upm.GetUserProfile(strAccountName); ;
}
catch (Exception ex)
{
//throw something
}
return upUser;
}
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:
- Microsoft.Office.Server
- Microsoft.Office.Server.UseProfiles
- Microsoft.SharePoint
- System.Web
public Microsoft.Office.Server.UserProfiles.UserProfile GetUserByAccountName(string strAccountName)
{
Microsoft.Office.Server.UserProfiles.UserProfile upUser = null;
try
{
System.Security.PermissionSet ps =
new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
ps.Assert();
Microsoft.SharePoint.SPSite siteColl = Microsoft.SharePoint.SPContext.Current.Site;
Microsoft.SharePoint.SPServiceContext serviceContext =
Microsoft.SharePoint.SPServiceContext.GetContext(siteColl);
Microsoft.Office.Server.UserProfiles.UserProfileManager upm =
new Microsoft.Office.Server.UserProfiles.UserProfileManager(serviceContext);
upUser = upm.GetUserProfile(strAccountName);
return upUser;
}
catch (Exception ex)
{
this.WriteErrorLog("GetUserByAccountName: " + ex.ToString());
}
finally
{
System.Security.CodeAccessPermission.RevertAssert();
}
return upUser;
}
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:
Console.WriteLine(propertyValue);
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:
private System.Collections.ArrayList GetArrValueOfUserField(
Microsoft.Office.Server.UserProfiles.UserProfile userInfo,
Microsoft.Office.Server.UserProfiles.Property proper)
{
System.Collections.ArrayList arrValue = new System.Collections.ArrayList();
try
{
System.Security.PermissionSet ps =
new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
ps.Assert();
bool isMultivalue = proper.IsMultivalued;
string separatorName = proper.Separator.ToString();
object a = userInfo[proper.Name].Value;
if (isMultivalue && (a != null))
{
Microsoft.Office.Server.UserProfiles.UserProfileValueCollection valueOfField = userInfo[proper.Name];
char separatorSymBol = GetSeparatorOfProperty(separatorName);
for (int i = 0; i < valueOfField.Count; i++)
{
object ieValue = valueOfField[ i ];
arrValue.Add(ieValue.ToString());
}
}
else
{
arrValue.Add(a.ToString());
}
}
catch (Exception ex)
{
this.WriteErrorLog("GetArrValueOfUserField: " + ex.ToString());
}
finally
{
System.Security.CodeAccessPermission.RevertAssert();
}
return arrValue;
}
We can also get the direct reports using the userInfo.GetDirectReports() method, and get manager by using the userInfo.getManager() method.
See Also