Tuesday, 23 September 2014

Reading Change Log for a User Profile in Sharepoint


Nice Article by Sandeep

http://snahta.blogspot.in/2008/09/reading-change-log-for-user-profile.html


using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

namespace ChangeLog
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (SPSite spSite = new SPSite(@"http://localhost"))
                {
                    ServerContext siteContext = ServerContext.GetContext(spSite);
                    UserProfileManager pmManager = new UserProfileManager(siteContext);

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

                    // Display the changes that have occurred in the last month
                    DateTime dtNumberDays =
                             DateTime.UtcNow.Subtract(TimeSpan.FromDays(30));
                    // Create a change token to compare the number of days
                    UserProfileChangeToken upChangeToken = 
                                new UserProfileChangeToken(dtNumberDays);

                    // Create a query object to determine which changes are displayed
                     UserProfileChangeQuery QueryAnniversayAndColleagues = 
                               new UserProfileChangeQuery(false, true);
                    QueryAnniversayAndColleagues.ChangeTokenStart = upChangeToken;
                    QueryAnniversayAndColleagues.Anniversary = true;
                    QueryAnniversayAndColleagues.Colleague = true;

                    string strUserName = "domain\\sandeep";

                    if (pmManager.UserExists(strUserName))
                    {
                        UserProfile spMyUser = pmManager.GetUserProfile(strUserName);

                        UserProfileChangeCollection MyUserProfileChanges =                                     spMyUser.GetChanges(QueryAnniversayAndColleagues);

                        foreach (UserProfileChange MyUserChange in MyUserProfileChanges)
                        {
                            Console.WriteLine(MyUserChange.EventTime.ToString());
                            if (MyUserChange is UserProfileAnniversaryChange)
                            {
                                UserProfileAnniversaryChange AnniversryEvent =
                                      (UserProfileAnniversaryChange)MyUserChange;
                                Console.WriteLine(AnniversryEvent.Anniversary);
                                Console.WriteLine(AnniversryEvent.ProfileProperty);
                            }
                            else if (MyUserChange is UserProfileColleagueChange)
                            {
                                UserProfileColleagueChange ColleagueEvent = (UserProfileColleagueChange)MyUserChange;
                            }
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
        }
    }
}


Reference

http://snahta.blogspot.in/2008/09/reading-change-log-for-user-profile.html

No comments:

Post a Comment