External users and user profiles in JSOM

External users and user profiles in JSOM

When I started working on the Mavention Profile Completeness the first thing we focused on was to get a working version into the store. In this first version it was all about getting the properties and checking if they where empty or not. Now with that version running all smooth and having a bit more experience with the client object model calls used to retrieve the properties I reckoned we could optimize a bit. One of the things you would likely do when working with users is making a difference between external users and your ‘normal’ users.

In a post explaining the workings of the Mavention Profile Completeness you can see that retrieving the profile itself is pretty straight forward:

function getUserPropertiesFromProfile() {
    // Get the current client context and PeopleManager instance.
    var clientContext = new SP.ClientContext.get_current();
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
    // Get user profile properties for the current user
    userProfileProperties = peopleManager.getMyProperties();
    // Load the UserProfilePropertiesForUser object and send the request.
    clientContext.executeQueryAsync(onProfileRequestSuccess, onProfileRequestFail);
function onProfileRequestSuccess() {
    propertyValue = userProfileProperties.get_userProfileProperties()["MyProperty"];
function onProfileRequestFail() {
    // Handle failure

However external users are slightly different than normal users, an external user will not have a profile like a normal user would have, and thus an external user will not have the option to edit their profile. As for the profile completeness this will result in external users always have a 0% completeness level, and no way to achieve anything else by filling in a profile.

Now if you have external users you might want to show them something else than the Profile Completeness progress, or just hide the part all together. To do so we will have to distinguish an external users from the internal users. As it turned out achieving that is pretty easy. If you would run the code example above you will find that for external users most properties will be empty. The way the Client Object Model works for external user is that it will return empty values for all the properties that are available  in the User Profile Service Application, hence the 0%. If you check the userprofileproperties object you will find that some properties are filled though. If you check all the available properties, one of them is the ADGuid, a property that is filled for all users that reside in an active directory, however external users will have an empty value for that one. So to differ between external users and internal users all we have to do is to check if the ADGuid is present:

if (userProfileProperties.get_userProfileProperties()["ADGuid"] === "") {
    log('external user');
else { }

Whenever you are working with external users you can use the example above to make sure you are targeting your app conents correctly.

Leave a Reply