How To Fetch Office 365 Users Using Graph API In SharePoint SpFx.


import * as React from 'react'
// import styles from './GraphApi.module.scss';
import { IGraphApiProps } from './IGraphApiProps';


import { DetailsList, PrimaryButton } from 'office-ui-fabric-react';
// import { MSGraphClient } from '@microsoft/sp-http';
import { MSGraphClientV3 } from '@microsoft/sp-http';


export interface Iuser {
? displayName: string;
? mail: string;
}
export interface IUserState {
? userstate: Iuser[];
}
// public allusers: Iuser[] = [];
export default class GraphApi extends React.Component<IGraphApiProps, IUserState> {
? constructor(props: IGraphApiProps) {
? ? super(props);
? ? this.state = {
? ? ? userstate: []
? ? }
? }
? public allusers: Iuser[] = [];
? public Getusers = (): void => {
? ? 
? ? this.props.msGraphClientFactory.
? ? ? getClient('3').
? ? ? then((msGraphClient: MSGraphClientV3) => {
? ? ? ? msGraphClient.
? ? ? ? ? api("users").
? ? ? ? ? version("v1.0").
? ? ? ? ? select("displayName,mail").
? ? ? ? ? get((err:any, res:any) => {
? ? ? ? ? ? if (err) {
? ? ? ? ? ? ? console.log("error occurred",err);
? ? ? ? ? ? }
? ? ? ? ? ? res.value.map((result: any) => {
? ? ? ? ? ? ? this.allusers.push({
? ? ? ? ? ? ? ? displayName: result.displayName,
? ? ? ? ? ? ? ? mail: result.mail
? ? ? ? ? ? ? });
? ? ? ? ? ? });
? ? ? ? ? ? this.setState({userstate:this.allusers});


? ? ? ? ? });


? ? ? });


? };
? public render(): React.ReactElement<IGraphApiProps> {



? ? return (
? ? ? <div>
? ? ? ? <PrimaryButton text="Search Users" onClick={this.Getusers}></PrimaryButton>
<DetailsList items={this.state.userstate}>


</DetailsList>
? ? ? </div>
? ? );
? }
}

;        
Gaurang Patel

M365/SharePoint, SharePoint 2013 Developer, jQuery, Rest API, SPFx, PowerApps, PowerAutomation, SharePoint Migration, Webhook, React Js, MS Teams, Azure Function

1 年

Thank you it's working on this solution

回复
Daniel Fasogbon

QA Engineer || Business Application Developer || Salesforce Administrator || Business Analyst

1 年

this doesnt work

回复

要查看或添加评论,请登录

Vijay Thapak的更多文章

  • Java Generics.

    Java Generics.

    Java Generics allows us to create a single class, method, interface respectively that can be used with different types…

社区洞察

其他会员也浏览了