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....





Power of using Clause in SharePoint



Power of using Clause in SharePoint


The Using Clause will help to avoid memory leaks in SharePoint by automatically Disposing the MOSS objects.

For example when we create SPSite and SPWeb objects, if we don't dispose it explicitly it might create memory leaks,

the given below code is not a good practice as it might trigger memory leakage...

SPSite spSite = new SPSite("http://mysharepointserver");
SPWeb spWeb = spSite.OpenWeb();
SPUser spUser = spSite.SystemAccount;


the same code can be written with using clause like below to avoid memory leakage...

using (SPSite spSite = new SPSite("http://mysharepointserver"))
{
SPWeb spWeb = spSite.OpenWeb();
SPUser spUser = spSite.SystemAccount;
}


more details on avoiding memory leak on different situations can be found here