src/util/sp/fetchViewer.js
/**
* Get the currently logged in user.
*
* @return {Promise<Object>} object of user data.
*/
export default function fetchViewer() {
return new Promise(function(resolve, reject) {
window.SP.SOD.executeFunc("sp.js", "SP.ClientContext", function(){
window.SP.SOD.registerSod("sp.userprofiles.js", window.SP.Utilities.Utility.getLayoutsPageUrl("sp.userprofiles.js"));
window.SP.SOD.executeFunc("sp.userprofiles.js", "SP.UserProfiles.PeopleManager", () => {
const ctx = window.SP.ClientContext.get_current();
const manager = new window.SP.UserProfiles.PeopleManager(ctx);
const currentUser = ctx.get_web().get_currentUser();
ctx.load(currentUser);
const groups = currentUser.get_groups();
ctx.load(groups);
ctx.executeQueryAsync(
function(){
const groupEnum = groups.getEnumerator();
const userGroups = [];
while (groupEnum.moveNext()) {
userGroups.push(groupEnum.get_current().get_title());
}
const username = currentUser.get_loginName();
const userProperties = manager.getPropertiesFor(username);
ctx.load(userProperties);
ctx.executeQueryAsync(
function(){
const user = {};
const props = userProperties.get_userProfileProperties();
user.SID = props.SID;
user.AccountName = props.AccountName;
user.PreferredName = props.PrefferedName;
user.Title = props.Title;
user.PictureUrl = props.PictureUrl;
user.UserName = props.UserName;
user.Office = props.Office;
user.WorkEmail = props.WorkEmail;
user.TIAEnhet = props.TIAEnhet;
user.TIATittel = props.TIATittel;
user.TIARolleType = props.TIARolleType;
user.TIATypeStilling = props.TIATypeStilling;
user.groups = userGroups;
resolve(user);
},
function(sender, args){reject(sender, args);}
);
},
function(sender, args){reject(sender, args);}
);
});
});
});
}