This question was posted in MSDN forum. I did some research on this and got the logic to get it!
First, how do we determine if the user is a manager? In AD, every manager will have Direct Reports. This is the assumption I've made to write the logic. Let's see the code:
1. First you should add a reference to Microsoft.Office.Server
2. Define the namespace:
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
3. Add the below piece of code:
Here, it checks how many direct reports are there for the SPUser. If it is more than 0, then the person is manager.
Of course, we have to handle exception as it throws error if any SPUser does not have User Profile.
First, how do we determine if the user is a manager? In AD, every manager will have Direct Reports. This is the assumption I've made to write the logic. Let's see the code:
1. First you should add a reference to Microsoft.Office.Server
2. Define the namespace:
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
3. Add the below piece of code:
using (SPSite mySite = new SPSite("http://mysite"))
{
ServerContext context = ServerContext.GetContext(mySite);
UserProfileManager profileManager = new UserProfileManager(context);
using (SPWeb myWeb = mySite.OpenWeb())
{
foreach (SPUser user in myWeb.Users)
{
UserProfile profile = profileManager.GetUserProfile(user.LoginName);
if (profile.GetDirectReports().Length > 0)
//Manager!
else
// Not a manager
}
}
}
Here, it checks how many direct reports are there for the SPUser. If it is more than 0, then the person is manager.
Of course, we have to handle exception as it throws error if any SPUser does not have User Profile.
Comments
Post a Comment