Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let filename = "users.csv"
- let users = [];
- //This example will log out a list of all users in the system.
- var usersApi = new platformClient.UsersApi();
- let opts = {
- 'pageSize': 100, // Number | Page size
- 'pageNumber': 1, // Number | Page number
- 'sortOrder': "ASC", // String | Ascending or descending sort order
- 'state': "deleted" // String | Only list users of this state
- };
- function delay(t, v) {
- return new Promise(function(resolve) {
- setTimeout(resolve.bind(null, v), t)
- });
- }
- function processPageOfUsers(results){
- for(var x=0; x< results.entities.length; x++){
- // console.log(results.entities[x].name);
- let u = results.entities[x];
- users.push([u.id, u.name]);
- }
- if(results.nextUri){
- opts.pageNumber++;
- //get the next page of users directly
- delay(250).then(function(){
- usersApi.getUsers(opts).then(processPageOfUsers);
- });
- }
- else{
- console.log("Exporting csv...");
- exportCSV(filename, users);
- }
- }
- function exportCSV(filename, data){
- let csvContent = "data:text/csv;charset=utf-8,"
- + data.map(e => e.join(",")).join("\n");
- // doesn't escape commas
- var encodedUri = encodeURI(csvContent);
- var link = document.createElement("a");
- link.setAttribute("href", encodedUri);
- link.setAttribute("download", filename);
- //document.body.appendChild(link); // Required for FireFox
- link.click();
- }
- usersApi.getUsers(opts).then(processPageOfUsers);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement