Share via


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

Applies to: SharePoint Server 2010

Microsoft SharePoint Server 2010 offers a change tracking object model for the user profile store to support person-based alerts. These alerts notify you of anniversaries and many other changes in a user profile. SharePoint Server 2010 treats all date fields in the user profile as anniversaries. When a date in the user's profile matches the current date (based on server time), SharePoint Server 2010 creates a new anniversary event. Note that changes to a date field do not create an event.

The implementation of the change tracking object model is similar to, but less robust than, the Microsoft SharePoint Foundation 2010SPChange object. The UserProfile class defines two overloaded methods to support change tracking. These are:

  • public Microsoft.Office.Server.UserProfiles.UserProfileChangeCollection GetChanges(Microsoft.Office.Server.UserProfiles.UserProfileChangeQuery)

  • public Microsoft.Office.Server.UserProfiles.UserProfileChangeCollection GetColleagueChanges (Microsoft.Office.Server.UserProfiles.UserProfileChangeQuery)

Use the GetChanges method of the UserProfile object to return the collection of changes that have occurred in the user profile in a given time frame. An SPChange object contains information about the type of change, as represented by the ChangeType enumeration. The ChangeType value indicates the type of change, including adding, updating, deleting, or renaming changes.

UserProfileChangeQuery is an enumeration that describes the change event you are interested in. For example, it can be:

  • DistributionListMembership

  • Colleague

  • QuickLink

  • Anniversary

  • ProfileProperty

  • UserProfile

  • PrivacyItem

  • SingleValueProperty

  • MultivalueProperty

  • SiteMembership

The following code example shows you how to use the GetChanges() method to find what changed in a user profile in the last five days. Note that the GetColleagueChanges() method works the same way and returns the changes in the user's colleague's profile.

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

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UseProfiles

  • 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 UserProfilesOMApp
{
    class Program
    {
        static void Main(string[] args)
        {

            using (SPSite site = new SPSite("https://servername"))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                UserProfileManager profileManager = 
                    new UserProfileManager(context);

                // this gets some subset of changes to a user profile

                DateTime startDate = 
                    DateTime.UtcNow.Subtract(TimeSpan.FromDays(5));
                UserProfileChangeQuery changeQuery = 
                    new UserProfileChangeQuery(false, true);
                UserProfileChangeToken changeToken = 
                    new UserProfileChangeToken(startDate);
                changeQuery.ChangeTokenStart = changeToken;
                changeQuery.Anniversary = true;
                changeQuery.SingleValueProperty = true;
                changeQuery.MultiValueProperty = true;
                changeQuery.DistributionListMembership = true;
                changeQuery.SiteMembership = true;

                UserProfileChangeCollection changes = 
                    profileManager.GetUserProfile
                    ("domainname\\username").
                    GetChanges(changeQuery);
                foreach (UserProfileChange change in changes)
                {
                    Console.WriteLine(change.EventTime.ToString());
                    if (change is UserProfilePropertyValueChange)
                    {
                        UserProfilePropertyValueChange propertyChange = 
                            (UserProfilePropertyValueChange)change;
                            Console.WriteLine(propertyChange.
                            ChangeType.ToString());
                    }
                    else if (change is UserProfileMembershipChange)
                    {
                        UserProfileMembershipChange membershipChange = 
                            (UserProfileMembershipChange)change;
                            Console.WriteLine(membershipChange.
                            ChangeType.ToString());
                    }

                }
                Console.Read();
               }
            }
        }
    }

See Also

Tasks

How to: Retrieve a User Profile

How to: Retrieve User Profile Properties

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