Fetching user profile information for a given user can be both slow performance wise and technically arduous (due to there not being a way to filter specific custom user profile properties for a given user).
So we will look at how we can fetch this user profile information elegantly and and save it locally.
In this article I will walk through how to fetch user profile properties using REST and then store them in a usable locally cached form.
Getting User Profile information
For this example I will construct a simple JSON object called ‘Current‘ which lives in the MyCompany.UserProfiles namespace and store the user profile property values in there.
Excellent, but we want to actually fetch these properties back from the REST endpoint and save them in this object and as it turns out that’s pretty straight forward as well.
First we declare a constants JSON object which will hold the REST endpoint and the local storage cache key (we need this to save the properties to the web browser’s local storage cache).
Wait a minute… What is this GetLocalStorageItemCacheForKey function? Well this is where we check to see if we already have this specific set of data in the user’s web browser local storage cache and then return either the cached results or the endpoint data.
So how do we actual check Local Storage and fetch the profile properties?
The full code for working with the Local Storage in this way is as follows:
We do this with the help of a function which iterates through the user profile collection until the relevant property is found.
So now we have everything that we need to get a specific selection of user profile properties efficiently – storing them in the relevant user’s web browser local storage cache and fetching them only when needed.
It would be pretty straight forward to adapt this code to save the formatted JSON object to the web browser local storage cache instead of the raw endpoint data.
You could also, extend the ‘Current’ JSON object to hold the mappings that you need – this would allow you to iterate through this single object instead of creating a temporary array (as shown in the GetCurrent function).
With these things in mind, hopefully you can see how easy it is to get this information and store it for efficiency and ease of use going forward.