Integrating Client-Side Scripts with Server-Side Plugins in Dynamics 365

 

Integrating Client-Side Scripts with Server-Side Plugins in Dynamics 365

Introduction

Integrating front-end actions with back-end server plugins in Dynamics 365 allows for robust process automation and enhanced user interaction. This article presents a generalized approach, showcasing a client-side script that triggers a custom action, subsequently invoking a server-side plugin for comprehensive business logic execution.

Client-Side Custom Action Implementation

  1. Triggering Custom Action from Client-Side Script:
// Initiates a custom action from a client-side script, like a ribbon button click
async function TriggerCustomAction(primaryControl, actionName, userConfirmation, progressMessage) {
    try {
        const userResponse = await getUserConfirmation(userConfirmation);
        if (!userResponse || userResponse === "cancelled") return;

        Xrm.Utility.showProgressIndicator(progressMessage);
       
        const entityId = primaryControl.data.entity.getId().replace(/[{}]/g, "");
        const actionData = createActionData(entityId, actionName, userResponse);
        const response = await Xrm.WebApi.online.execute(actionData);
       
        await processActionResponse(response, primaryControl);
    } catch(err) {
        console.error("Custom Action Error: " + err);
    } finally {
        Xrm.Utility.closeProgressIndicator();
    }
}


  • Handles user confirmation and triggers a custom action.
  • Processes the response for UI updates.
  1. Handling Custom Action Response:
// Processes the response from the custom action
async function processActionResponse(response, primaryControl) {
    if (response.ok) {
        // Implement success logic, such as refreshing form data
        await primaryControl.data.refresh();
    } else {
        throw new Error("Custom action execution failed");
    }
}

  • Handles both successful and failed executions of the custom action.

Server-Side Plugin Logic

  1. Generic Plugin Framework:
// General plugin structure for handling custom actions
public class GenericCustomActionPlugin : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        IOrganizationService service = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(context.UserId);
        ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

        if (context.MessageName != "expected_CustomActionName") return;
        if (context.PrimaryEntityName != "expected_EntityName") return;
        if (!context.InputParameters.ContainsKey("Target")) return;

        try {
            // Your business logic here
        }
        catch (Exception ex) {
            tracer.Trace("Exception: {0}", ex);
            throw;
        }
    }
}

  • Tailored to a specific custom action and entity.
  • Executes the defined business logic based on the action's input.

Conclusion

This general framework demonstrates how client-side scripts can effectively interact with server-side plugins in Dynamics 365, enabling complex process automation. By leveraging this approach, developers can craft sophisticated solutions that marry front-end user actions with back-end logic, enhancing both functionality and user experience within Dynamics 365 environments.




No comments:

Post a Comment