Skip to content

Commit 91675bd

Browse files
authored
.NET: Add SharePoint sample #3674 (#4227)
1 parent 804dbb6 commit 91675bd

5 files changed

Lines changed: 158 additions & 0 deletions

File tree

‎dotnet/agent-framework-dotnet.slnx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
<Project Path="samples/GettingStarted/FoundryAgents/FoundryAgents_Step18_FileSearch/FoundryAgents_Step18_FileSearch.csproj" />
185185
<Project Path="samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/FoundryAgents_Step19_OpenAPITools.csproj" />
186186
<Project Path="samples/GettingStarted/FoundryAgents/FoundryAgents_Step21_BingCustomSearch/FoundryAgents_Step21_BingCustomSearch.csproj" />
187+
<Project Path="samples/GettingStarted/FoundryAgents/FoundryAgents_Step22_SharePoint/FoundryAgents_Step22_SharePoint.csproj" />
187188
<Project Path="samples/GettingStarted/FoundryAgents/FoundryAgents_Step25_WebSearch/FoundryAgents_Step25_WebSearch.csproj" />
188189
<Project Path="samples/GettingStarted/FoundryAgents/FoundryAgents_Step26_MemorySearch/FoundryAgents_Step26_MemorySearch.csproj" />
189190
<Project Path="samples/GettingStarted/FoundryAgents/FoundryAgents_Evaluations_Step01_RedTeaming/FoundryAgents_Evaluations_Step01_RedTeaming.csproj" />
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net10.0</TargetFrameworks>
6+
7+
<Nullable>enable</Nullable>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
<NoWarn>$(NoWarn);CA1812;CS8321</NoWarn>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
14+
<PackageReference Include="Azure.Identity" />
15+
<PackageReference Include="Azure.AI.Projects" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.AzureAI\Microsoft.Agents.AI.AzureAI.csproj" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
// This sample shows how to use SharePoint Grounding Tool with AI Agents.
4+
5+
using Azure.AI.Projects;
6+
using Azure.AI.Projects.OpenAI;
7+
using Azure.Identity;
8+
using Microsoft.Agents.AI;
9+
using OpenAI.Responses;
10+
11+
string endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set.");
12+
string deploymentName = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
13+
string sharepointConnectionId = Environment.GetEnvironmentVariable("SHAREPOINT_PROJECT_CONNECTION_ID") ?? throw new InvalidOperationException("SHAREPOINT_PROJECT_CONNECTION_ID is not set.");
14+
15+
const string AgentInstructions = """
16+
You are a helpful agent that can use SharePoint tools to assist users.
17+
Use the available SharePoint tools to answer questions and perform tasks.
18+
""";
19+
20+
// Get a client to create/retrieve/delete server side agents with Azure Foundry Agents.
21+
// WARNING: DefaultAzureCredential is convenient for development but requires careful consideration in production.
22+
// In production, consider using a specific credential (e.g., ManagedIdentityCredential) to avoid
23+
// latency issues, unintended credential probing, and potential security risks from fallback mechanisms.
24+
AIProjectClient aiProjectClient = new(new Uri(endpoint), new DefaultAzureCredential());
25+
26+
// Create SharePoint tool options with project connection
27+
var sharepointOptions = new SharePointGroundingToolOptions();
28+
sharepointOptions.ProjectConnections.Add(new ToolProjectConnection(sharepointConnectionId));
29+
30+
AIAgent agent = await CreateAgentWithMEAIAsync();
31+
// AIAgent agent = await CreateAgentWithNativeSDKAsync();
32+
33+
Console.WriteLine($"Created agent: {agent.Name}");
34+
35+
AgentResponse response = await agent.RunAsync("List the documents available in SharePoint");
36+
37+
// Display the response
38+
Console.WriteLine("\n=== Agent Response ===");
39+
Console.WriteLine(response);
40+
41+
// Display grounding annotations if any
42+
foreach (var message in response.Messages)
43+
{
44+
foreach (var content in message.Contents)
45+
{
46+
if (content.Annotations is not null)
47+
{
48+
foreach (var annotation in content.Annotations)
49+
{
50+
Console.WriteLine($"Annotation: {annotation}");
51+
}
52+
}
53+
}
54+
}
55+
56+
// Cleanup by agent name removes the agent version created.
57+
await aiProjectClient.Agents.DeleteAgentAsync(agent.Name);
58+
Console.WriteLine($"\nDeleted agent: {agent.Name}");
59+
60+
// --- Agent Creation Options ---
61+
62+
// Option 1 - Using AgentTool.CreateSharepointTool + AsAITool() (MEAI + AgentFramework)
63+
async Task<AIAgent> CreateAgentWithMEAIAsync()
64+
{
65+
return await aiProjectClient.CreateAIAgentAsync(
66+
model: deploymentName,
67+
name: "SharePointAgent-MEAI",
68+
instructions: AgentInstructions,
69+
tools: [((ResponseTool)AgentTool.CreateSharepointTool(sharepointOptions)).AsAITool()]);
70+
}
71+
72+
// Option 2 - Using PromptAgentDefinition SDK native type
73+
async Task<AIAgent> CreateAgentWithNativeSDKAsync()
74+
{
75+
return await aiProjectClient.CreateAIAgentAsync(
76+
name: "SharePointAgent-NATIVE",
77+
creationOptions: new AgentVersionCreationOptions(
78+
new PromptAgentDefinition(model: deploymentName)
79+
{
80+
Instructions = AgentInstructions,
81+
Tools = { AgentTool.CreateSharepointTool(sharepointOptions) }
82+
})
83+
);
84+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Using SharePoint Grounding with AI Agents
2+
3+
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.
4+
5+
## What this sample demonstrates
6+
7+
- Creating agents with SharePoint grounding capabilities
8+
- Using AgentTool.CreateSharepointTool (MEAI abstraction)
9+
- Using native SDK SharePoint tools (PromptAgentDefinition)
10+
- Managing agent lifecycle (creation and deletion)
11+
12+
## Prerequisites
13+
14+
Before you begin, ensure you have the following prerequisites:
15+
16+
- .NET 10 SDK or later
17+
- Azure Foundry service endpoint and deployment configured
18+
- Azure authentication configured for `DefaultAzureCredential` (for example, Azure CLI logged in with `az login`, environment variables, managed identity, or IDE sign-in)
19+
- A SharePoint project connection configured in Azure Foundry
20+
21+
**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).
22+
23+
Set the following environment variables:
24+
25+
```powershell
26+
$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
27+
$env:AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME="gpt-4o-mini" # Optional, defaults to gpt-4o-mini
28+
$env:SHAREPOINT_PROJECT_CONNECTION_ID="your-sharepoint-connection-id" # Required: SharePoint project connection ID
29+
```
30+
31+
## Run the sample
32+
33+
Navigate to the FoundryAgents sample directory and run:
34+
35+
```powershell
36+
cd dotnet/samples/GettingStarted/FoundryAgents
37+
dotnet run --project .\FoundryAgents_Step22_SharePoint
38+
```
39+
40+
## Expected behavior
41+
42+
The sample will:
43+
44+
1. Create two agents with SharePoint grounding capabilities:
45+
- Option 1: Using AgentTool.CreateSharepointTool (MEAI abstraction)
46+
- Option 2: Using native SDK SharePoint tools
47+
2. Run the agent with a query: "List the documents available in SharePoint"
48+
3. The agent will use SharePoint grounding to search and retrieve relevant documents
49+
4. Display the response and any grounding annotations
50+
5. Clean up resources by deleting both agents

‎dotnet/samples/GettingStarted/FoundryAgents/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Before you begin, ensure you have the following prerequisites:
5959
|[Code interpreter](./FoundryAgents_Step14_CodeInterpreter/)|This sample demonstrates how to use the code interpreter tool with a Foundry agent|
6060
|[Computer use](./FoundryAgents_Step15_ComputerUse/)|This sample demonstrates how to use computer use capabilities with a Foundry agent|
6161
|[Bing Custom Search](./FoundryAgents_Step21_BingCustomSearch/)|This sample demonstrates how to use Bing Custom Search tool with a Foundry agent|
62+
|[SharePoint grounding](./FoundryAgents_Step22_SharePoint/)|This sample demonstrates how to use the SharePoint grounding tool with a Foundry agent|
6263
|[Web search](./FoundryAgents_Step25_WebSearch/)|This sample demonstrates how to use the Responses API web search tool with a Foundry agent|
6364
|[Memory search](./FoundryAgents_Step26_MemorySearch/)|This sample demonstrates how to use memory search tool with a Foundry agent|
6465
|[File search](./FoundryAgents_Step18_FileSearch/)|This sample demonstrates how to use the file search tool with a Foundry agent|

0 commit comments

Comments
 (0)