CRM Crate

Exploring the new Xrm.Copilot API client reference for Power Apps

In this blog post, we will explore the new Xrm.Copilot API client references introduced for Power Apps and Dynamics 365 Customer Engagement (CE), highlighting their capabilities and potential use cases. Before we begin, ensure you subscribe to CRM Crate to remain informed about the latest developments in the Power Platform field.

Exploring the new Xrm.Copilot API client reference for Power Apps

In Power Apps and Dynamics 365 Customer Engagement (CE), JavaScript plays a crucial role in enhancing the user experience by enabling client-side logic and customizations. Developers can use JavaScript to interact with form events, manipulate form data, validate user input, and integrate with external APIs. The JavaScript web client API provides a rich set of references through the Xrm namespace, which includes objects like Xrm.PageXrm.Navigation, and Xrm.Utility (in earlier versions) or formContext and executionContext in the Unified Interface. These APIs allow for dynamic control over form elements, such as showing/hiding fields, setting field values, or triggering workflows. 


Exploring the Xrm.Copilot Client API Reference

With the rise of AI-powered experiences in business applications, Microsoft has introduced a game-changing addition to the Power Platform: Xrm.Copilot. This new Client API, currently in preview, brings the capabilities of Microsoft Copilot Studio directly into your model-driven apps in Power Apps and Dynamics 365 CE.

Xrm.Copilot is a namespace in the Client API that allows developers to programmatically trigger Copilot Studio topics from within model-driven apps. Think of it as a bridge between your app and the conversational intelligence of Copilot Studio.

This means you can now:

  • Trigger AI-powered conversations based on user actions.
  • Automate responses using natural language prompts.
  • Seamlessly integrate Copilot into your custom business logic.

This client reference API exposes two main methods as given below:

Xrm.Copilot.executeEvent(eventName, eventParameters).then(successCallback, errorCallback);

Initiates a Microsoft Copilot Studio topic corresponding to the given Event Name.

This method triggers a Copilot Studio topic based on a registered event name. It’s ideal when you want to invoke a specific topic that’s already been configured in Copilot Studio.

For example, you can create a custom Copilot topic event and invoke it using the Xrm.Copilot.executeEvent API, as shown below.

Exploring the new Xrm.Copilot API client reference for Power Apps


The following JavaScript snippet illustrates how to use Xrm.Copilot.executeEvent to trigger a Copilot topic event from within Power Platform (Dynamics 365 Customer Engagement).

function executeCOPILOTEVENT(executionContext) {
    var formContext = executionContext.getFormContext();
    debugger;
    var eventName = "CRMCrate_COPILOT_EVENT";
    Xrm.Copilot.executeEvent(eventName)
        .then(response => {
            console.log("Copilot Response", response);
        })
        .catch(error => {
            console.error("Error during JDRequest:", error);    
        });
}

This method allows you to send a natural language prompt directly to Copilot Studio. It’s more dynamic and flexible, especially when you want to generate responses based on user input or contextual data.

Example use case: Asking Copilot specific questions, even though the Copilot used in this demo is not fully configured in this example.

The following JavaScript snippet demonstrates how to generate an AI-powered response by sending a custom natural language prompt to Copilot Studio.

function executeCOPILOTEVENT(executionContext) {
    const formContext = executionContext.getFormContext();
    const description = formContext.getAttribute("description").getValue();
    const prompt = "Give answer for : ${description}";
    Xrm.Copilot.executePrompt(prompt)
        .then((responses) => {
            responses.forEach((response) => {
                console.log("Copilot Response:", response.text);
            });
        })
        .catch((error) => {
            console.error("Error executing Copilot prompt:", error.message);
        });
}
Exploring the new Xrm.Copilot API client reference for Power Apps

Thus, Xrm.Copilot is a powerful step toward embedding AI natively into your business applications. Whether you’re a developer looking to enhance user experiences or a maker exploring automation, this API opens up exciting possibilities.


error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.