web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Search Active Director...
Power Apps
Answered

Search Active Directory by DisplayName without Middle Initials

(0) ShareShare
ReportReport
Posted on by 19
I have a canvas app with an edit form, with several cards and combo boxes linked to SP person columns. Users may search by Email or DisplayName, but I have received feedback from users expressing annoyance at having to know an employee's middle initial when searching by DisplayName. I am attempting to configure a combo box's Items so the user may bypass middle initials if searching by name, but I have encountered some problems. Here is my Items code snippet:
 
Filter(
    Office365Users.SearchUserV2(
        {
            searchTerm: Trim(Self.SearchText),
            isSearchTermRequired: true
        }
    ).value,
    And(
        If(
            Not(IsBlank(Self.SearchText)),
            StartsWith(
                GivenName,
                Trim(
                    Left(
                        Self.SearchText,
                        Find(
                            " ",
                            Self.SearchText
                        )
                    )
                )
            )
        ),
        If(
            " " in Self.SearchText,
            Substitute(
                Self.SearchText,
                $"{Trim(
                    First(
                        Split(
                            Self.SearchText,
                            " "
                        )
                    ).Value
                )} ",
                ""
            ) in Surname,
            true
        ),
        AccountEnabled = true
    )
)
 
The first If condition appears to be working correctly, as all GivenName matches are returned, but for the second condition, not all "GivenName Surname" matches are returned. The condition checks for a single space in SearchText, and if found, removes all text before and including it via Substitute(), and if not, returns true to ensure that GivenName matches are returned. Another condition specified for Surname is as follows:
 
If(
    " " in Self.SearchText,
    StartsWith(
        Surname,
        Substitute(
            Self.SearchText,
            $"{Trim(
                First(
                    Split(
                        Self.SearchText,
                        " "
                    )
                ).Value
            )} ",
            ""
        )
    ),
    true
)
 
This case results in the same outcome as with the previous case.
 
Is there another way I could rewrite the second If() statement to ensure all Surname matches are returned, or is there a mistake or delegation issue I am unaware of? I'd appreciate any advice, and thank you in advance!
Categories:
I have the same question (0)
  • Suggested answer
    WarrenBelz Profile Picture
    155,838 Most Valuable Professional on at
    A bit of a variation on what you have, but this seems to work when tested. Split your SearchText into a Table around the spaces and then test both Given Name and Surname on the one or two elements entered
    With(
       {
          _Names: Split(
             Trim(Self.SearchText),
             " "
          )
       },
       Filter(
          Office365Users.SearchUserV2(
             {
                searchTerm: Trim(Self.SearchText),
                isSearchTermRequired: true
             }
          ).value,
          AccountEnabled = true && StartsWith(
             GivenName,
             First(_Names).Value
          ) && (CountRows(_Names) = 1 || StartsWith(
             Surname,
             Last(_Names).Value
          ))
       )
    )
     
    Please ✅ Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider answering Yes to Was this reply helpful? or give it a Like ♥
    Visit my blog
    Practical Power Apps    LinkedIn  
  • nmingo3 Profile Picture
    19 on at
     
    Thank you for your suggestion, but the With() function approach does not appear to return a complete picture, either. But one thing I noticed for both my initial approach and yours is that whenever SearchText contains text after a single space, employees with middle initials in their DisplayName are not returned, even though the With() record is acting on Surname and not DisplayName.
     
    I tried other alternatives such as IsNumeric() to validate the location of Last(_Name).Value in Surname, but those also yielded incomplete results. Do you believe this could be a Microsoft bug?
  • WarrenBelz Profile Picture
    155,838 Most Valuable Professional on at
    Hi @nmingo3
    How many users are in your Entra ? I did some testing here on an Entra facility with about 200 users and it worked as expected every time.
     
    All the With() function is doing is splitting the user input into two strings that can be compared with Given and Surname, although once you start manipulating built-in functions (such as the Search facility in a Combo Box), the results are not always as expected.
  • nmingo3 Profile Picture
    19 on at
     
    Apologies for my delayed response! I'm part of a large, global organization that has over 200k employees.
  • WarrenBelz Profile Picture
    155,838 Most Valuable Professional on at
    Maybe filter by Department or Manager ?
  • WarrenBelz Profile Picture
    155,838 Most Valuable Professional on at
    A quick follow-up to see if you received the answer you were looking for. Happy to assist further if not.
     
    Please ✅ Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider answering Yes to Was this reply helpful? or give it a Like â™¥
    Visit my blog
    Practical Power Apps    LinkedIn   
  • nmingo3 Profile Picture
    19 on at
     
    Thank you for following up again. Unfortunately, the Department Name and Manager filtering did not yield complete results, either. Upon further discussion with my team, we are putting this feature on hold until a solution is found. The name search is still present, but I have informed users that middle initials will be required.
     
    This goes without saying, but I am still befuddled as to why the Surname search is not functioning as expected. With() has also come in handy several times before, so I doubt that is the issue. Also, thank you, as I truly appreciate your assistance in this matter!
  • Verified answer
    WarrenBelz Profile Picture
    155,838 Most Valuable Professional on at
    I agree that the surname search also should be working, but I suspect that the volume of users may be affecting that. I also just realised that I was looking at the wrong problem (so what I posted will not work) on anything over 1000 users. The initial search is limited to 999 users, so any filter is applied after that.
  • nmingo3 Profile Picture
    19 on at
     
    Oh wow, I was not expecting that to be the case! I'll let my team know about this and assess how to proceed. Thanks again!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Valantis Profile Picture

Valantis 424

#2
WarrenBelz Profile Picture

WarrenBelz 355 Most Valuable Professional

#3
11manish Profile Picture

11manish 290

Last 30 days Overall leaderboard