How to: Retrieve What's Common Between Two User Profiles

When a user visits another user’s Profile page, Microsoft Office SharePoint Server 2007 shows all the commonalities between the two users, such as:

  • Their common manager

  • The distribution lists and Windows SharePoint Services 3.0 sites they are both a member of

  • The colleagues they have in common

The object model allows you to find commonalities between any two user profile instances. The following code example shows you how to find the common memberships and the common manager of the specified user and the currently logged-on user.

Before using the code example, replace servername, domainname, and username with actual values. Also add references to the following in your Microsoft Visual Studio project:

  • Microsoft.Office.Server

  • Microsoft.SharePoint

  • System.Web

Example

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;

namespace UserProfilesApp
{
    class Program
    {
        static void Main(string[] args)
        {

            using (SPSite site = new SPSite("http://servername"))
            {
                ServerContext context = ServerContext.GetContext(site);
                UserProfileManager profileManager =
                    new UserProfileManager(context);
                string sAccount = "domainname\\username";
                UserProfile u =
                    profileManager.GetUserProfile(sAccount);

                //Get common memberships
                MemberGroup[] mem =
                    u.Memberships.GetCommonMemberships();
                Console.WriteLine(mem[0].DisplayName);

                //Get common manager
                UserProfile manager = u.GetCommonManager();
                Console.WriteLine(manager["DisplayName"]);

            }

        }
    }
}

See Also

Community Additions

ADD
Show: