Simplicity is prerequisite for reliability

First, solve the problem. Then, write the code.

  • Subscribe

  • Advertisements
  • January 2008
    M T W T F S S
        Apr »
     123456
    78910111213
    14151617181920
    21222324252627
    28293031  

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

Advertisements

5 Responses to “Sharepoint User Profiles (Part 2)”

  1. hidemoto said

    hi there,

    can i know where to put all this coding???

  2. CJ said

    great work. thanks for the examples.
    One thing to look out for is users running this code that aren’t administrators. The GetUserProfileCount method needs administrator privileges.
    cheers
    CJ

  3. Ariel said

    Hi, if I would like to run the code to enumarete the different profiles, should I be site collection administrator? Of what site collection? What specific permissions should I have in order to avoid being server administrator?

    Thanks a lot.

  4. Vlad said

    Hi,
    How can I obtain activeUser and login from LoginResult res = auth.Login(activeUser.UserPrincipalName, login.Password)?

    Thanks.

    PS Great post !

  5. […] https://usingsystem.wordpress.com/2008/01/18/sharepoint-user-profiles-part-2/#comments […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: