Copilot Studio agent deployed on SharePoint site - global variable is not working

Kumbhar, Kanchan 5 Reputation points
2025-08-11T11:46:23.9166667+00:00

Hi everyone,

I’ve implemented a Microsoft Copilot agent using Copilot Studio and deployed it directly to a SharePoint Online site. The agent is designed to capture a value from the query string in the page URL and store it in a global variable for use during the conversation.

This setup works perfectly on a demo website, where the agent successfully reads the query string and stores the value. However, when I test the same agent on the SharePoint Online site, it fails to capture the query string value.

What I’ve Tried:

  • Used the global variable approach to read the query string.
  • Verified that the query string is correctly appended to the SharePoint page URL.
  • Confirmed that the agent is embedded and loads correctly on the SharePoint page.

Issue:

On SharePoint Online, the global variable does not get populated with the query string value, even though the same logic works on a non-SharePoint demo site.

Questions:

  • Are there any known limitations or restrictions with query string parsing in SharePoint Online when using embedded Copilot agents?
  • Is there a recommended workaround or alternative method to pass values from SharePoint to the agent?
  • Could this be related to how SharePoint handles iframe embedding or URL rewriting?

Any insights or suggestions would be greatly appreciated!

Thanks in advance.

Microsoft Copilot | Microsoft 365 Copilot | Development
{count} votes

1 answer

Sort by: Most helpful
  1. Karan Shewale 1,040 Reputation points Microsoft External Staff
    2025-08-12T05:12:45.1466667+00:00

    Hi Kanchan,

    This is a common issue when deploying Copilot Studio agents on SharePoint Online. The problem stems from how SharePoint handles iframe embedding and URL context isolation.

    Root Cause

    SharePoint Online isolates embedded content (like Copilot Studio agents) in iframes, which prevents direct access to the parent page's query string parameters. The agent runs in a separate context and cannot access the SharePoint page's URL directly.

    Solutions

    1. Use SharePoint Web Parts with Custom Properties

    // In a SharePoint Framework (SPFx) web part
    const urlParams = new URLSearchParams(window.location.search);
    const paramValue = urlParams.get('yourParameter');
    // Pass to Copilot agent via custom properties or postMessage
    

    2. Implement PostMessage Communication

    // On SharePoint page
    window.addEventListener('load', function() {
        const iframe = document.querySelector('#copilot-iframe');
        const urlParams = new URLSearchParams(window.location.search);
        
        iframe.contentWindow.postMessage({
            type: 'sharepoint-params',
            data: Object.fromEntries(urlParams)
        }, '*');
    });
    

    3. Use SharePoint Context API

    Configure your agent to receive context through SharePoint's web part properties:

    {
        "contextParameters": {
            "siteUrl": "{{Site.Url}}",
            "listId": "{{List.Id}}",
            "customParam": "{{QueryString.yourParam}}"
        }
    }
    

    4. Alternative: Custom Connector Approach

    Create a Power Automate flow that:

    • Captures SharePoint context
    • Passes parameters to your Copilot agent
    • Returns responses back to SharePoint

    Recommended Implementation

    Step 1: Modify your Copilot Studio agent to accept parameters via a custom action instead of global variables.

    Step 2: Create a SharePoint Framework web part that:

    • Reads the query string from the parent page
    • Passes parameters to the embedded agent
    • Handles the communication bridge

    Step 3: Deploy the custom web part to your SharePoint app catalog.

    Quick Workaround

    If you need an immediate solution:

    1. Modify the SharePoint page to include a JavaScript snippet that extracts query parameters
    2. Use SharePoint's built-in script editor web part to inject the parameter into the agent's initialization
    3. Update your agent to receive the parameter through a different method (custom action or topic trigger)

    SharePoint-Specific Considerations

    • Security: SharePoint's Content Security Policy may block certain scripts
    • Permissions: Ensure proper app permissions for cross-domain communication
    • Updates: SharePoint updates may affect iframe behavior
    • Mobile: Consider mobile SharePoint app limitations

    The most robust solution is implementing a custom SPFx web part that properly handles the parameter passing between SharePoint and your Copilot agent.

    This is the Feedback document link

    Thanks,  

    Karan Shewale. 

    *************************************************************************  

    If the response is helpful, please click "Accept Answer" and upvote it. You can share your feedback via Microsoft Teams Developer Feedback link. Click here to escalate. 

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.