
The SharePoint User Profile service application provides a means to offload the traffic on a domain controller when querying information in an Active Directory schema.
Better than that, SharePoint User Profiles work in a similar fashion to ADAM / AD LDS in that it hosts a separate instance of data from an Active Directory schema that can be synchronised on a scheduled basis and also modified and extended to allow additional metadata to be held against user accounts.
Accessing User Profile information is pretty straightforward and the most obvious route is via the UserProfileService.asmx SharePoint web service. If you’ve configured the user profiles service on the root SharePoint site, the web service URL should be of the form:
http://myserver/_vti_bin/UserProfileService.asmx
Code language: plaintext (plaintext)
If the the user profiles service is installed on a separate SharePoint site then the URL should be of the form:
http://myserver/sub-site/_vti_bin/UserProfileService.asmx
Code language: plaintext (plaintext)
The WSDL description for the web service can be retrieved by appending ?wsdl to the URL.
This article assumes you are developing a solution in Visual Studio (C#). Simply add a service reference using the URL to the web service on your SharePoint server and you are ready to go. In the example code below, the service reference is called UserProfiles.
using (UserProfiles.UserProfileServiceSoapClient search =
new UserProfiles.UserProfileServiceSoapClient())
{
search.ClientCredentials.Windows.AllowedImpersonationLevel =
System.Security.Principal.TokenImpersonationLevel.Impersonation;
search.ClientCredentials.Windows.ClientCredential =
System.Net.CredentialCache.DefaultNetworkCredentials;
// Search for a user by username.
foreach (UserProfiles.PropertyData element in
search.GetUserProfileByName(@"domain\username"))
{
// PropertyData is a key-value(s) pair where the name is the name of
// a user profile property and the value(s) are the data stored in the
// property. In most cases there will be one value.
}
// Retrieve information about users who report to the same manager.
foreach (UserProfiles.ContactData element in
search.GetCommonColleagues(@"domain\username"))
{
// Access the richly typed contact properties.
// For example, element.AccountName.
}
}
Code language: C# (cs)
You can also access User Profile data via the SharePoint API.
Development needs to be undertaken on a SharePoint server because you’ll need to reference SharePoint assemblies (Microsoft.Office.Server, Microsoft.Office.Server.UserProfiles and Microsoft.SharePoint), and utilise the SPServiceContext and UserProfileManager classes.
Before continuing with code examples, it is worth noting that using the API classes often need to be run in the SharePoint administrator account. To further complicate things, the API appears to undo any in-code user impersonation so the program connecting to SharePoint must run as that administrator account.
Also, you still have to explicitly grant permissions on the user profile service. Do this via SharePoint Central Admin… go to ‘manage service applications’, then select the user profile service app so the ribbon is displayed, choose administrators from the ribbon, and add your admin account to the list. Then choose Permissions and add your admin account there too. You should now have the permissions you need.
// Create a site context (the suffix in the URL may be different,
// depending on how the server was set up.)
SPServiceContext siteContext =
SPServiceContext.GetContext(new SPSite(@"http://sharepointServer/profiles"));
int count = 0;
UserProfileManager manager = new UserProfileManager(siteContext);
foreach (UserProfile profile in manager)
{
// Filter the results as required. The properties of the user profile can be
// accessed by name using profile["propertyName"],
// where some examples of property names are: AccountName, UserName,
// SPS-DistinguishedName, WorkEmail, Department, Manager, etc.
// Retrieve the user profiles of the users that report to the same manager.
foreach (Colleague colleague in profile.Colleagues.GetItems())
{
// The user profile of the colleague can be accessed quite simply as
// colleague.Profile.
}
}
Code language: C# (cs)