My organization is currently using the https://github.com/pnp/sp-dev-fx-webparts (React-Script-Editor) webpart on several of our SharePoint online site collections to enable the functionality we had on-premise. Since the SharePoint online user profiles do not contain all the information that our on-prem profiles did I looked into the Graph API to be able to bring back some of that information.
I managed to create a webpart that uses the /me endpoint to bring back user data and inserts it in a javascript variable that's available to the page once it's loaded.
What I'm trying to do now is be able to create a javascript object or function that is available to the page that when passed in a user email will call the graph api endpoint for
https://graph.microsoft.com/beta/users('[email protected]')
and then render that to a variable on the page. I know I can hardcode a function into the webpart render method and pull back user data that way, but I'd like to be able to pass a value to a function and call the endpoint at will.
Currently on page load, you can call the myGraphMe and myGraphMeBeta variables to get user info. I'm hoping for something like
userInfo = GetUserInfoFor('[email protected]')
I'm just not sure if you can expose a function like that.
I'll provide my code that works for my current solution so far, but for some reason I'm drawing a blank when trying to figure out how to accomplish the second part. Any thoughts or help would be greatly appreciated.
import * as React from 'react';
import { MSGraphClient } from "@microsoft/sp-http";
import styles from './GraphUserInfo.module.scss';
import { IGraphUserInfoProps } from './IGraphUserInfoProps';
import { IGraphUserInfoState } from './IGraphUserInfoState';
export default class GraphUserInfo extends React.Component<IGraphUserInfoProps, IGraphUserInfoState> {
constructor(props: IGraphUserInfoProps, state: IGraphUserInfoState){
super(props);
this.state = {
me: ""
};
}
public componentDidMount(){
const script = document.createElement("script");
script.id = "myGraphMeBetaTag";
this._getMyPropertiesBeta.then(res=>{
script.innerHTML = "var myGraphMeBeta = " +res + ";";
this._getMyProperties.then(res1 =>{
script.innerHTML += "var myGraphMe = " + res1 + ";";
document.body.appendChild(script);
}).catch(e => console.log(e));
}).catch(e => console.log(e));
}
public render(): React.ReactElement<IGraphUserInfoProps> {
return (
this.props.isEditMode?
<div className={ styles.graphUserInfo }>
<div className={ styles.container }>
<div className={styles.row}>
<span>
EDIT MODE Please look for a script tag with the ID of "myGraphMe" & "myGraphMeBeta" at the end of the body.
</span>
</div>
</div>
</div>
:
<div style={{display:'none'}}></div>
);
}
private _getMyPropertiesBeta = new Promise<string>((resolve,reject) =>{
console.log("Getting my user Properties");
this.props.context.msGraphClientFactory
.getClient()
.then((client: MSGraphClient) => {
client
.api("https://graph.microsoft.com/beta/me")
.get((err,res) =>{
if(err){
//console.log(err);
reject(JSON.stringify(err));
return;
}
//console.log(res);
resolve(JSON.stringify(res));
});
});
});
private _getMyProperties = new Promise<string>((resolve,reject) =>{
console.log("Getting my user Properties");
this.props.context.msGraphClientFactory
.getClient()
.then((client: MSGraphClient) => {
client
.api("https://graph.microsoft.com/v1.0/me")
.get((err,res) =>{
if(err){
//console.log(err);
reject(JSON.stringify(err));
return;
}
//console.log(res);
resolve(JSON.stringify(res));
});
});
});
}