pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
753 stars 305 forks source link

How to retrieve all users using pnp/sp if the site contains more than 5000 users? #2970

Closed bhoomesh-spe closed 5 months ago

bhoomesh-spe commented 5 months ago

What version of PnPjs library you are using

3.x

Minor Version Number

3.15.0

Target environment

All

Additional environment details

SharePoint Online, SPFx, React

Question/Request

Hello Everyone,

I have tried to get all site users programmatically using the below code.

import { spfi } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/site-users/web";

const sp = spfi(...);
// get the first 100 users
// const users = await sp.web.siteUsers();
// get upto 5000 users
const users = await sp.web.siteUsers.orderBy("Title", true).top(5000)();
console.log("users: ", users);

The above code is working as expected if the site contains less than 5000 users.

But if the site contains more than 5000 users what changes need to do in the above code?

Can anyone help me with the same?

Thanks

patrick-rodgers commented 5 months ago

Have you tried anything? What solutions did you research that didn't work?

bhoomesh-spe commented 5 months ago

I have tried the below code but it is not working due to threshold limitation.

import { spfi } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/site-users/web";

const sp = spfi(...);
const users = await sp.web.siteUsers.orderBy("Title", true).top(10000)();
console.log("users: ", users);`
bcameron1231 commented 5 months ago

Hi, you can't select top 10000. The max is 5,000. So you'll need to set up your own logic to loop through after each top(5000). Potentially, you would order by id descending, and on subsequent calls just do a filter for gt than your last highest id.

bcameron1231 commented 5 months ago

Closing this issue as answered. If you have additional questions or we did not answer your question, please open a new issue, ref this issue, and provide any additional details available. Thank you!

github-actions[bot] commented 4 months ago

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.