Skip to main content
Post

#PowerPlatformTip 180: 'Preload Config & User Context at Conversation Start in Copilot Studio'

#PowerPlatformTip 180: 'Preload Config & User Context at Conversation Start in Copilot Studio'

TL;DR: Treat the Conversation Start system topic as your init hub. Read Environment Variables (config that changes per environment, usable directly in topics) and per-user data from Microsoft Graph / Dataverse (department, open tickets), then store everything in Global variables (Global.UserDepartment). You configure context once, ALM stays clean, and no topic ever has to ask again.

💡 Challenge

Two smells show up in almost every growing agent:

🔸 Hard-coded config: URLs, SharePoint site IDs and API endpoints baked straight into topics. Move the agent from Dev to Test to Prod and it happily points at the wrong data.

🔸 Repeated context lookups: every topic re-queries “which department is this user in?” or “do they have open tickets?”, duplicating logic and slowing the conversation.

✅ Solution

Copilot Studio variables come in four scopes: Topic, Global, System, and Environment variables (created in Power Platform, usable in topics like other variables but read-only inside Copilot Studio, and designed to swap values automatically when a solution is imported into another environment). Combine two of them in one place:

🔸 Put your init logic in the Conversation Start system topic - it runs before the user’s first message.

🔸 Load config from Environment Variables and per-user context from Graph/Dataverse, and write both into Global variables (prefixed Global., alive for the whole session).

flowchart TD
    A([Conversation Start · system topic]) --> B[Read Environment Variables<br/>config per environment]
    A --> C[Call Microsoft Graph / Dataverse<br/>per-user context]
    B --> D[Set variable value<br/>Global.ApiBaseUrl]
    C --> E[Set variable value<br/>Global.UserDepartment]
    D --> F{{Global variables ready<br/>before first user message}}
    E --> F
    F --> G[Any topic reuses Global.* <br/>no re-asking, no hard-coding]

🔧 How It’s Done

1. Externalize config as Environment Variables

🔸 In your Dataverse solution, add Environment Variables for values that differ per environment (e.g. API base URL, site ID). On solution import, the target environment supplies its own value - no manual edits.

🔸 Environment variables are available in Copilot Studio as a variable scope and can be referenced in topics like Topic, Global and System variables, but only read, not changed. No flow is needed to read them.

🔸 The published agent contains the values the environment variables had at publish time. After an admin changes a value, republish the agent. Only variables of type Secret are read at runtime and need no republish.

2. Open the Conversation Start system topic

🔸 Go to Topics → System → Conversation Start. This topic fires at the very beginning of every session. Exception: in Microsoft Teams it runs only once per user (see Good to Know), so plan the Teams variant from the start.

3. Load config and user context

🔸 Add your actions: for per-user context, call the Office 365 Users connector action Get my profile (V2) (returns department, jobTitle, officeLocation of the signed-in user) or an agent flow that lists the user’s open tickets from Dataverse. Use the Environment variables directly for config. Decide with which identity these calls run. The profile and the tickets must be those of the person in the chat, so use the signed-in user’s own connection (user authentication on the connection) or, if a service connection runs the call, let the flow or server side check which authenticated person is asking and return only their data. Note that Get my profile (V2) always returns the profile of the identity behind the connection, so with a service connection it would return the service account. In that case use Get user profile (V2) with the verified user principal name or email instead. Test with two accounts that have different rights.

🔸 For the user’s name and e-mail address, no call is needed: System.User.DisplayName and System.User.Email are already there with the default Microsoft authentication.

4. Store the results in Global variables

🔸 Add Variable management → Set a variable value nodes. Set the target variable to a new variable and, in Variable properties, choose Global (any topic can access) - the name gains the Global. prefix, e.g. Global.ApiBaseUrl, Global.UserDepartment.

🔸 Example: Global.ApiBaseUrl gets the value of your environment variable for the API URL (pick it from the Environment tab of the variable picker), and Global.UserDepartment gets the department returned by your Graph call.

🔸 Every other topic and tool now reads Global.UserDepartment directly instead of re-querying it.

💡 Tip: If the embedding website or app should pass context in (for example a page or customer ID), enable External sources can set values on that Global variable in its Variable properties. Treat such a value only as a selection (which customer or page the user means), not as proof that this user may see it: before a protected lookup, check on the server side that the authenticated person may read that customer.

🎉 Result

Configuration is separated from logic, the agent adapts to each environment on import, and personalized context is ready the moment the conversation opens - fetched once, reused everywhere.

🌟 Key Advantages

🔸 Clean ALM: Environment Variables can swap per environment, so Prod uses its own endpoints, provided each environment gets the right value on import. Check the effective endpoint in Prod before you publish.

🔸 One lookup, everywhere: per-user context lives in Global.* instead of being re-queried in each topic.

🔸 Separation of concerns: config and context are decoupled from the agent’s conversational logic.

⚠️ Good to Know

🔸 Microsoft Teams: for agents published to Teams, Conversation Start runs only once per user, the first time the user adds the agent, because Teams uses the same conversation ID for every session. Global variables from that first run are not there in later sessions. Put the init logic into its own topic (for example “Set context”) and redirect to it from the topics that need it when your own initialization flag is not set yet (for example Global.ContextReady, which the “Set context” topic sets to true at its end). A check on IsBlank(Global.UserDepartment) alone would run the lookup again and again for a user whose department is legitimately empty, and it cannot tell a failed lookup from an empty value, so handle lookup errors separately.

🔸 Latency: Conversation Start runs before the first reply, so heavy Graph or Dataverse calls here delay the greeting. Load only what most conversations need and fetch the rest in the topic that uses it.

🔸 Read-only config: Environment variables can’t be changed in Copilot Studio. Change the value in the environment or solution, then republish the agent.

🛠️ FAQ

1. Can I read an Environment Variable directly in Copilot Studio?

Yes. Environment variables are available as a variable scope in Copilot Studio and can be referenced in topics, but they are read-only. The agent uses the value from its last publish, except for Secret variables, which are read at runtime.

2. Why the Conversation Start topic instead of each topic?

It runs at the start of the conversation, so you populate context before the user types. In Teams it runs only once per user, so there a dedicated “Set context” topic that other topics redirect to is the reliable variant. Either way, you configure it once and reuse Global.* everywhere.

3. How long do Global variables live?

For the current conversation. Their names must be unique across all topics and carry the Global. prefix. The Reset Conversation system topic clears them.

4. Won’t the Graph/Dataverse calls slow the agent down?

They can, because Conversation Start runs before the first reply. Keep init to one or two fast calls and load everything else in the topic that needs it.

5. Do I still hard-code anything?

No. Endpoints and IDs move into Environment Variables (config), user context comes from Graph/Dataverse at runtime, and both land in Global.* - the agent logic stays free of literals.

🔸 #PowerPlatformTip 61: ‘Power of Environment Variables’ - the ALM foundations of Environment Variables across Dev/Test/Prod.

🔸 #PowerPlatformTip 105: ‘EnvironmentVariables Without Premium’ - the flow-based way to read Environment Variables, for places where no variable scope is available.

🔸 #PowerPlatformTip 174: ‘Self-Healing Connector Actions in Copilot Studio’ - the agent flow pattern for tools that call an API, which can use the endpoints you load here.

This tip is by Marcel Lehmann, Microsoft MVP (Business Applications) and founder of the Power Platform Academy, which runs free live workshops (in German). See the next free workshop dates (in German). For companies that want this implemented for them, see KMUpower: Power Platform consulting & automation for SMEs (DACH).

This post is licensed under CC BY 4.0 by the author.

Microsoft and the Power Platform product names (for example Power Apps, Power Automate, Copilot Studio) are trademarks of the Microsoft group of companies. This is an independent website. It is not operated, sponsored or endorsed by Microsoft.