1

I have to fetch the User Profiles list with filters like - First_Name starts with A or Department_Name = "Test" and get the count of those users and all users without Search Service/KQL. Simply I have to fire an query (Like- SQL/CAML) to get the list of users profiles. I have to implement this in the Custom WebPart.

1
  • (msdn.microsoft.com/en-us/library/office/…) KQL is the option but it uses the search service and needed the Search Service started. Do we have any other option for the same without using search with programmatically?
    – Ram
    Sep 23, 2016 at 10:55

1 Answer 1

1

Profile Manager Search method searches 'FirstName', 'LastName', 'PreferredName', 'UserName', 'Office', 'Title', 'Department', 'WorkEmail', 'SPS-SipAddress', and 'AccountName'. The method performs a "begins with" sort of search, so you get a match when the search pattern strings match the first characters in any of the searchable properties.

This is how you can search for anything starting with "A" programmatically:

using (SPSite site = new SPSite("https://spsite.yourcompany.com/"))
{
  SPServiceContext context = SPServiceContext.GetContext(site);
  UserProfileManager profileManager = new UserProfileManager(context);
  string searchPattern = "A";
  ProfileBase[] EmpSrchResults = profileManager.Search(searchPattern);
  foreach (ProfileBase EmpProfile in EmpSrchResults)
  {
      Console.WriteLine(EmpProfile.DisplayName);
  }
}   

Another alternative is to use PowerShell to get all your results filtered at once and write the results to a CSV file as shown below :

$siteUrl = "https://spsite.yourcompany.com/"
$outputFile = "D:\Shared\CSVProfiles\$(get-date -uformat '%Y-%m-%d-%H-%M-%S-%p').csv"

$serviceContext = Get-SPServiceContext -Site $siteUrl
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);
$profiles = $profileManager.GetEnumerator() | Sort -property  @{Expression={$_["FirstName"]}} | ?{$_["FirstName"] -like "A*"}

#Following line will give you the count of the filtered results
$count=$profiles.Count

Write-Host "Exporting profiles" 

$collection = @()
foreach ($profile in $profiles) 
{  
 $profileData = "" | select "AccountName","FirstName", "LastName"
 $profileData.AccountName = $profile["AccountName"].Value
 $profileData.FirstName = $profile["FirstName"].Value
 $profileData.LastName = $profile["LastName"].Value
 $collection += $profileData
}

$collection | Export-Csv $outputFile -NoTypeInformation

Write-Host "Profiles Created Successfully"
4
  • I have to get only those profiles which are start with First_Name ="A" not to get all the records with every property start with "A". I need a collection of those users once, I don't want to iterate, because here are huge users available( 3000 to 4000 users) so it'll take a lot of time to show the users. Thanks!
    – Ram
    Sep 21, 2016 at 5:23
  • Ram, Look at my solution above with PowerShell and hope this helps!
    – Kauser
    Sep 21, 2016 at 20:25
  • Did you used KQL in the Search?
    – Ram
    Sep 22, 2016 at 6:46
  • I used SQL Like operator to query for results. See my powershell solution above. I hope my solution would be of help to you!!
    – Kauser
    Sep 27, 2016 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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