Last active
August 24, 2017 09:22
-
-
Save zplume/f65fc885032245d90da83131595b8ee5 to your computer and use it in GitHub Desktop.
Quick script to get SharePoint user properties (LoginName, Title, Email) for multiple users, via the Chrome console, using async/await, Promise.all, fetch, computed properties and rest parameters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async function() { | |
let userIds = [239, 405]; | |
// computed property name: [prop]: | |
// rest parameters: ...props | |
// stolen from: https://stackoverflow.com/a/25554551 | |
function pick(o, ...props) { | |
return Object.assign({}, ...props.map(prop => ({[prop]: o[prop]}))); | |
} | |
async function getUser(userId) { | |
let apiUrl = `${_spPageContextInfo.webAbsoluteUrl}/_api/web/GetUserById(${userId})`; | |
let options = { | |
method: "GET", | |
headers: { | |
"Accept": "application/json; odata=verbose" | |
}, | |
credentials: "include" | |
}; | |
let response = await fetch(apiUrl, options); | |
if(response.ok) { | |
let item = await response.json(); | |
return pick(item.d, "LoginName", "Title", "Email"); | |
} | |
else { | |
return Promise.reject(new Error(`${response.status}: ${response.statusText}`)); | |
} | |
} | |
let results = await Promise.all(userIds.map(getUser)); | |
console.table(results); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment