0

I'm trying to use React useEffect Hook to get the current user's profile picture URL. I tried executeOrDelayUntilScriptLoaded and executeFunc() to get the SP.UserProfiles.js before running my code. Neither of them worked.

SP.SOD.executeOrDelayUntilScriptLoaded(handleEffect, 'SP.UserProfiles.js');
const peopleManager = new SP.UserProfiles.PeopleManager(ctx);

Is there any other way I can load SP.UserProfiles.js?

0

When we were at SharePoint on-premise, we tend to look at SP.UserProfile.js to load user profile information. When we're on Office 365 world, Graph API is the best source to look at.

You can try on Microsoft Graph API Explorer: Graph API Explorer - My Photo. Get the user profile photo (based on example) to the URL:

https://graph.microsoft.com/v1.0/me/photo/$value

To execute it in your code, get the reference to the MSGraphClient

import { MSGraphClient } from "@microsoft/sp-http";

If you're using React framework, make sure you pass your WebPartContext to the component. Then you can call it as follows.

this.props.webpart.context.msGraphClientFactory.getClient().then((client: MSGraphClient) => {
    client
    .api(`/me/photo/$value`)
    .get((error, response:any, rawResponse: any) => {
        if (rawResponse.ok) {
            //your code here
            console.log(response);
        }
    });
});

Why Graph API: Office 365 as the parent bundle product of SharePoint Online does not store the user profile photo on SharePoint User Profile database. Currently, it is being stored on Exchange Online and I am not too sure how it will be in the future. Office 365 is evolving.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.