Skip to content

Instantly share code, notes, and snippets.

@zplume
Last active August 24, 2017 09:22
Show Gist options
  • Save zplume/f65fc885032245d90da83131595b8ee5 to your computer and use it in GitHub Desktop.
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.
(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