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.
-
SharePoint on premises OR SharePoint online and Are u using c#?– SP DeveloperSep 16, 2016 at 6:36
-
SharePoint on Premises and using C# language.– RamSep 16, 2016 at 6:49
-
Which language you are using to develop webpart?– SP DeveloperSep 16, 2016 at 6:51
-
Ram,what you have tried so far?– Akshay BelureSep 16, 2016 at 6:55
-
@Akshay, Working on msdn.microsoft.com/en-us/library/office/jj163182.aspx article.– RamSep 16, 2016 at 7:28
|
Show 2 more comments
1 Answer
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)
{
}
-
#Vishal, I have made little bit modification in the question, please check it. Thanks for your feedback.– RamSep 16, 2016 at 10:52
-
-
1We 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(); } }– RamSep 16, 2016 at 11:11