Monday, June 06, 2011

Get changed user profiles in SharePoint using C#

Get all the user profiles/properties which are modified since yesterday date or any datetime interval:

With the help of UserProfileManager class (Microsoft.Office.Server.UserProfiles), we can access properties of user profile and changed profile which are modified since yesterday date or any datetime interval.
In this example we will access different components of UserProfileManager :


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;
using System.Collections;

namespace UserProfilesOMApp
{
class Program
{
static void Main(string[] args)
{

using (SPSite site = new SPSite("http://amitkumarmca04.blogspot.com"))
{
ServerContext context = ServerContext.GetContext(site);
UserProfileManager profileManager =
new UserProfileManager(context);

//Fetch all user profile properties
Microsoft.Office.Server.UserProfiles.PropertyCollection props = profileManager.Properties;
//Print all user profile properties


Console.WriteLine("_______________________Start:Print all user profile properties.");

foreach (Property prop in props)
{
Console.WriteLine(prop.Name);
}
Console.WriteLine("_______________________End:Print all user profile properties.");


//Find user profile changes.

// this gets some subset of changes to a user profile, changed 5 days before
DateTime startDate =
DateTime.UtcNow.Subtract(TimeSpan.FromDays(5));

/* If we want to access changes after "4/30/2011 11:33:47 PM" then we can use below mentioned code:

StartTime="5/30/2010 11:33:47 PM"
System.TimeSpan dateDiff = DateTime.UtcNow.Subtract(Convert.ToDateTime(StartTime));
DateTime startDate = DateTime.UtcNow.Subtract(dateDiff);
*/

UserProfileChangeQuery changeQuery = new UserProfileChangeQuery(true, true);
changeQuery.ChangeTokenStart = new UserProfileChangeToken(startDate);
UserProfileChangeCollection changes = profileManager.GetChanges(changeQuery);
//Used to store unique user names
ArrayList arrLstUsers = new ArrayList();
foreach (UserProfileChange change in changes)
{
if (arrLstUsers != null && !arrLstUsers.Contains(change.AccountName))
{
//Add user to array list
arrLstUsers.Add(change.AccountName);
//Print chagned profile user name
Console.Write(change.AccountName);
}
}
Console.Read();
}
}
}
}
Share:

0 comments:

About Me

My photo
Based in Bangalore, INDIA, Amit is a MCC. He has an MCA from the U.P.T.U., INDIA and holds Microsoft certifications including SharePoint 2007/2010/2013. He is an contributor to MSDN and http://amitkumarmca04.blogspot.com/. Amit have experience in SharePoint,ASP.Net with C#, Classic ASP, VB,Macros,HTML,JavaScript,JSON,BootStrap and AngularJS.

Followers

Labels