cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Sidhant_02
Post Prodigy
Post Prodigy

Retrieve all users in an organization using Office365 connector

Hi everyone,
I was working on a requirement which is as follows:
We have an application built to track visitors and employees (& their assets). For visitors to grant them temporary access card they need to fill in the information and in case of employees if they forget their ID card they need to fill in the required details, once done then only they are issued a temporary card for that day.
Now this application is deployed at one location (Pune) and we are planning to expand the same to other locations (Germany, Chennai), so for that we were thinking of having two roles: SuperAdmin and Admin. SuperAdmin will have all the access (of the data) and they can assign admins for locations. Whereas admins can only create entries and view entries only for the location for which they have been assigned to (by superadmin)
Example:
There 50 records created with Location: Pune and 78 with Germany location. And Rajesh is been assigned as Admin for Pune location so he can only view Pune location records only not other location.

So to do that I thought of retrieving all the data from Office365 connector and store it in a collection for which I have made use of following expression:

 

Set(varSkip, 0); // Initialize a variable for pagination
 
// Loop to get all users
Collect(Office365UsersCollection, Office365Users.SearchUser({searchTerm: "", top: 999, skip: varSkip}));
 
// Create a collection for pagination
ClearCollect(Pagination, {Value: 0}, {Value: 999}, {Value: 1998}, {Value: 2997}); // Add more items if you have more than 4000 users
 
// Use ForAll to get all users
ForAll(Pagination,
    Collect(Office365UsersCollection, Office365Users.SearchUser({searchTerm: "", top: 999, skip: Value}))
);

//To get only Active users:
ClearCollect (
    ActiveUsers,
    Filter (
        Office365UsersCollection,
        AccountEnabled = true && !IsBlank(OfficeLocation)
        //Only retreiving ActiveUsers (AccountEnabled = true) && avoiding external users for whom location is blank so have used !IsBlank(OfficeLocation)
    )
)

//To get unique location values:
ClearCollect(uniqueLocations,Distinct(ActiveUsers,OfficeLocation))

Now based on selection (Location dropdown selection made in drop down) I need to show only users with that particular location (OfficeLocation), so superadmin selects : Pune then the Person type choice drop-down should only show employees with Pune location.
For that on the On-Change of thee location drop-down I used the following
ClearCollect(locationsBasedUsers, Filter(ActiveUsers,OfficeLocation=DataCardValue15.Selected.Value))

//DataCardValue15.Selected.Value: Refers to the location drop-down

It gives me the results but I have observed the collection containing few duplicates and when I tried to use the collection for Items property of the drop-down like:
Choices(locationsBasedUsers,DisplayName) or Choices(locationsBasedUsers.DisplayName) 

It gave an error.

 

Name isn't valid. '{0}' isn't recognized (Error)
So I used: locationsBasedUsers.DisplayName, but here the issue was that I was only able to get few set of records in the dropdown (like 30 odd records that were incomplete the selected location had more number of records).

So my question was is there any better way to do it then what I have done or how can I improve my existing structure.

Regards,
Sidhant.
 

1 ACCEPTED SOLUTION

Accepted Solutions

@Sidhant_02 ,

You need Distinct() on the FIlter as below.

Clear(Office365UsersCollection);
ForAll(
   Sequence(4, 1, 999),
   Collect(
      Office365UsersCollection,
      Distinct(
         Filter(
            Office365Users.SearchUser(
               {
                  searchTerm: "",
                  top: 999,
                  skip: Value
               }
            ),
            AccountEnabled && OfficeLocation = DataCardValue15.Selected.Value
         ),
         Mail
      )
   )
)

 

Please click Accept as solution 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 giving it Thumbs Up.

MVP (Business Applications)   Visit my blog Practical Power Apps

View solution in original post

14 REPLIES 14
WarrenBelz
Most Valuable Professional
Most Valuable Professional

Hi @Sidhant_02 ,

Please try this for the filtered location

ForAll(
   Sequence(4, 1, 999),
   Collect(
      Office365UsersCollection,
      Filter(
         Office365Users.SearchUser(
            {
               searchTerm: "",
               top: 999,
               skip: Value
            }
         ),
         AccountEnabled = true && OfficeLocation = DataCardValue15.Selected.Value
      )
   )
)

 

Please click Accept as solution 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 giving it Thumbs Up.

MVP (Business Applications)   Visit my blog Practical Power Apps

Hi @WarrenBelz ,
Thanks for your quick response, I tried the expression on my test screen 
(In the drop-down I had selected location as 'Chennai' and I got all the employees with OfficeLocation as Chennai)

Sidhant_02_1-1719909140688.png

 

Sidhant_02_0-1719909080515.png

I have one query as we are using Collect all the new records will be appended into the same collection.
Like if a user selects Chennai and gets all the Chennai individuals but then changes the location to USA (as I will be using the expression on On-Change of the location drop-down it will add all the new records in the same collection)
So for now I have added: Clear(Office365UsersCollection) on Visible property of the screen but is there any other way to avoid the appending.

And the other issue was in the Administrator column (which is a person type column in my AdminList_VM sharepoint list).
In my test screen I added another drop-down and tried to extract the DisplayName from the Office365UsersCollection using 'Choices':

Sidhant_02_2-1719909605267.png

I even tried: Choices(Office365UsersCollection.DisplayName)

Sidhant_02_3-1719909655225.png


Have I missed something?

Regards,
Sidhant.

Hi @Sidhant_02 ,

Easy enough to avoid the appending

Clear(Office365UsersCollection);
ForAll(
   Sequence(4, 1, 999),
   Collect(
      Office365UsersCollection,
      Filter(
         Office365Users.SearchUser(
            {
               searchTerm: "",
               top: 999,
               skip: Value
            }
         ),
         AccountEnabled = true && OfficeLocation = DataCardValue15.Selected.Value
      )
   )
)

As for the drop-down Items - you can use (you do not need Choices)

Office365UsersCollection.DisplayName

 

Please click Accept as solution 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 giving it Thumbs Up.

MVP (Business Applications)   Visit my blog Practical Power Apps

Thanks @WarrenBelz ,
Missed the part of adding the Clear option before 😉. But in the collection I can see few duplicates so to avoid that I thought of modifying the current expression by using Distinct()

ClearCollect(
   Office365UsersCollection,
   Distinct(
      ForAll(
         Sequence(0, 999),
         Filter(
            Office365Users.SearchUser(
               {
                  searchTerm: "",
                  top: 1000,
                  skip: Value * 1000
               }
            ),
            AccountEnabled = true &&
            OfficeLocation = Dropdown2.Selected.Title
         )
      ),
      Email
   )
)

Sidhant_02_0-1719921614005.png


Did I miss something?

Regards,
Sidhant.

@Sidhant_02 ,

You need Mail, not Email.

Hi @WarrenBelz ,
I tried replacing email with mail but it was not recognized:

Sidhant_02_3-1720091024585.png

Original exression:
Clear(Office365UsersCollection);
ForAll(
   Sequence(4, 1, 999),
   Collect(
      Office365UsersCollection,
      Filter(
         Office365Users.SearchUser(
            {
               searchTerm: "",
               top: 999,
               skip: Value
            }
         ),
         AccountEnabled = true && OfficeLocation = Dropdown2_1.Selected.Title
      )
   )
)


Added Distinct:

Sidhant_02_4-1720091208844.png

(Same issue and it was expecting 2 arguments)

Regards,
Sidhant

@Sidhant_02 ,

You need Distinct() on the FIlter as below.

Clear(Office365UsersCollection);
ForAll(
   Sequence(4, 1, 999),
   Collect(
      Office365UsersCollection,
      Distinct(
         Filter(
            Office365Users.SearchUser(
               {
                  searchTerm: "",
                  top: 999,
                  skip: Value
               }
            ),
            AccountEnabled && OfficeLocation = DataCardValue15.Selected.Value
         ),
         Mail
      )
   )
)

 

Please click Accept as solution 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 giving it Thumbs Up.

MVP (Business Applications)   Visit my blog Practical Power Apps

Thanks @WarrenBelz ,
I am using the following list (for locations)

Sidhant_02_2-1720171399620.png

(not retrieving locations from office 365 which was done previously)

Sidhant_02_3-1720171474864.png

On-Change of the locations drop-down I have used the same expression that you mentioned in your latest reply

Sidhant_02_4-1720171552754.png

 

Clear(Office365UsersCollection);
ForAll(
   Sequence(4, 1, 999),
   Collect(
      Office365UsersCollection,
      Distinct(
         Filter(
            Office365Users.SearchUser(
               {
                  searchTerm: "",
                  top: 999,
                  skip: Value
               }
            ),
            AccountEnabled && OfficeLocation = Dropdown2.Selected.Title
         ),
         DisplayName  //Used DisplayName inplace of Mail
      )
   )
)

Now the expression is fine, but still I observed there are duplications even after applying Distinct keyword like:


Sidhant_02_0-1720171022267.png


After scrolling down (in the collection) I saw the same set of values again

Sidhant_02_1-1720171150662.png

(For security reasons have masked other values but as you can Ajit record was visible again) even after applying Distinct, so in this case is the Distinct has not worked as expected.
What are your thoughts on this?

Regards,
Sidhant.

@Sidhant_02 ,

It did work as expected on each iteration of ForAll, however there is still an opportunity for duplicates to exist in two different "bundles". You cannot add a further Distinct into this process due to Data Row Limit issues, however if you then used

Distinct(
   Office365UsersCollection,
   Mail
)

you should receive the correct values.

 

Please click Accept as solution 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 giving it Thumbs Up.

MVP (Business Applications)   Visit my blog Practical Power Apps

Helpful resources

Announcements

Community will be READ ONLY July 16th, 5p PDT -July 22nd

Dear Community Members,   We'd like to let you know of an upcoming change to the community platform: starting July 16th, the platform will transition to a READ ONLY mode until July 22nd.   During this period, members will not be able to Kudo, Comment, or Reply to any posts.   On July 22nd, please be on the lookout for a message sent to the email address registered on your community profile. This email is crucial as it will contain your unique code and link to register for the new platform encompassing all of the communities.   What to Expect in the New Community: A more unified experience where all products, including Power Apps, Power Automate, Copilot Studio, and Power Pages, will be accessible from one community.Community Blogs that you can syndicate and link to for automatic updates. We appreciate your understanding and cooperation during this transition. Stay tuned for the exciting new features and a seamless community experience ahead!

Summer of Solutions | Week 4 Results | Winners will be posted on July 24th

We are excited to announce the Summer of Solutions Challenge!   This challenge is kicking off on Monday, June 17th and will run for (4) weeks.  The challenge is open to all Power Platform (Power Apps, Power Automate, Copilot Studio & Power Pages) community members. We invite you to participate in a quest to provide solutions in the Forums to as many questions as you can. Answers can be provided in all the communities.    Entry Period: This Challenge will consist of four weekly Entry Periods as follows (each an “Entry Period”)   - 12:00 a.m. PT on June 17, 2024 – 11:59 p.m. PT on June 23, 2024 - 12:00 a.m. PT on June 24, 2024 – 11:59 p.m. PT on June 30, 2024 - 12:00 a.m. PT on July 1, 2024 – 11:59 p.m. PT on July 7, 2024 - 12:00 a.m. PT on July 8, 2024 – 11:59 p.m. PT on July 14, 2024   Entries will be eligible for the Entry Period in which they are received and will not carryover to subsequent weekly entry periods.  You must enter into each weekly Entry Period separately.   How to Enter: We invite you to participate in a quest to provide "Accepted Solutions" to as many questions as you can. Answers can be provided in all the communities. Users must provide a solution which can be an “Accepted Solution” in the Forums in all of the communities and there are no limits to the number of “Accepted Solutions” that a member can provide for entries in this challenge, but each entry must be substantially unique and different.    Winner Selection and Prizes: At the end of each week, we will list the top ten (10) Community users which will consist of: 5 Community Members & 5 Super Users and they will advance to the final drawing. We will post each week in the News & Announcements the top 10 Solution providers.  At the end of the challenge, we will add all of the top 10 weekly names and enter them into a random drawing.  Then we will randomly select ten (10) winners (5 Community Members & 5 Super Users) from among all eligible entrants received across all weekly Entry Periods to receive the prize listed below. If a winner declines, we will draw again at random for the next winner.  A user will only be able to win once overall. If they are drawn multiple times, another user will be drawn at random.  Individuals will be contacted before the announcement with the opportunity to claim or deny the prize.  Once all of the winners have been notified, we will post in the News & Announcements of each community with the list of winners.   Each winner will receive one (1) Pass to the Power Platform Conference in Las Vegas, Sep. 18-20, 2024 ($1800 value). NOTE: Prize is for conference attendance only and any other costs such as airfare, lodging, transportation, and food are the sole responsibility of the winner. Tickets are not transferable to any other party or to next year’s event.   ** PLEASE SEE THE ATTACHED RULES for this CHALLENGE**   Week 1 Results: Congratulations to the Week 1 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Community MembersNumber of SolutionsSuper UsersNumber of Solutions @anandm08  23 @WarrenBelz  31 @DBO_DV  10 @Amik  19 AmínAA 6 @mmbr1606  12 @rzuber  4 @happyume  7 @Giraldoj  3@ANB 6 (tie)   @SpongYe  6 (tie)     Week 2 Results: Congratulations to the Week 2 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Community MembersSolutionsSuper UsersSolutions @anandm08  10@WarrenBelz 25 @DBO_DV  6@mmbr1606 14 @AmínAA 4 @Amik  12 @royg  3 @ANB  10 @AllanDeCastro  2 @SunilPashikanti  5 @Michaelfp  2 @FLMike  5 @eduardo_izzo  2   Meekou 2   @rzuber  2   @Velegandla  2     @PowerPlatform-P  2   @Micaiah  2     Week 3 Results: Congratulations to the Week 3 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Week 3:Community MembersSolutionsSuper UsersSolutionsPower Apps anandm0861WarrenBelz86DBO_DV25Amik66Michaelfp13mmbr160647Giraldoj13FLMike31AmínAA13SpongYe27     Week 4 Results: Congratulations to the Week 4 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Week 4:Community MembersSolutionsSuper UsersSolutionsPower Apps DBO-DV21WarranBelz26Giraldoj7mmbr160618Muzammmil_0695067Amik14samfawzi_acml6FLMike12tzuber6ANB8   SunilPashikanti8

Check Out | 2024 Release Wave 2 Plans for Microsoft Dynamics 365 and Microsoft Power Platform

On July 16, 2024, we published the 2024 release wave 2 plans for Microsoft Dynamics 365 and Microsoft Power Platform. These plans are a compilation of the new capabilities planned to be released between October 2024 to March 2025. This release introduces a wealth of new features designed to enhance customer understanding and improve overall user experience, showcasing our dedication to driving digital transformation for our customers and partners.    The upcoming wave is centered around utilizing advanced AI and Microsoft Copilot technologies to enhance user productivity and streamline operations across diverse business applications. These enhancements include intelligent automation, AI-powered insights, and immersive user experiences that are designed to break down barriers between data, insights, and individuals. Watch a summary of the release highlights.    Discover the latest features that empower organizations to operate more efficiently and adaptively. From AI-driven sales insights and customer service enhancements to predictive analytics in supply chain management and autonomous financial processes, the new capabilities enable businesses to proactively address challenges and capitalize on opportunities.    

Updates to Transitions in the Power Platform Communities

We're embarking on a journey to enhance your experience by transitioning to a new community platform. Our team has been diligently working to create a fresh community site, leveraging the very Dynamics 365 and Power Platform tools our community advocates for.  We started this journey with transitioning Copilot Studio forums and blogs in June. The move marks the beginning of a new chapter, and we're eager for you to be a part of it. The rest of the Power Platform product sites will be moving over this summer.   Stay tuned for more updates as we get closer to the launch. We can't wait to welcome you to our new community space, designed with you in mind. Let's connect, learn, and grow together.   Here's to new beginnings and endless possibilities!   If you have any questions, observations or concerns throughout this process please go to https://aka.ms/PPCommSupport.   To stay up to date on the latest details of this migration and other important Community updates subscribe to our News and Announcements forums: Copilot Studio, Power Apps, Power Automate, Power Pages

Users online (959)