|
| 1 | +--- |
| 2 | +title: 'How to use the Sharepoint tool' |
| 3 | +titleSuffix: Azure AI Foundry |
| 4 | +description: Find examples on how to ground agents with Sharepoint. |
| 5 | +services: cognitive-services |
| 6 | +manager: nitinme |
| 7 | +ms.service: azure-ai-agent-service |
| 8 | +ms.topic: how-to |
| 9 | +ms.date: 04/09/2025 |
| 10 | +author: aahill |
| 11 | +ms.author: aahi |
| 12 | +ms.custom: azure-ai-agents-code |
| 13 | +zone_pivot_groups: selection-sharepoint |
| 14 | +--- |
| 15 | + |
| 16 | +# How to use the Sharepoint tool |
| 17 | + |
| 18 | +Use this article to find step-by-step instructions and code samples for using the Sharepoint tool in Azure AI Foundry Agent Service. |
| 19 | + |
| 20 | +## Step 1: Create a project client |
| 21 | + |
| 22 | +Create a client object, which will contain the connection string for connecting to your AI project and other resources. |
| 23 | + |
| 24 | +```python |
| 25 | +import os |
| 26 | +from azure.ai.projects import AIProjectClient |
| 27 | +from azure.identity import DefaultAzureCredential |
| 28 | +from azure.ai.projects.models import SharepointTool |
| 29 | +``` |
| 30 | + |
| 31 | +## Step 2: Create an Agent with the SharePoint tool enabled |
| 32 | + |
| 33 | +To make the SharePoint tool available to your agent, use a connection to initialize the tool and attach it to the agent. You can find your connection in the **connected resources** section of your project in the Azure AI Foundry portal. |
| 34 | + |
| 35 | +```python |
| 36 | +# Initialize Sharepoint tool with connection id |
| 37 | +sharepoint_connection = project_client.connections.get( |
| 38 | + connection_name="CONNECTION_NAME", |
| 39 | +) |
| 40 | +conn_id = sharepoint_connection.id |
| 41 | +print(conn_id) |
| 42 | +sharepoint = SharepointTool(connection_id=conn_id) |
| 43 | + |
| 44 | +# Create agent with SharePoint tool and process assistant run |
| 45 | +with project_client: |
| 46 | + agent = project_client.agents.create_agent( |
| 47 | + model=os.environ["MODEL_NAME"], |
| 48 | + name="my-assistant", |
| 49 | + instructions="You are a helpful assistant", |
| 50 | + tools=sharepoint.definitions, |
| 51 | + headers={"x-ms-enable-preview": "true"}, |
| 52 | + ) |
| 53 | + print(f"Created agent, ID: {agent.id}") |
| 54 | +``` |
| 55 | + |
| 56 | +## Step 3: Create a thread |
| 57 | + |
| 58 | +```python |
| 59 | +# Create thread for communication |
| 60 | +thread = project_client.agents.create_thread() |
| 61 | +print(f"Created thread, ID: {thread.id}") |
| 62 | + |
| 63 | +# Create message to thread |
| 64 | +# Remember to update the message with your data |
| 65 | +message = project_client.agents.create_message( |
| 66 | + thread_id=thread.id, |
| 67 | + role="user", |
| 68 | + content="<ask questions specific to your SharePoint documents>", |
| 69 | +) |
| 70 | +print(f"Created message, ID: {message.id}") |
| 71 | +``` |
| 72 | + |
| 73 | +## Step 4: Create a run and check the output |
| 74 | + |
| 75 | +Create a run and observe that the model uses the SharePoint tool to provide a response to the user's question. |
| 76 | + |
| 77 | +```python |
| 78 | +# Create and process agent run in thread with tools |
| 79 | +run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id) |
| 80 | +print(f"Run finished with status: {run.status}") |
| 81 | + |
| 82 | +if run.status == "failed": |
| 83 | + print(f"Run failed: {run.last_error}") |
| 84 | + |
| 85 | +# Delete the assistant when done |
| 86 | +project_client.agents.delete_agent(agent.id) |
| 87 | +print("Deleted agent") |
| 88 | + |
| 89 | +# Fetch and log all messages |
| 90 | +messages = project_client.agents.list_messages(thread_id=thread.id) |
| 91 | +print(f"Messages: {messages}") |
| 92 | +``` |
0 commit comments