Retrieving User Profiles from SharePoint 2013

To demonstrate this scenario, I will retrieve user profiles and store it into GridView.
 
Here is my .aspx code
  1. <table style="width: 900px; padding: 20px;">  
  2.     <tr>  
  3.         <td>Find profiles :</td>  
  4.         <td>  
  5.             <asp:TextBox runat="server" ID="txtUserID"></asp:TextBox>  
  6.             <asp:Button runat="server" ID="btnSearch" Text="Find" OnClick="FindUserProfile" />  
  7.         </td>  
  8.     </tr>  
  9. </table>  
  10. <div id="divUserProfilesGrid" runat="server">  
  11.     <hr />  
  12.     <p>Following user profiles are found by this search :</p>  
  13.     <br />  
  14.     <div style="height: auto;overflow-y: auto;max-height:215px">  
  15.         <asp:GridView ID="grdUserProf" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">  
  16.             <AlternatingRowStyle BackColor="White" />  
  17.             <EditRowStyle BackColor="#2461BF" />  
  18.             <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
  19.             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />  
  20.             <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />  
  21.             <RowStyle BackColor="#EFF3FB" />  
  22.             <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />  
  23.             <SortedAscendingCellStyle BackColor="#F5F7FB" />  
  24.             <SortedAscendingHeaderStyle BackColor="#6D95E1" />  
  25.             <SortedDescendingCellStyle BackColor="#E9EBEF" />  
  26.             <SortedDescendingHeaderStyle BackColor="#4870BE" />  
  27.         </asp:GridView>  
  28.     </div>  
  29.     <hr />  
  30. </div>  
For this scenario we need to add references to assembly.
  1. using Microsoft.SharePoint;  
  2. using Microsoft.Office.Server;  
  3. using Microsoft.Office.Server.UserProfiles;  
On the click of Find button we are executing following code. 
  1. SPServiceContext serviceContext1 = SPServiceContext.GetContext(SiteUrl);  
  2. UserProfileManager upm = new UserProfileManager(serviceContext1);  
Here in above line we are creating object of SPServiceContext class that takes site collection url as parameter and creating UserProfileManager class to use that service contest.
  1. string EnterSearchData = _filterName;// txtUserID textbox value  
  2. string[] searchPattern = EnterSearchData.Split(',').ToArray<string>();  
This is the array of string (i.e. search data) 
  1. ProfileSearchManager sp = ProfileSearchManager.GetProfileSearchManager(serviceContext1);  
  2. ProfileBase[] baseArray = sp.Search(searchPattern, ProfileSearchFlags.User);  
ProfileSearchManager class is available in Microsoft.Office.Server.UserProfiles assembly which provides the search method which take string array and ProfileSearchFlags.User this ENUM value.
 
Now we are taking value from user profile and assigning to data table on row by row basis.
  1. if (baseArray != null)   
  2. {  
  3.     DataTable table = new DataTable();  
  4.     table.Columns.Add("AccountName"typeof(string));  
  5.     table.Columns.Add("PreferredName"typeof(string));  
  6.     table.Columns.Add("Email"typeof(string));  
  7.     table.Columns.Add("Title"typeof(string));  
  8.     table.Columns.Add("Department"typeof(string));  
  9.     foreach(ProfileBase base2 in baseArray)   
  10.     {  
  11.         DataRow row = table.NewRow();  
  12.         UserProfile profile = base2 as UserProfile;  
  13.         if (profile["AccountName"] != null)   
  14.         {  
  15.             row["AccountName"] = profile["AccountName"].Value;  
  16.         }  
  17.         if (profile["PreferredName"] != null)   
  18.         {  
  19.             row["PreferredName"] = profile["PreferredName"].Value;  
  20.         }  
  21.         if (profile["WorkEmail"] != null)   
  22.         {  
  23.             row["Email"] = profile["WorkEmail"].Value;  
  24.         }  
  25.         if (profile["Title"] != null)   
  26.         {  
  27.             row["Title"] = profile["Title"].Value;  
  28.         }  
  29.         if (profile["Department"] != null)   
  30.         {  
  31.             row["Department"] = profile["Department"].Value;  
  32.         }  
  33.         table.Rows.Add(row);  
  34.     }  
  35.     grdUserProf.DataSource = table;  
  36.     grdUserProf.DataBind();  
  37. }  
Following  is the screenshot for result.