This project implements a chat agent using Azure AI Foundry SDK that retrieves and grounds responses on SharePoint Embedded content through Microsoft 365 Copilot Retrieval API. Note that SPE datasource is in private preview for the Retrieval API.
This agent uses Azure AI Foundry and Retrieval API to enable contract managers reason with their documents.
- Azure AI Foundry Integration: Uses Azure AI SDK for chat completions with configurable models
- SharePoint Content Retrieval: Leverages Microsoft 365 Copilot Retrieval API for content grounding
- User Authentication: Interactive browser authentication with token caching
- Configurable Filtering: SharePoint path-based content filtering
- RAG Implementation: Retrieval-augmented generation with proper source attribution
- .NET 8.0 SDK
- Azure AI Foundry resource
- Microsoft 365 tenant with SharePoint
- Azure App Registration with delegated permissions
π± Application Type: These setup instructions are specifically for the Console Application version of this project. Web application deployments require different Azure AD authentication configurations.
git clone <your-repo-url>
cd SPEAgentWithRetrievalπ Important: These instructions are specifically for Console Applications. Web applications require different authentication platform configurations (SPA or Web platform with different redirect URIs and flows).
-
Create an Azure App Registration in your tenant:
- Go to Azure Portal
- Navigate to Azure Active Directory β App registrations
- Click New registration
- Name: your app name
- Supported account types: Accounts in this organizational directory only (Single tenant)
- Click Register
-
Configure Authentication Platform (Critical for Console Applications):
- Go to Authentication in the left menu
- Under Platform configurations:
- Remove any Single-page application platforms (these cause authentication conflicts)
- Click Add a platform β Mobile and desktop applications
- Set redirect URI to:
http://localhost - Click Configure
-
Configure API Permissions (Delegated):
- Go to API permissions in the left menu
- Click Add a permission β Microsoft Graph β Delegated permissions
- Add these permissions:
Files.Read.All(for SharePoint file access)Sites.Read.All(for SharePoint site access)- 'FileStorageContainer.Selected` delegated (if you need SharePointEmbedded container access)
- Click Grant admin consent for your organization
-
Note Important IDs:
- Copy the Application (client) ID from the Overview page
- Copy the Directory (tenant) ID from the Overview page
- You'll need these for your
appsettings.json
β οΈ Common Issue: If you get authentication errors likeAADSTS9002327orAADSTS7000218, it means your app registration is configured as a Single-Page Application instead of a Public Client. Make sure to remove all SPA platforms and only use Mobile/Desktop platform withhttp://localhostredirect URI.
π Web Application Note: If you're building a web application instead of a console app, you'll need to configure the authentication platform differently:
- Use Single-page application or Web platform instead of Mobile/Desktop
- Set appropriate redirect URIs for your web app (e.g.,
https://localhost:5001/signin-oidc)- Do NOT enable "Allow public client flows" for web applications
-
Copy the example configuration:
cp appsettings.example.json appsettings.json
-
Update
appsettings.jsonwith your values:{ "AzureAIFoundry": { "ProjectEndpoint": "https://your-foundry-resource.services.ai.azure.com", "ModelName": "gpt-4.1" //or your model name }, "Microsoft365": { "TenantId": "your-tenant-id-guid", "ClientId": "your-client-id-guid", "FilterExpression": "path:\"https://your-tenant.sharepoint.com/your-content-path/\"" //or any SharePoint URL. Check Retrieval API documentation for the URL } }
dotnet restoredotnet build
dotnet run- First Run: The application will open a browser for Microsoft 365 authentication
- Subsequent Runs: Tokens are cached, no re-authentication needed
- Ask Questions: Type questions about your SharePoint content
- View Sources: Responses include source document citations
The application is structured around the following components:
- Purpose: Fetches content from SharePoint using Microsoft Graph API.
- Key Component:
CopilotRetrievalService.cs- Retrieves SharePoint content.
- Implements chunking and embedding strategies for retrieval-augmented generation (RAG).
- Handles authentication and API calls using Microsoft Graph SDK.
- Purpose: Generates responses using Azure AI Foundry SDK.
- Key Component:
FoundryService.cs- Synthesizes responses based on retrieved content.
- Implements chat completions and content generation patterns.
- Uses Azure AI SDK for .NET.
- Purpose: Coordinates retrieval and synthesis processes.
- Key Component:
ChatService.cs- Sequentially orchestrates retrieval and synthesis.
- Implements async/await patterns for I/O operations.
- Handles error management and logging.
- Purpose: Displays synthesized responses and sources to the user.
- Key Component:
Program.cs- Manages user input and output.
- Displays top sources and synthesized responses.
- Purpose: Manages application settings and logs.
- Key Components:
appsettings.json: Stores configuration settings.ILogger: Implements structured logging for debugging and monitoring.
- Purpose: Ensures secure access to APIs.
- Key Component: Token caching with
InteractiveBrowserCredential.
+---------------------+
| Presentation |
| Layer |
| (Program.cs) |
+---------------------+
|
v
+---------------------+
| Orchestration |
| Layer |
| (ChatService.cs) |
+---------------------+
|
v
+---------------------+ +---------------------+
| Retrieval Layer | | Synthesis Layer |
| (CopilotRetrieval | | (FoundryService) |
| Service.cs) | | |
+---------------------+ +---------------------+
| |
v v
+---------------------+ +---------------------+
| Microsoft Graph API | | Azure AI Foundry |
| (SharePoint) | | SDK |
+---------------------+ +---------------------+
- No Secrets in Code: All sensitive configuration in
appsettings.json(git-ignored) - Delegated Permissions: Respects user's SharePoint access rights
- Token Security: Uses Azure Identity SDK for secure token handling
Cause: App registration is configured as SPA instead of Public Client
Solution:
- Go to Azure Portal β App registrations β Your app β Authentication
- Remove all Single-page application platforms
- Keep only Mobile and desktop applications with
http://localhostredirect URI - Ensure Allow public client flows is Enabled
Error: AADSTS7000218 - "The request body must contain the following parameter: 'client_assertion' or 'client_secret'"
Cause: App registration is configured as Confidential Client instead of Public Client
Solution:
- Go to Azure Portal β App registrations β Your app β Authentication
- Set Allow public client flows to Yes
- Use Mobile and desktop applications platform (not Web or SPA)
- Verify app registration has "Allow public client flows" enabled
- Ensure delegated permissions are granted with admin consent
- Check that redirect URI
http://localhostis configured - Remove any SPA or Web platform configurations that might conflict
- Verify the user has access to the SharePoint content
- Check the
FilterExpressionpath is correct - Ensure
Sites.Read.AllandFiles.ReadWrite.Allpermissions are granted
- Verify the project endpoint URL is correct
- Ensure the model name matches your deployment
- Check Azure AI Foundry resource permissions
For convenience, this repository includes automation scripts to fix common Azure AD app registration issues:
./fix-azure-app-registration.sh./fix-azure-app-registration.ps1These scripts will automatically:
- Remove SPA platform configurations
- Add Mobile/Desktop platform with correct redirect URI
- Enable public client flows
- Display current configuration for verification
- Fork the repository
- Create a feature branch
- Make your changes
- Ensure
appsettings.jsonis not committed - Submit a pull request
This project is licensed under the MIT License.