Friday, July 24, 2015

A bookmarklet to open a entity by Guid in Dynamics CRM

So I colleague of mine passed me a number of CRM 2013 bookmarklets that do all sorts of things, like showing the guid of the entity, enabling all fields, removing the business required attribute, etc. The 'god mode' one is the most fun.

Anyway, I decided to write one of mine: a bookmarklet that enables you to open an entity using it's guid. Unfortunately, right now, I ask that you also give me the entity name, but other than that, it's pretty useful while debugging applications that make extensive use of the SDK.

So, without any more delays: get it here.

If you are curious, you can check the bookmarklet code below.

javascript:var entity = window.prompt("Enter the name of the entity to open the default form for:", "");var guid = window.prompt("Enter the guid of the entity to open thedefault form for:", "");var o = new Object(); o.uri = "main.aspx?etn=" + entity + "&pagetype=entityrecord&id=%7B" + guid + "%7D";window.top.document.getElementById("navBar").control.raiseNavigateRequest(o);

Here's the same code block in a more readable way:

javascript:
var entity = window.prompt("Enter the name of the entity to open the default form for:", "");
var guid = window.prompt("Enter the guid of the entity to open thedefault form for:", "");

var o = new Object(); 
o.uri = "main.aspx?etn=" + entity + "&pagetype=entityrecord&id=%7B" + guid + "%7D";
window.top.document.getElementById("navBar").control.raiseNavigateRequest(o);

Tuesday, July 7, 2015

USD: Default Actions In Hosted Controls

While I don't find time to do a proper introduction over Unified Service Desk, Microsoft's platform for the development of call center systems, I'll keep bringing some nice-to-know-facts about it.

Custom Hosted Controls

A custom hosted control is basically a WPF UserControl specialized to wire-up against USD's internal engines. To develop it, you can download and install the UII SDK and the Visual Studio Extension that contains the templates for it. Or you can do it the hardcode way, which I prefer, and simply install a USD nuget:

PM > Install-Package Microsoft.CrmSdk.USD.CoreAssemblies
Once that's done, simply create a new WPF UserControl and change it's base class to DynamicsBaseHostedControl:

< dynamics:dynamicsbasehostedcontrol x:class="MyLib.Controls.Views.FooView" 

                                        xmlns:dynamics="clr-namespace:Microsoft.Crm.UnifiedServiceDesk.Dynamics;assembly=Microsoft.Crm.UnifiedServiceDesk.Dynamics"
                                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                                        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" >

Nice! Now you can start developing you own USD control.

Actions and Action Calls

There are only two reasons to actually create DynamicsBaseHostedControl controls. One is to access the USD Context, the other is to wire up Actions and Events.

Microsoft's documentation over Action and Events is surprisingly good, so it's worth to check out. But it fails to talk about how to develop using action and events. I'll probably address this more comprehensively in the coming posts, but, for now, let's just focus on how to create custom actions that you can call from other controls.

In your DynamicsBaseHostedControl, override the method DoAction:

protected override void DoAction(RequestActionEventArgs args)
{
    switch (args.Action)
    {
        case "ActionName":
            DoSomething();
        break;
        
        case "AnotherAction":
            DoSomethingElse();
        break;

            // (...)
    }

    base.DoAction(args);
}

The args contain everything you should need to implement that call:
  • Its property Action contains the name of the action that was called.  You should switch on it.
  • Its property Data contains the parameters that were sent with the action call
[as you can see from above, USD unfortunately suffers from String Obsession (a common kind of Primitive Obsession), since it's heavily based on parametrization]

Now, the call to base.DoAction is very important: every DynamicsBaseHostedControl implements a few standard actions. Microsoft documentation on this is very thin (as is on everything USD), but at least these actions are implemented:
  • new
  • new_crm_page
  • open
  • open_crm_page
  • popup
  • close
  • fireevent
  • movetopanel
  • setsize
If you don't call the base, you are effectively disabling all these standard behaviors. But here's the grand finale and the reason for this post: USD won't warn against unknown actions, so if you send the action "foobar" against it, it will not reject, only log it. You can imagine what this means in practice: a typo can cause all sorts of pain. So, be wary when dealing with actions.