How I Put a Copilot Studio Agent on SharePoint Pages with SPFx (Open Source)
10 min read
Topics: SPFx, SharePoint Framework, Copilot Studio, Azure Functions, Entra ID, Open Source
I built an open-source SharePoint Framework (SPFx) solution that adds an Ask button to modern SharePoint pages on configured sites. People type a question, and a Copilot Studio agent uses SharePoint knowledge with the signed-in user’s permissions. In my demo tenant, two users asking the same question received different answers based on their document access.
- Code (MIT licence): spfx-sharepoint-ai-assistant on GitHub
- 90-second demo: watch it on YouTube
Below is how it works, how I set it up, and what tripped me up along the way. Everything in the screenshots comes from a demo tenant with a fictional company, Contoso.
TL;DR
- SPFx 1.24.2 provides the chat interface in SharePoint.
- An Azure Function calls Copilot Studio with delegated user authentication.
- A user-assigned managed identity replaces a stored client secret.
- In the demo, users with different SharePoint permissions got different answers.
- The source code and setup guide are on GitHub under the MIT licence.

What it does
- An Ask button in the corner of modern pages on the site (an SPFx Application Customizer).
- A full-page chat web part for a dedicated “Ask HR” or “Ask IT” page.
- Answers come with their sources, linked to the SharePoint documents.
- On a phone, the chat opens full screen.
- The Azure Function does not persist chat history. Copilot Studio may retain conversation transcripts according to its configuration and retention policies.
This pattern is useful for HR policy portals, IT knowledge bases and intranets, where employees need answers without leaving SharePoint and without widening their document access.
Same question, different permissions
This is the part I most wanted to prove. I asked the same question as two different people.
The site owner can open the Leadership folder, so the agent finds the compensation guideline and answers:

A regular member of the same site cannot open that folder. Same question, same agent:

SharePoint document permissions are enforced through the agent’s authenticated knowledge retrieval, not by custom permission filtering in my code. The API controls who can call it and keeps the user’s delegated identity when it calls the agent. That avoids a shared service account reading SharePoint content.
These two tests show the expected behaviour in my demo tenant. Before a production rollout, I would also test conversation isolation, permission changes, unauthorized requests and prompt-injection scenarios.
How the pieces fit
SharePoint page (SPFx)
-> Azure Function token for the user, requested by SharePoint
-> Copilot Studio agent on behalf of the same user
-> SharePoint knowledge searched with that user's permissions
- SPFx (1.24.2, React 18). The button and the web part call my API with
AadHttpClient. SharePoint gets a token for the signed-in user; no secret lives in the browser. - Azure Function (Node.js 24, Flex Consumption). It checks the token, then exchanges it for a new token for the same user, scoped to the Power Platform API (the on-behalf-of flow).
- Copilot Studio. The Function talks to the agent with Microsoft’s Agents SDK client for Copilot Studio (
@microsoft/agents-copilotstudio-client). - The agent uses SharePoint as its knowledge source with “Authenticate with Microsoft”, so it retrieves content in the user’s context.
If you’re upgrading an existing solution to the same version, my SPFx 1.24 upgrade checklist covers the toolchain and React 18 changes.
Why an API in the middle?
I chose a backend API rather than calling Copilot Studio directly from the browser. This keeps the Power Platform API token on the server and gives me one place to:
- check every token properly (more on that below),
- apply a per-user rate limit of 10 requests per minute, held in memory on each Function instance,
- filter returned links to the tenant’s own SharePoint and OneDrive locations,
- keep logs free of prompts, answers and tokens.
The in-memory rate limit suits this demo, but it isn’t a distributed quota across several Function instances. That matters for both security and Copilot Studio costs.
The API does not use a database to keep conversations. Instead, it returns an encrypted conversation reference to the browser, sealed with AES-256-GCM and bound to the signed-in user. The encryption key still needs protecting as part of the backend configuration; the setup guide keeps it in Key Vault.
Setting it up
The repo’s setup guide has every step with screenshots. Here are the parts that matter most.
1. The Copilot Studio agent
I created a Standard agent (Copilot Studio’s standard harness), added the SharePoint site as a knowledge source, and turned off ungrounded responses and web search to keep answers to the configured knowledge. Standard agents currently support only delegated access when called through the Microsoft 365 Agents SDK, which is exactly what this design uses.

Under Settings > Security > Authentication, I selected Authenticate with Microsoft. Together with the delegated token flow and the SharePoint knowledge source, this lets the agent retrieve content in the user’s context.

Then publish the agent and share it with the people who will use it. Each user needs access to the agent.
The environment also needs Copilot Studio capacity. Without it, the agent replies that it “has reached its usage limit”. For the demo I used a pay-as-you-go billing plan in the Power Platform admin center.
The code is MIT-licensed, but running it still needs the right Microsoft 365 access, Copilot Studio capacity and Azure resources. Review licensing, consumption, monitoring and clean-up before you deploy it.
2. The API app registration
One app registration in Entra ID, named SharePoint AI Assistant API. It:
- exposes a scope,
access_as_user, that SharePoint asks for on behalf of the user, - has one delegated permission, Power Platform API > CopilotStudio.Copilots.Invoke, with admin consent.

If “Power Platform API” doesn’t appear in the list of APIs, its service principal hasn’t been created in your tenant yet. The setup guide has the one-line command for that.
3. No client secret
The on-behalf-of exchange needs the API to prove who it is. The usual answer is a client secret, which then has to be stored, rotated and never leaked.
Instead, the app registration trusts the Function’s user-assigned managed identity through a federated credential. Client secrets: zero.

One catch: this only works with a user-assigned identity. A system-assigned identity can’t be used for the federated credential.
4. The Azure Function
App Service Authentication sits in front of the code, requires a valid token and returns HTTP 401 to anything else.

The code then checks again: signature, issuer, audience, tenant, the access_as_user scope, that it’s a user token (not an app-only token), and which client app asked for it. By default it only accepts tokens that SharePoint requested for SPFx.
That last check matters because of how SPFx API access works.
5. The SPFx package and API access
The package asks for SharePoint AI Assistant API / access_as_user, and an admin approves it in SharePoint admin center > Advanced > API access.
That approval is tenant-wide. Once granted, any SPFx solution in the tenant could request a token for this API. So I also:
- turned on Assignment required on the API’s enterprise application, with a security group of allowed users,
- kept the client-app check in the Function,
- compiled the API address into the package instead of exposing it as a web part setting. This stops a page editor from redirecting requests that carry access tokens through that setting. A security review caught this risk before release.
These controls restrict access, but they don’t create an app-specific permission boundary between SPFx solutions. Other components that use SharePoint’s shared extensibility principal can also request the approved scope for eligible users. Administrators should review and trust the SPFx packages deployed in their tenant.
The button is registered on each site with a small PnP PowerShell script, which keeps the extension’s registration separate from package deployment.
The web part and the phone view
The same chat works as a full-page web part, for a dedicated page:

On a phone, the panel opens full screen:

What tripped me up
- The components never appeared. In my deployment, the package had no features and was missing
"skipFeatureDeployment": true. Setting it fixed the problem, with the extension registered separately by the setup script. - SharePoint kept serving an old version of the button. In my demo tenant, the previous extension version stayed in place for hours after deployment. Changing the component ID fixed it during development. I wouldn’t change component IDs as a routine upgrade step, because existing registrations depend on them.
- A failed upload left a second copy of the package in the site app catalog, with the same solution ID. That broke registration until I deleted it.
- Answers take time. In my demo, answers took 12 to 16 seconds. The panel shows a typing indicator, but set expectations with users.
- Answer quality comes from the agent, not from this code. Its instructions and knowledge sources decide how good the answers are. This project provides the authenticated integration and user experience around the agent.
Doesn’t Copilot Studio already do this?
Copilot Studio supports publishing standard agents directly to SharePoint through its SharePoint publishing channel. Users then reach the agent through SharePoint’s agent experience. Channel availability differs by agent type: Microsoft’s channels overview for Microsoft Copilot agents currently lists SharePoint as unavailable. Check which agent type and capabilities your environment supports. If the built-in experience meets your needs, use it. It’s less to maintain.
I’d build something like this project when you want:
- the chat on the page itself, or as a full-page web part, with your own look,
- an API you control, for limits, logging and link rules,
- a pattern you can reuse for other agents and other sites.
Try it
The GitHub repo has the SPFx solution, the Azure Function, both PowerShell scripts, the setup guide and the security notes. It’s MIT-licensed, so you can use it as a starting point for your own.
Questions or improvements are welcome as GitHub issues. Security issues: please use GitHub’s private reporting on the repo.
Need a custom Copilot Studio integration, SharePoint web part or enterprise SPFx application? I work with organizations and IT providers across Canada and the United States, including direct development and white-label subcontracting. Learn more about my SPFx development services.
Need a production-ready SPFx solution?
I build SharePoint Framework web parts, extensions, dashboards, and integrations using React, TypeScript, PnPjs, and Microsoft Graph.
Free SharePoint planning resource
Solving the technical issue is only part of the Microsoft 365 problem.
Use the checklist to review the governance, permission, ownership, and support decisions that often determine whether a SharePoint solution succeeds long term.
SharePoint architect & Microsoft 365 consultant • 16+ years of experience
If you have questions about your SharePoint environment, feel free to reach out.


