2 minute read

In SharePoint each user has a User profile and has the ability to change profile properties. In certain situations you would like to see what the users has changed on their profile, but how do you accomplish this? Within SharePoint the user profile manager or the user profile of a user has a collection called 'UserProfileChangeCollection' in this collection you can find a complete log of changed user profiles. When a change is for example of the type 'UserProfileSingleValueChange' you can read out the values that have been changed in the profile. Beneath here is a little example to read out all of the changes:

 using (SPSite site = new SPSite("yoursite"))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(site.WebApplication.ServiceApplicationProxyGroup, SPSiteSubscriptionIdentifier.Default);
                UserProfileManager profileConfigManager = new UserProfileManager(serviceContext);
                try
                {
                    UserProfileChangeCollection collection = profileConfigManager.GetChanges(query);
                    foreach (UserProfileChange change in collection)
                    {
                        Console.WriteLine(change.AccountName);
                        Console.WriteLine(change.ChangedProfile);
                        Console.WriteLine(change.ChangeType);
                        Console.WriteLine(change.EventTime);
                        Console.WriteLine(change.Id);
                        Console.WriteLine(change.ObjectType);

                        if (change is UserProfileSingleValueChange)
                        {
                            UserProfileSingleValueChange propertyChange = (UserProfileSingleValueChange)change;
                            Console.WriteLine(propertyChange.ProfileProperty.Name);
                        }
                    }
                }
                catch (UserNotFoundException)
                {
                }

With this you have the ability to find all of the changes that have occurred. But this can become a great list of changes. To find the changes you want you can also specify a UserProfileChangeQuery.

 using (SPSite site = new SPSite("yoursite"))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(site.WebApplication.ServiceApplicationProxyGroup, SPSiteSubscriptionIdentifier.Default);
                UserProfileManager profileConfigManager = new UserProfileManager(serviceContext);
                try
                {
                    UserProfileChangeQuery query = new UserProfileChangeQuery(true, true);
                    query.Add = false;
                    query.Custom = false;
                    query.Delete = false;
                    query.MultiValueProperty = false;
                    query.SingleValueProperty = true;
                    query.Update = true;
                    query.UpdateMetadata = true;
                    
                    UserProfileChangeCollection collection = profileConfigManager.GetChanges(query);
                    foreach (UserProfileChange change in collection)
                    {
                        Console.WriteLine(change.AccountName);
                        Console.WriteLine(change.ChangedProfile);
                        Console.WriteLine(change.ChangeType);
                        Console.WriteLine(change.EventTime);
                        Console.WriteLine(change.Id);
                        Console.WriteLine(change.ObjectType);

                        if (change is UserProfileSingleValueChange)
                        {
                            UserProfileSingleValueChange propertyChange = (UserProfileSingleValueChange)change;
                            Console.WriteLine(propertyChange.ProfileProperty.Name);
                        }                       
                    }
                }
                catch (UserNotFoundException)
                {
                }
            }

 

On the query object you can set several properties to find the changes you want. The properties you can define you can find here. Happy SharePointing!