This tip demonstrates how to find different kinds of hierarchy existing within user profiles.
For example
You can find immediate manager of a particular user.
All the users who reports the same manager.
All the managers above in the chain for a particular user
Find Manager for currently logged in user.
private void GetManegerProfile() { SPSite site; SPWeb web; SPServiceContext serviceContext; UserProfileManager profiles; UserProfile profile;
try
{ using (site = new SPSite(SPContext.Current.Web.Url)) { using (web = site.OpenWeb()) { serviceContext = SPServiceContext.GetContext(site); profiles = new UserProfileManager(serviceContext); //Getting the profile of logged in user. profile = profiles.GetUserProfile(web.CurrentUser.LoginName); //Getting profile of the manager of the logged in user. profile = profile.GetManager(); } } } catch { } }
Find all the user who reports the same manager as logged in user.
private void GetManegerProfile() { SPSite site; SPWeb web; SPServiceContext serviceContext; UserProfileManager profiles; UserProfile profile; List<UserProfile> peers; try { using (site = new SPSite(SPContext.Current.Web.Url)) { using (web = site.OpenWeb()) { serviceContext = SPServiceContext.GetContext(site); profiles = new UserProfileManager(serviceContext); //Getting the profile of logged in user. profile = profiles.GetUserProfile(web.CurrentUser.LoginName); //Getting peers of the logged in user. peers = new List<UserProfile>(profile .GetPeers()); } } } catch { } }
Find all the managers above in the chain for the logged in user.
private void GetManegerProfile() { SPSite site; SPWeb web; SPServiceContext serviceContext; UserProfileManager profiles; UserProfile profile; List<UserProfile> managers; try { using (site = new SPSite(SPContext.Current.Web.Url)) { using (web = site.OpenWeb()) { serviceContext = SPServiceContext.GetContext(site); profiles = new UserProfileManager(serviceContext); //Getting the profile of logged in user. profile = profiles.GetUserProfile(web.CurrentUser.LoginName); //Getting manager profiles of the logged in user. managers =new List<UserProfile>( profile.GetManagers()); } } } catch { } }