diff --git a/dotnet/agent-framework-dotnet.slnx b/dotnet/agent-framework-dotnet.slnx index e0423ab908..041da4a6f6 100644 --- a/dotnet/agent-framework-dotnet.slnx +++ b/dotnet/agent-framework-dotnet.slnx @@ -184,6 +184,7 @@ + diff --git a/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step22_SharePoint/FoundryAgents_Step22_SharePoint.csproj b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step22_SharePoint/FoundryAgents_Step22_SharePoint.csproj new file mode 100644 index 0000000000..4d17fe06bb --- /dev/null +++ b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step22_SharePoint/FoundryAgents_Step22_SharePoint.csproj @@ -0,0 +1,22 @@ + + + + Exe + net10.0 + + enable + enable + $(NoWarn);CA1812;CS8321 + + + + + + + + + + + + + diff --git a/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step22_SharePoint/Program.cs b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step22_SharePoint/Program.cs new file mode 100644 index 0000000000..6d1daf85df --- /dev/null +++ b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step22_SharePoint/Program.cs @@ -0,0 +1,84 @@ +// Copyright (c) Microsoft. All rights reserved. + +// This sample shows how to use SharePoint Grounding Tool with AI Agents. + +using Azure.AI.Projects; +using Azure.AI.Projects.OpenAI; +using Azure.Identity; +using Microsoft.Agents.AI; +using OpenAI.Responses; + +string endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set."); +string deploymentName = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; +string sharepointConnectionId = Environment.GetEnvironmentVariable("SHAREPOINT_PROJECT_CONNECTION_ID") ?? throw new InvalidOperationException("SHAREPOINT_PROJECT_CONNECTION_ID is not set."); + +const string AgentInstructions = """ + You are a helpful agent that can use SharePoint tools to assist users. + Use the available SharePoint tools to answer questions and perform tasks. + """; + +// Get a client to create/retrieve/delete server side agents with Azure Foundry Agents. +// WARNING: DefaultAzureCredential is convenient for development but requires careful consideration in production. +// In production, consider using a specific credential (e.g., ManagedIdentityCredential) to avoid +// latency issues, unintended credential probing, and potential security risks from fallback mechanisms. +AIProjectClient aiProjectClient = new(new Uri(endpoint), new DefaultAzureCredential()); + +// Create SharePoint tool options with project connection +var sharepointOptions = new SharePointGroundingToolOptions(); +sharepointOptions.ProjectConnections.Add(new ToolProjectConnection(sharepointConnectionId)); + +AIAgent agent = await CreateAgentWithMEAIAsync(); +// AIAgent agent = await CreateAgentWithNativeSDKAsync(); + +Console.WriteLine($"Created agent: {agent.Name}"); + +AgentResponse response = await agent.RunAsync("List the documents available in SharePoint"); + +// Display the response +Console.WriteLine("\n=== Agent Response ==="); +Console.WriteLine(response); + +// Display grounding annotations if any +foreach (var message in response.Messages) +{ + foreach (var content in message.Contents) + { + if (content.Annotations is not null) + { + foreach (var annotation in content.Annotations) + { + Console.WriteLine($"Annotation: {annotation}"); + } + } + } +} + +// Cleanup by agent name removes the agent version created. +await aiProjectClient.Agents.DeleteAgentAsync(agent.Name); +Console.WriteLine($"\nDeleted agent: {agent.Name}"); + +// --- Agent Creation Options --- + +// Option 1 - Using AgentTool.CreateSharepointTool + AsAITool() (MEAI + AgentFramework) +async Task CreateAgentWithMEAIAsync() +{ + return await aiProjectClient.CreateAIAgentAsync( + model: deploymentName, + name: "SharePointAgent-MEAI", + instructions: AgentInstructions, + tools: [((ResponseTool)AgentTool.CreateSharepointTool(sharepointOptions)).AsAITool()]); +} + +// Option 2 - Using PromptAgentDefinition SDK native type +async Task CreateAgentWithNativeSDKAsync() +{ + return await aiProjectClient.CreateAIAgentAsync( + name: "SharePointAgent-NATIVE", + creationOptions: new AgentVersionCreationOptions( + new PromptAgentDefinition(model: deploymentName) + { + Instructions = AgentInstructions, + Tools = { AgentTool.CreateSharepointTool(sharepointOptions) } + }) + ); +} diff --git a/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step22_SharePoint/README.md b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step22_SharePoint/README.md new file mode 100644 index 0000000000..9f89aaf652 --- /dev/null +++ b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step22_SharePoint/README.md @@ -0,0 +1,50 @@ +# Using SharePoint Grounding with AI Agents + +This sample demonstrates how to use the SharePoint grounding tool with AI agents. The SharePoint grounding tool enables agents to search and retrieve information from SharePoint sites. + +## What this sample demonstrates + +- Creating agents with SharePoint grounding capabilities +- Using AgentTool.CreateSharepointTool (MEAI abstraction) +- Using native SDK SharePoint tools (PromptAgentDefinition) +- Managing agent lifecycle (creation and deletion) + +## Prerequisites + +Before you begin, ensure you have the following prerequisites: + +- .NET 10 SDK or later +- Azure Foundry service endpoint and deployment configured +- Azure authentication configured for `DefaultAzureCredential` (for example, Azure CLI logged in with `az login`, environment variables, managed identity, or IDE sign-in) +- A SharePoint project connection configured in Azure Foundry + +**Note**: This demo uses `DefaultAzureCredential` for authentication. This credential will try multiple authentication mechanisms in order (such as environment variables, managed identity, Azure CLI login, and IDE sign-in) and use the first one that works. A common option for local development is to sign in with the Azure CLI using `az login` and ensure you have access to the Azure Foundry resource. For more information, see the [Azure CLI documentation](https://learn.microsoft.com/cli/azure/authenticate-azure-cli-interactively) and the [DefaultAzureCredential documentation](https://learn.microsoft.com/dotnet/api/azure.identity.defaultazurecredential). + +Set the following environment variables: + +```powershell +$env:AZURE_FOUNDRY_PROJECT_ENDPOINT="https://your-foundry-service.services.ai.azure.com/api/projects/your-foundry-project" # Replace with your Azure Foundry resource endpoint +$env:AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME="gpt-4o-mini" # Optional, defaults to gpt-4o-mini +$env:SHAREPOINT_PROJECT_CONNECTION_ID="your-sharepoint-connection-id" # Required: SharePoint project connection ID +``` + +## Run the sample + +Navigate to the FoundryAgents sample directory and run: + +```powershell +cd dotnet/samples/GettingStarted/FoundryAgents +dotnet run --project .\FoundryAgents_Step22_SharePoint +``` + +## Expected behavior + +The sample will: + +1. Create two agents with SharePoint grounding capabilities: + - Option 1: Using AgentTool.CreateSharepointTool (MEAI abstraction) + - Option 2: Using native SDK SharePoint tools +2. Run the agent with a query: "List the documents available in SharePoint" +3. The agent will use SharePoint grounding to search and retrieve relevant documents +4. Display the response and any grounding annotations +5. Clean up resources by deleting both agents diff --git a/dotnet/samples/GettingStarted/FoundryAgents/README.md b/dotnet/samples/GettingStarted/FoundryAgents/README.md index d8677ea58b..8f83afbd1e 100644 --- a/dotnet/samples/GettingStarted/FoundryAgents/README.md +++ b/dotnet/samples/GettingStarted/FoundryAgents/README.md @@ -59,6 +59,7 @@ Before you begin, ensure you have the following prerequisites: |[Code interpreter](./FoundryAgents_Step14_CodeInterpreter/)|This sample demonstrates how to use the code interpreter tool with a Foundry agent| |[Computer use](./FoundryAgents_Step15_ComputerUse/)|This sample demonstrates how to use computer use capabilities with a Foundry agent| |[Bing Custom Search](./FoundryAgents_Step21_BingCustomSearch/)|This sample demonstrates how to use Bing Custom Search tool with a Foundry agent| +|[SharePoint grounding](./FoundryAgents_Step22_SharePoint/)|This sample demonstrates how to use the SharePoint grounding tool with a Foundry agent| |[Web search](./FoundryAgents_Step25_WebSearch/)|This sample demonstrates how to use the Responses API web search tool with a Foundry agent| |[Memory search](./FoundryAgents_Step26_MemorySearch/)|This sample demonstrates how to use memory search tool with a Foundry agent| |[File search](./FoundryAgents_Step18_FileSearch/)|This sample demonstrates how to use the file search tool with a Foundry agent|