How to track the changes of user contacts(colleagues) by using SharePoint API




We might have seen colleague tracker web part which keep tracks the changes made in our contacts/colleagues details like profile/lists/sites. The same tracking mechanism can be implemented by using MOSS API in any custom web part/ web control/ feature/web service...

UserProfile.GetColleagueChanges is the method which returns all changes happened with the contacts, this method also accepts a Query (UserProfileChangeQuery) where we can specify different parameters to filter the changes


I have given below the example to use GetColleagueChanges method


UserProfileManager userProfileManager = new UserProfileManager(ServerContext.Default);
UserProfile userProfile = userProfileManager.GetUserProfile("MyLoginName");

//Change the start date to get the changes from certain date
DateTime startDate = DateTime.Today;

UserProfileChangeQuery userProfileChangeQuery = new UserProfileChangeQuery(true, true);
UserProfileChangeToken userProfileChangeToken = new UserProfileChangeToken(startDate);
userProfileChangeQuery.ChangeTokenStart = userProfileChangeToken;
userProfileChangeQuery.UserProfile = true;
userProfileChangeQuery.PersonalizationSite = true;
userProfileChangeQuery.Update = true;
userProfileChangeQuery.Add = true;
userProfileChangeQuery.Delete = true;
userProfileChangeQuery.SingleValueProperty = true;
userProfileChangeQuery.MultiValueProperty = true;
userProfileChangeQuery.Colleague = true;
userProfileChangeQuery.SiteMembership = true;
userProfileChangeQuery.Anniversary = true;

UserProfileChangeDictionary userProfileChangeDictionary = userProfile.GetColleagueChanges(userProfileChangeQuery);

Dictionary.Enumerator userProfileChangeCollection = userProfileChangeDictionary.GetEnumerator();
while (userProfileChangeCollection.MoveNext())
{
UserProfileChangeCollection userProfileChanges = userProfileChangeCollection.Current.Value;
foreach (UserProfileChange userProfileChange in userProfileChanges)
{
if (userProfileChange is UserProfileSingleValueChange)
{
UserProfileSingleValueChange propertyChange = (UserProfileSingleValueChange)userProfileChange;
//propertyChange will have the new and old value of profile fileds
}
else if (userProfileChange is UserProfileMultiValueChange)
{
UserProfileMultiValueChange propertyChange = (UserProfileMultiValueChange)userProfileChange;
//propertyChange will have the new and old value of profile fileds
}
else if (userProfileChange is UserProfileWebLogChange)
{
UserProfileWebLogChange listChange = (UserProfileWebLogChange)userProfileChange;
//listChange will have the newly added/modifed list item url
}
}
}


Above code will return all the changes happened in profiles/lists/sites for all contacts/colleagues....