共用方式為


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

Applies to: SharePoint Server 2010

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

  • Their common manager

  • The distribution lists and Microsoft SharePoint 2010 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.Office.Server.UserProfiles

  • 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("https://servername"))
            {
                SPServiceContext context = SPServiceContext.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

Tasks

How to: Retrieve a User Profile

How to: Retrieve User Profile Properties

How to: Get Recent User Profile Changes Using the Change Log

How to: Use the Web Service to Find What's Common Between Two User Profiles