Sharepoint User Profiles (Part 2)
Posted by ricetristan on January 18, 2008
In my previous post, I discussed the basics of SharePoint user profiles for both WSS and MOSS environments. As an addition to this discussion, I feel the use of MOSS web services for user profile interaction should be covered. With the addition of the MOSS infrastructure to your WSS server, you gain many additional features including a wide array of web services for remote interaction with the MOSS framework.
For this discussion, we’ll be dealing with the User Profile web service, which can be found at http://<moss-server>/_vti_bin/userprofileservice.asmx
This web service provides the following functionality:
- AddColleague
- AddLink
- AddMembership
- AddPinnedLink
- CreateMemberGroup
- CreateUserProfileByAccountName
- GetCommonColleagues
- GetCommonManager
- GetCommonMemberships
- GetInCommon
- GetPropertyChoiceList
- GetUserColleagues
- GetUserLinks
- GetUserMemberships
- GetUserPinnedLinks
- GetUserProfileByGuid
- GetUserProfileByIndex
- GetUserProfileByName
- GetUserProfileCount
- GetUserProfileSchema
- ModifyUserPropertyByAccountName
- RemoveAllColleagues
- RemoveAllLinks
- RemoveAllMemberships
- RemoveAllPinnedLinks
- RemoveColleague
- RemoveLink
- RemoveMembership
- RemovePinnedLink
- UpdateColleaguePrivacy
- UpdateLink
- UpdateMembershipPrivacy
- UpdatePinnedLink
In order to use this web service in a VS project, you would simply add a new Web Reference to your project, and point it to the url of the web service (similar to that shown above). Once you’ve done that, the following code shows and example of how to use this service to enumerate all MOSS profiles:
UserProfileService service = new UserProfileService(); service.Credentials = System.Net.CredentialCache.DefaultCredentials; int numProfiles = (int)service.GetUserProfileCount(); for (int i = 0; i < numProfiles; i++) { Console.WriteLine(service.GetUserProfileByIndex(i).UserProfile[1].Values[0].Value); }
In the code above, the values displayed would be the Windows domain accounts (such as DOMAIN\user), and the credentials used when calling the web service must have the appropriate permissions to view user profiles. Assuming the permissions are correct, you will see all user profiles for the MOSS site. Keep in mind, however, that these are different than the simple WSS profiles for each Site Collection (see the previous part of this post for details).
What happens, you might ask, if I am using forms-based authentication rather than Windows authentication for my site? In that case, you will need to make use of the Authentication web service, which resides at http://<moss-server>/_vti_bin/authentication.asmx and provides the following methods:
- Login
- Mode
This service is used by passing a username/password to the Login method, and grabbing the ASP.NET forms auth cookie that is returned in the response. This cookie can then be used for calls to any of the other web services. As usual, a few lines of codes speak louder than my words:
// First authenticate against the auth service Authentication auth = new Authentication(); auth.CookieContainer = new System.Net.CookieContainer(); LoginResult res = auth.Login(activeUser.UserPrincipalName, login.Password); if (res.ErrorCode == LoginErrorCode.NoError) { // If that succeeds, obtain auth cookie System.Net.CookieCollection cookies = auth.CookieContainer.GetCookies(new Uri(auth.Url)); System.Net.Cookie authCookie = cookies[res.CookieName]; // Now call the whatever service you want, and include the cookie UserProfileService service = new UserProfileService(); service.CookieContainer = new System.Net.CookieContainer(); service.CookieContainer.Add(authCookie); //... same as previous example ... }
That pretty much covers the user profile web service interface. Along with the methods listed above, there is a UserProfileChangeService that provides access to a change-history for the MOSS user profiles. This is not, however, essential to your understanding of this topic, so it has been left for your own study.
-Tristan
June 4, 2008 at 7:26 am
hi there,
can i know where to put all this coding???