Skip to content

hammerlink/msgraph-sdk-javascript

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

240 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Microsoft Graph JavaScript Client Library

npm version badge Travis

The Microsoft Graph JavaScript client library is a lightweight wrapper around the Microsoft Graph API that can be used server-side and in the browser.

Looking for IntelliSense on models (Users, Groups, etc.)? Check out the Microsoft Graph Types repository!

TypeScript demo

Installation

Via npm

npm install @microsoft/microsoft-graph-client

Via Script Tag

Include lib/graph-js-sdk-web.js in your page.

<script type="text/javascript" src="graph-js-sdk-web.js"></script>

Getting started

1. Register your application

Register your application to use Microsoft Graph API using one of the following supported authentication portals:

2. Authenticate for the Microsoft Graph service

The Microsoft Graph JavaScript Client Library has an adapter implementation (MSALAuthenticationProvider) for MSAL (Microsoft Authentication Library) which takes care of getting the accessToken. MSAL library does not ship with this library, user have to include it externally (For including MSAL, refer this). Creating an instance of MSALAuthenticationProvider,

const clientID = 'your_client_id'; // Client Id of the registered application
const graphScopes = ["user.read", "mail.send"]; // An array of graph scopes
const options = { // An Optional options for initializing the MSAL @see https://github.com/AzureAD/microsoft-authentication-library-for-js/wiki/MSAL-basics#configuration-options
    redirectUri: "Your redirect URI"
};
const authProvider = new MSALAuthenticationProvider(clientId, scopes, options);

User can integrate own preferred authentication library by implementing IAuthenticationProvider interface.

3. Initialize a Microsoft Graph Client object with an authentication provider

An instance of the Client class handles requests to Microsoft Graph API and processing the responses. To create a new instance of this class, you need to provide an instance of IAuthenticationProvider which needs to be passed as a value for authProvider key in Options to a static initializer method Client.init.

const options = {
    authProvider // An instance created from previous step
};
const client = MicrosoftGraph.Client.init(options);

For more information, refer: default options, custom middleware chain

4. Make requests to the graph

Once you have authentication setup and an instance of Client, you can begin to make calls to the service. All requests should be start with client.api(path) and end with an action.

Getting user details,

try {
    let userDetails = await client.api("/me").get();
    console.log(userDetails);
} catch(error) {
    throw error;
}

Sending an email to the recipients

// Construct email object
const mail = {
    subject: "Microsoft Graph JavaScript Sample",
    toRecipients: [{
        emailAddress: {
            address: "example@example.com"
        }
    }],
    body: {
        content: "<h1>MicrosoftGraph JavaScript Sample</h1>Check out https://github.com/microsoftgraph/msgraph-sdk-javascript",
        contentType: "html"
    }
};
try {
    let response = await client.api("/me/sendMail").post({message: mail});
    console.log(response);
} catch(error) {
    throw error;
}

For more information, refer: Calling Pattern, Actions, Query Params, API Methods and more.

Questions and comments

We'd love to get your feedback about the Microsoft Graph JavaScript client library. You can send your questions and suggestions to us in the Issues section of this repository.

Contributing

Please see the contributing guidelines.

Additional resources

Copyright

Copyright (c) Microsoft Corporation. All rights reserved.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Third Party Notices

See Third Party Notices for information on the packages that are included in the package.json

About

Microsoft Graph client library for JavaScript

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 99.7%
  • HTML 0.3%