Skip to content
Rishan Solutions
Rishan Solutions
  • PowerApps
  • SharePoint online
    • Uncategorized
    • Uncategorized
  • PowerAutomate
Rishan Solutions
Latest Posts
  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025 June 24, 2025
  • Recursive Queries in T-SQL May 7, 2025
  • Generating Test Data with CROSS JOIN May 7, 2025
  • Working with Hierarchical Data May 7, 2025
  • Using TRY_CAST vs CAST May 7, 2025
  • Dynamic SQL Execution with sp_executesql May 7, 2025

Using jQuery with Microsoft Graph API

Posted on April 2, 2025April 2, 2025 by Zubair Shaik

Loading

Using jQuery with Microsoft Graph API: A Comprehensive Guide

Introduction

Microsoft Graph API is a powerful tool that enables developers to interact with Microsoft 365 services such as Outlook, OneDrive, Teams, and SharePoint. When combined with jQuery, it allows for seamless integration, making it possible to retrieve, create, update, and delete data from Microsoft 365 applications directly from a web application.

This guide will cover everything you need to know about using jQuery with Microsoft Graph API, including authentication, making API requests, handling responses, and best practices.


Table of Contents

  1. What is Microsoft Graph API?
  2. Why Use jQuery with Microsoft Graph API?
  3. Setting Up a Microsoft Azure App for Graph API
  4. Getting an Access Token for Authentication
  5. Making API Requests with jQuery
  6. Fetching User Profile Information
  7. Retrieving Emails from Outlook
  8. Accessing OneDrive Files
  9. Sending Messages in Microsoft Teams
  10. Best Practices
  11. Error Handling
  12. Conclusion

1. What is Microsoft Graph API?

Microsoft Graph API is a unified API endpoint that allows developers to access Microsoft 365 services. It enables integration with:

  • Outlook (emails, contacts, calendar)
  • OneDrive (files, folders)
  • Microsoft Teams (messages, channels)
  • SharePoint (documents, lists)
  • Azure Active Directory (user data, groups)

It uses OAuth 2.0 authentication to provide secure access to Microsoft services.


2. Why Use jQuery with Microsoft Graph API?

Although modern JavaScript frameworks like React and Angular are commonly used with Graph API, jQuery remains relevant due to its simplicity and ease of use.

Advantages of Using jQuery:

  • Lightweight and easy to use
  • Works well with AJAX requests
  • Simplifies API calls with $.ajax()
  • No need for additional setup or dependencies

3. Setting Up a Microsoft Azure App for Graph API

To use the Microsoft Graph API, you must register an application in the Azure Active Directory (AAD).

Steps to Register an App in Azure

  1. Go to the Azure Portal.
  2. Navigate to Azure Active Directory > App registrations.
  3. Click New registration.
  4. Provide an app name (e.g., “MyGraphApp”).
  5. Select “Accounts in any organizational directory and personal Microsoft accounts”.
  6. Set the Redirect URI to http://localhost (for testing).
  7. Click Register.

Configuring API Permissions

  1. In the app dashboard, go to API Permissions.
  2. Click Add a permission > Microsoft Graph.
  3. Select the required permissions (e.g., User.Read, Mail.Read, Files.Read).
  4. Click Grant admin consent.

Generating a Client Secret

  1. Navigate to Certificates & secrets.
  2. Click New client secret.
  3. Copy and save the secret (It won’t be shown again).

4. Getting an Access Token for Authentication

Microsoft Graph API requires an OAuth 2.0 access token for API requests.

Using jQuery to Get an Access Token

To authenticate with OAuth 2.0, you can use $.ajax() to make a request to the token endpoint.

function getAccessToken(clientId, clientSecret, tenantId) {
    return $.ajax({
        url: `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`,
        method: "POST",
        data: {
            client_id: clientId,
            client_secret: clientSecret,
            scope: "https://graph.microsoft.com/.default",
            grant_type: "client_credentials"
        },
        success: function(response) {
            console.log("Access Token: ", response.access_token);
        },
        error: function(error) {
            console.log("Error fetching token:", error);
        }
    });
}

5. Making API Requests with jQuery

Once you have an access token, you can make requests to Microsoft Graph API.

Example: Fetching User Profile

function getUserProfile(accessToken) {
    return $.ajax({
        url: "https://graph.microsoft.com/v1.0/me",
        method: "GET",
        headers: {
            "Authorization": `Bearer ${accessToken}`
        },
        success: function(response) {
            console.log("User Profile: ", response);
        },
        error: function(error) {
            console.log("Error fetching user profile:", error);
        }
    });
}

6. Fetching User Profile Information

You can use Graph API to get details about the logged-in user.

API Endpoint

GET https://graph.microsoft.com/v1.0/me

Example Request

$.ajax({
    url: "https://graph.microsoft.com/v1.0/me",
    method: "GET",
    headers: {
        "Authorization": `Bearer ${accessToken}`
    },
    success: function(response) {
        console.log("User Info:", response);
    },
    error: function(error) {
        console.log("Error:", error);
    }
});

7. Retrieving Emails from Outlook

To fetch user emails, you need the Mail.Read permission.

API Endpoint

GET https://graph.microsoft.com/v1.0/me/messages

Example Request

$.ajax({
    url: "https://graph.microsoft.com/v1.0/me/messages",
    method: "GET",
    headers: {
        "Authorization": `Bearer ${accessToken}`
    },
    success: function(response) {
        console.log("Emails:", response);
    },
    error: function(error) {
        console.log("Error fetching emails:", error);
    }
});

8. Accessing OneDrive Files

With Files.Read permission, you can list user files.

API Endpoint

GET https://graph.microsoft.com/v1.0/me/drive/root/children

Example Request

$.ajax({
    url: "https://graph.microsoft.com/v1.0/me/drive/root/children",
    method: "GET",
    headers: {
        "Authorization": `Bearer ${accessToken}`
    },
    success: function(response) {
        console.log("OneDrive Files:", response);
    },
    error: function(error) {
        console.log("Error fetching files:", error);
    }
});

9. Sending Messages in Microsoft Teams

To send messages, you need the Chat.ReadWrite permission.

API Endpoint

POST https://graph.microsoft.com/v1.0/me/chats/{chat-id}/messages

Example Request

$.ajax({
    url: `https://graph.microsoft.com/v1.0/me/chats/{chat-id}/messages`,
    method: "POST",
    headers: {
        "Authorization": `Bearer ${accessToken}`,
        "Content-Type": "application/json"
    },
    data: JSON.stringify({
        "body": {
            "content": "Hello from jQuery!"
        }
    }),
    success: function(response) {
        console.log("Message Sent:", response);
    },
    error: function(error) {
        console.log("Error sending message:", error);
    }
});

10. Best Practices

  • Use OAuth 2.0 PKCE flow for better security.
  • Always refresh access tokens when they expire.
  • Limit API calls to reduce latency and quota usage.

11. Error Handling

Ensure your code handles errors properly.

function handleError(error) {
    console.log("Error:", error);
    if (error.status === 401) {
        console.log("Unauthorized: Check your access token.");
    }
}

This guide covered using jQuery with Microsoft Graph API to fetch user details, emails, OneDrive files, and send messages. By leveraging Microsoft Graph, you can build powerful applications integrating Microsoft 365 services.

Would you like examples for a specific use case?

Posted Under jQueryAccess Token AJAX API Best Practices API Error Handling API Integration API permissions app registration Authentication Azure Active Directory Chat Messages client-side scripting Cloud Integration Email Retrieval File Management frontend development JavaScript jQuery JSON Microsoft 365 Microsoft Azure Microsoft Graph API Microsoft Graph Permissions Microsoft Services Microsoft Teams API OAuth 2.0 OAuth Authentication OneDrive API Outlook API REST API Secure API Calls SharePoint API Teams Integration Token-Based Authentication User Profile Fetching Web Applications Web Development Web Security

Post navigation

NPM/Yarn dependency conflicts leading to build issues
Invalid JSX error due to missing closing tag

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025
  • Recursive Queries in T-SQL
  • Generating Test Data with CROSS JOIN
  • Working with Hierarchical Data
  • Using TRY_CAST vs CAST

Recent Comments

  1. Michael Francis on Search , Filter and Lookup in power apps
  2. A WordPress Commenter on Hello world!

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • March 2024
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • January 2022

Categories

  • Active Directory
  • AI
  • AngularJS
  • Blockchain
  • Button
  • Buttons
  • Choice Column
  • Cloud
  • Cloud Computing
  • Data Science
  • Distribution List
  • DotNet
  • Dynamics365
  • Excel Desktop
  • Extended Reality (XR) – AR, VR, MR
  • Gallery
  • Icons
  • IoT
  • Java
  • Java Script
  • jQuery
  • Microsoft Teams
  • ML
  • MS Excel
  • MS Office 365
  • MS Word
  • Office 365
  • Outlook
  • PDF File
  • PNP PowerShell
  • Power BI
  • Power Pages
  • Power Platform
  • Power Virtual Agent
  • PowerApps
  • PowerAutomate
  • PowerPoint Desktop
  • PVA
  • Python
  • Quantum Computing
  • Radio button
  • ReactJS
  • Security Groups
  • SharePoint Document library
  • SharePoint online
  • SharePoint onpremise
  • SQL
  • SQL Server
  • Template
  • Uncategorized
  • Variable
  • Visio
  • Visual Studio code
  • Windows
© Rishan Solutions 2025 | Designed by PixaHive.com.
  • Rishan Solutions