1

I have to get all the users from User Profiles. I have to implement search on those users on the basis of First Name & Last Name. This question may be repeated but I don't get use-full solution so posted.

7

1 Answer 1

2

Add Assembly : Microsoft.Office.Server.UserProfiles.dll

Add Namespace : using Microsoft.Office.Server.UserProfiles;

try
{
    using (SPSite site = new SPSite(SPContext.Current.Web.Url))
    {
        using (SPWeb web = site.OpenWeb())
        {
            ServerContext context = ServerContext.GetContext(site);
            UserProfileManager profileManager = new UserProfileManager(context);
            foreach (UserProfile usrprofile in profileManager)
           {
                if (usrprofile.Properties.GetPropertyByName("FirstName") != null)
                {
                    string firstName = Convert.ToString(usrprofile["FirstName"].Value);
                }
                if (usrprofile.Properties.GetPropertyByName("LastName") != null)
                {
                    string lastName  = Convert.ToString(usrprofile["LastName"].Value);
                }           
           }
        }
    }
}
catch (Exception)
{
}
3
  • #Vishal, I have made little bit modification in the question, please check it. Thanks for your feedback.
    – Ram
    Sep 16, 2016 at 10:52
  • That is implemented :)
    – Ram
    Sep 16, 2016 at 11:09
  • 1
    We can do that like this also- var userProfiles = profileManager.GetEnumerator(); while (userProfiles.MoveNext()) { if (userProfile["FirstName"].Value != null) { firstName = userProfile["FirstName"].Value.ToString(); } if (userProfile["LastName"].Value != null) { lastName = userProfile["LastName"].Value.ToString(); } }
    – Ram
    Sep 16, 2016 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.