Last active
April 6, 2016 12:41
-
-
Save beckettkev/f5d69b544286e46d95cdb74b418fd479 to your computer and use it in GitHub Desktop.
This file contains 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
(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