Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
(function (userProfiles, $) {
userProfiles.Constants = {
EndPoint: '/_api/SP.UserProfiles.PeopleManager/GetMyProperties',
Cache: {
Key: 'ProfileProperties'
}
};
//Object to store the given user's profile properties
userProfiles.Current = {
Account: '',
FullName: '',
OfficeLocation: '',
Region: '',
Country: '',
Global: '',
Campaign: '',
Group: '',
Business: '',
};
//Store the value in our JSON object
function updateProperty (key, value) {
userProfiles.Current[key] = value;
}
//This method iterates through the nested ugly JSON object array to get what we need
function getPropertyByKey (results, key, keyName, defaultValue) {
if (typeof results[key] !== 'undefined') {
return results[key];
} else {
results.UserProfileProperties.results.some(function(item, i) {
if (item[keyName] === key) {
defaultValue = item.Value;
return true;
} else {
return false;
}
});
return defaultValue;
}
}
//Helper function fetches the profile properties and stores them in the local storage cache (callback is invoked after this process)
userProfiles.GetCurrent = function (callback) {
MyCompany.StorageCache.GetLocalStorageItemCacheForKey(
userProfiles.Constants.Cache.Key,
_spPageContextInfo.webAbsoluteUrl + userProfiles.Constants.EndPoint,
false
).then(function (properties) {
if (typeof properties.error === 'undefined') {
var collection = typeof properties.results === 'undefined' ? properties.d : properties.results;
[
{ Name: 'Account', Property: 'AccountName' },
{ Name: 'FullName', Property: 'PreferredName' },
{ Name: 'OfficeLocation', Property: 'OfficeLocation' },
{ Name: 'Region', Property: 'Region' },
{ Name: 'Country', Property: 'Country' },
{ Name: 'Global', Property: 'Global' },
{ Name: 'Campaign', Property: 'Campaign' },
{ Name: 'Group', Property: 'Group' },
{ Name: 'Business', Property: 'Business' }
].map(function(item, i) {
updateProperty(item.Name, getPropertyByKey(collection, item.Property, 'Key', ''));
});
callback();
} else {
//Hold head in hands
}
});
};
})(MyCompany.UserProfiles = MyCompany.UserProfiles || {}, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment