I would like to be able to retrieve all users in my Azure Active Directory and cache it so I could filter my users on demande. I know about the nextLink used for Pagination. I’m fairly new in the React world and Javascript so I need some help understanding how to cycle through all pages to get all my users. Here’s the code I have right now :
JavaScript
x
14
14
1
export async function searchADUser(searchQuery?: string, filter: string, orderBy: string = 'displayName')
2
{
3
const searchedUser = await graphClient!
4
.api('/users')
5
.header('ConsistencyLevel', 'eventual')
6
.filter(`${filter}`)
7
.search(`"${searchQuery}"`)
8
.orderby(`${orderBy}`)
9
.get();
10
11
const nextLink = searchedUser["@odata.nextLink"]
12
13
return searchedUser;
14
I was able to access the nextLink url using the [“@odata.nextLink”]. So my question is how to get all users ? Do I just loop until nextLink is null or there is a better way.
Thank you
Advertisement
Answer
Here’s the code to be able to get all pages of data from a Microsoft Graph :
JavaScript
1
21
21
1
export async function getAllAADUsers(
2
authProvider: AuthCodeMSALBrowserAuthenticationProvider
3
) {
4
ensureClient(authProvider);
5
6
try {
7
let response: PageCollection = await graphClient!.api('/users').get();
8
9
// let allUsers: User[] = [];
10
let callback: PageIteratorCallback = (data) => {
11
console.log('file: GraphService.tsx ~ line 129 ~ data', data);
12
return true;
13
};
14
15
let pageIterator = new PageIterator(graphClient!, response, callback);
16
17
await pageIterator.iterate();
18
} catch (e) {
19
throw e;
20
}
21
That works wonders!
Thank you all for the help