page_type | description | products | languages | extensions | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sample |
This console application demonstrates how to make delta queries to Microsoft Graph, allowing applications to request only changed entities within a target resource. This sample monitors changes to the mail folders in a user's mailbox. |
|
|
|
This console application demonstrates how to make delta queries to Microsoft Graph, allowing applications to request only changed entities within a target resource. This sample monitors changes to the mail folders in a user's mailbox.
To run this sample you will need:
- The .NET 7.0 SDK
- A user in a Microsoft 365 tenant with an Exchange Online mailbox.
Before running the sample, you will need to create an app registration in Azure Active Directory to obtain a client ID. You can do this with the PowerShell script in this sample, or you can register it manually in the Azure Active Directory portal.
The RegisterApp.ps1 script uses the Microsoft Graph PowerShell SDK to create the app registration. You will need to install the Microsoft Graph PowerShell SDK to use this script.
IMPORTANT: The PowerShell script requires a work/school account with the Application administrator, Cloud application administrator, or Global administrator role. If your account has the Application developer role, you can register in the Azure AD admin center.
-
Open PowerShell 7 in the root directory of this sample.
-
Run the following command to set the execution policy for the current PowerShell window to allow the script to run.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process
-
Run the script with the following command.
./RegisterApp.ps1 -AppName "Delta Query Console Sample"
-
Follow the prompt to open
https://microsoft.com/devicelogin
in a browser, enter the provided code, and complete the authentication process. -
The application ID is printed to the console.
SUCCESS Client ID: 2fb1652f-a9a0-4db9-b220-b224b8d9d38b Tenant ID: common
-
Open a browser and navigate to the Azure Active Directory admin center and login using a Microsoft 365 user that has permission to register an application in Azure Active Directory.
-
Select Azure Active Directory in the left-hand navigation, then select App registrations under Manage.
-
Select New registration. On the Register an application page, set the values as follows.
- Set Name to
Delta Query Console Sample
. - Set Supported account types to Accounts in any organizational directory and personal Microsoft accounts.
- Leave Redirect URI empty.
- Set Name to
-
Select Register. On the Delta Query Console Sample page, copy the value of the Application (client) ID and save it, you will need it in the next step.
-
Select Authentication under Manage. Locate the Advanced settings section and change the Allow public client flows toggle to Yes, then choose Save.
Open appsettings.json and replace YOUR_CLIENT_ID_HERE
with the client ID of your app registration. Update other settings as needed.
Setting | Description |
---|---|
clientId |
The client ID of your app registration |
tenantId |
If you registered your application as a single tenant app, add your Azure tenant ID here. Otherwise, leave as common . |
graphUserScopes |
The Microsoft Graph permission scopes needed by the app. |
pollInterval |
The number of seconds to wait between poll requests |
authRecordCachePath |
Path to a file to cache the authentication record. Set to empty string to disable authentication caching. |
When the sample runs, it will prompt you to browse to a login URL and enter a device code. Once signed in, the app will check for changes to the mail folders in the user's mailbox every 30 seconds.
-
Open the root folder of this sample using Visual Studio Code.
-
On the Debug menu, choose Start Debugging.
-
Open your command-line interface (CLI) in the directory that contains DeltaQuery.csproj.
-
Run the following command to build the sample.
dotnet build
-
Run the following command to run the sample.
dotnet run
The first time you run this sample, it will prompt you to authenticate in your browser. Your authentication record will be persisted to disk so that if you run the sample again, the app will silently authenticate. You can make the app reauthenticate by deleting the authentication cache file before running the sample.
To disable authentication caching, set the authRecordCachePath
value in appsettings.json to an empty string.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
THIS CODE IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.