Tuesday, December 3, 2019

How To Force Canvas Apps To Update An Edited Component


Frustrations With Updating a Component

Canvas Apps Components are an experimental feature that allow app creators to define a component that can be used in multiple places within an app, or in multiple apps, allowing for reuse and more DRY apps.  On a recent project I ran into an issue where, when attempting to update a component, it created a new component, and left the old version of my component in the app.  This means if I wanted the new changes from the component, I would have had to manually replace every instance of my component in the app.  Not fun!

What's Going On?

The Canvas App studio is attempting to be nice and keep from losing any changes that were made in a component inside your app.  It does this by "un-linking" the component from the source component whenever you update the component in the app that it is being used in, rather than the source app that it is being exported from.  So how does one "re-link" the app specific component to the source?

Canvas App Packager to the Rescue!!!

Using the Canvas App Packager, I was able to see what was going on in the app itself to link/unlink the component by unpacking the app and looking at the changes under the hood.  By default when a component is imported into an app, the Properties.json file contains the template for the component with the following header information:


But when an edit is made to the component in the app, the OriginalName property is removed, and the IsComponentLocked variable is set to false.  To allow the component to be refreshed when the component is reloaded, these will need to be added back.  The simplest approach to determining the OriginalName, is to re-add the component, making a duplicate, and then unpacking the app again to see what the OriginalName should be.  After adding back the OriginalName, flip the IsComponentLocked back to true, and pack and re-import the app.  Viola!  Now if the source component is imported again, it will actually update the component in the app rather than creating a duplicate!

Tuesday, October 8, 2019

How to Enable PCF Components for Older Canvas Apps

The Backstory

I wanted to try and embed a browser into one of my existing canvas apps but ran into a snag.  I followed the instructions in the docs on enabling PCF components (https://docs.microsoft.com/en-us/powerapps/developer/component-framework/component-framework-for-canvas-apps) but I could only input Canvas based components, not PCF components because the "Import Components" "Code (experimental)" tab wasn't showing up even after I turned on the components preview option for the app:

I was able to eventually get the PCF Components to show up, but that required me to turn on every single preview/experimental feature of the app.  I was concerned that maybe this was because my app was running on an old version of Canvas Apps, so I upgraded by App to the latest version of the app, and PCF components were still not showing up (Again, never had any problem with Canvas components showing up).  I then proceeded to add every single experimental feature in the app settings, and again, the PCF components tab showed up, but when I imported the app into a new environment, the "Explicit Column Selection" feature broke the app.  Turning off this feature removed my PCF control from the app, so I was in a no-win situation.

To test my theory that the issue was because my app had some legacy bloat which was causing it to fail, I created a brand new app, and the PCF components showed up exactly as expected.  I then extracted my app using the CanvasApp Packager (https://github.com/daryllabar/CanvasAppPackager) and compared the differences in the extract json and found the fix!


Actual How To

To get get the PCF Controls experimental feature to show up in your older canvas app follow these steps:

  1. Export your app from the make.powerapps.com site to your machine.
  2. Unpack the app using the CanvasApp Packager (https://github.com/daryllabar/CanvasAppPackager).
  3. Open the Extract\Apps\<App Name>\Properties.json file.
  4. Search for the AppPReviewFlagsKey array.
  5. Add "nativecdsexperimental" to the end of the array e.g. "AppPreviewFlagsKey":["delayloadscreens","componentauthoring", "nativecdsexperimental"]
  6. Pack the app using the CanvasApp Packager.
  7. Import back into your make.powerapps.com environment.
  8. Enjoy being able to select your PCF components in your older Canvas App!

Saturday, May 11, 2019

Negotiating the CDS/CRM/Xrm Plugin Trace Log Length Limitation

With the Dynamics 365 CRM 2015 U1 update, the Plugin Trace entity was added to the platform.  This provided an OOB implementation for the ITracingService (although it only works in Sandboxed plugins) to log to that was a much needed addition to the platform.  Over time, my dependency and use of the ITracingService within the DLaB.Xrm library has greatly increased.  By default, the plugin base auto logs the name of the plugin that is executing, the start and stop time, each IOrganizationService call that is made, and on exceptions, the entire plugin context to make debugging easier.   With all of this logging, it is becoming more and more common for plugins to exceed the 10,240 character limit.  This results in the beginning of the trace log getting truncated.

So what’s the solution?  You could completely abandon the built in ITracingService, and trace to Application Insights.  As much as I love that solution, for anything but large CRM/CDS implementations it may be overkill.  With the assumption that most of the time, the information that is helpful for debugging will be at the beginning or the end of the trace, I’ve updated the default ITracingSevice in the DLaB.Xrm library, when in cases of the tracing being longer than 10,240 characters, to retrace the first 5,120 characters, and then retrace the last 5,120.

Let’s take a look at the implementation:
public class ExtendedTracingService: IMaxLengthTracingService
{
    private ITracingService TraceService { get; }
    public int MaxTraceLength { get; }
    private StringBuilder TraceHistory { get; }
 
    public ExtendedTracingService(ITracingService service, int maxTraceLength = 10244) {
        TraceService = service;
        MaxTraceLength = maxTraceLength;
        TraceHistory = new StringBuilder();
    }
 
    public virtual void Trace(string format, params object[] args) {
        try
        {
            if (string.IsNullOrWhiteSpace(format) || TraceService == null)
            {
                return;
            }
 
            var trace = args.Length == 0
                ? format
                : string.Format(format, args);
            TraceHistory.AppendLine(trace);
            TraceService.Trace(trace);
        }
        catch (Exception ex)
        {
            AttemptToTraceTracingException(format, args, ex);
        }
    }

    /// <summary>
    /// If the max length of the trace has been exceeded, the most important parts of the trace are retraced.
    /// If the max length of the trace has not been exceeded, then nothing is done.
    /// </summary>
    public void RetraceMaxLength()
    {
        if(TraceHistory.Length <= MaxTraceLength)
        {
            return;
        }
 
        var trace = TraceHistory.ToString().Trim();
        if(trace.Length <= MaxTraceLength)
        {
            // White Space 
            Trace(trace);
            return;
        }
 
        //Assume the three Traces will each add New Lines, which are 2 characters each, so 6
        var maxLength = MaxTraceLength - 6;
        if(maxLength <= 0)
        {
            return;
        }
 
        var snip = Environment.NewLine + "..." + Environment.NewLine;
        var startLength = maxLength / 2 - snip.Length; // Subtract snip from start
        if(startLength <= 0)
        {
            // Really short MaxTraceLength, don't do anything
            return;
        }
        Trace(trace.Substring(0, startLength));
        Trace(snip);
        Trace(trace.Substring(trace.Length -(maxLength - (startLength + snip.Length))));
    }
}
The ExtendedTracingService wraps the default ITracingService from the platform, and then on calls to trace, it intercepts the call, and adds the trace to an in memory StringBuilder first, before actually tracing the call.  The final step in the plugin base is to then call RetraceMaxLength().  This will check the length of the StringBuilder, and if it’s over the max length, trace the first part of the traces, and then the last part, with an “…” in the middle to serve as a “Snip” statement.

If you’re already using the DLaB.Xrm.Source library, get the latest (>= 2.3.0.13) version from NuGet, and enjoy ensuring you always see the beginning and end of reach trace.  If you’re not using the DLaB.Xrm.Source Library, why not? It’s free, open source, and because it’s a source only NuGet package, doesn’t require ILMerge when being used from a plugin.  You can even use the Visual Solution Accelerator in the XrmToolBox to bring it into your existing CRM/CDS VS solution.

Here is an example log: (I’ve removed a great deal of text, just notice that the “…” serves as the signal that the trace was too long, and the middle portion has been truncated)


Starting Timer for Execute Request for dlab_LeadSearch with * Parameters *,     Param[IsMovers]: False,     Param[PhoneNumber]: 5553214321.
Timer Ended (  0.096 seconds)
Starting Timer: IsRejectedOrCreateLead
Partner Not First Party
Lead validations:
is 30 Days Logic: True
is Same Day Inquiry: True
is rejected: True - 30 Day Lead Logic
Timer Ended (  0.000 seconds): IsRejectedOrCreateLead
Starting Timer: is30DaysLogic
Timer Ended (  0.000 seconds): is30DaysLogic
Starting Timer for Create Request for dlab_leadqualifier with Id 00000000-0000-0000-0000-000000000000 and Attributes     [dlab_name]: Homer Simpson
     [dlab_azureid]: 317578
     [dlab_5daylogic]: 0
     [dlab_7daylogic]: 0
     [dlab_14daylogic]: 0
     [dlab_30daylogic]: 0
     [dlab_30daylogiccurrent]: 1
     [dlab_jornayalogic]: 0
     [dlab_dayslogic]: 0
     [dlab_existinglead]: True
Timer Ended (  0.033 seconds)
Start of isUpdatePath - 2019-05-11-07:32:41 407
Starting Timer for Update Request for lead with Id c97e6f52-6431-e911-8190-e0071b663e41 and Attributes     [trans
...
    Param[skipExport]: False
     Param[subdivision]:
     Param[subTotal]:
     Param[primaryPhoneDnc]: False
     Param[secondaryPhoneDnc]: False
* Output Parameters *
     Param[response]:
PostEntityImages: Empty
PreEntityImages: Empty
* Shared Variables *
     Param[Example.Xrm.LeadApi.Plugins|dlab_createLeadRequest|PostOperation|00000000-0000-0000-0000-000000000000]: 1
Has Parent Context: False
Stage: 40