January 7, 2016

Pay attention to the CRM version when using the SDK NuGet packages

Microsoft publishes the CRM SDK DLLs also as NuGet packages, you can find the list by browsing the crmsdk profile:

https://www.nuget.org/profiles/crmsdk

Because the packages are not separate by CRM version (for example a package for CRM 2015 and a package for CRM 2016) Microsoft simply updates the NuGet package increasing the version.
This can be an issue in some projects as happened to me some days ago. I was working on a plugin for CRM 2015, my project was targeting .NET 4.5.2 and I installed the current version of CoreAssemblies (8.0.1).
The build was successful but the plugin always returned the exception "Value cannot be null".

I spent some time to understand that the problem was the referenced DLL version, so I removed the NuGet package and I installed the 2015 specific version (7.1.1) using the Package Manager Console:

Install-Package Microsoft.CrmSdk.CoreAssemblies -Version 7.1.1

After the plugin worked like a charm.
As reference these are the current versions and the suggested .NET framework to use:
  • CRM 2011: 5.0.18 (.NET 4.0)
  • CRM 2013: 6.1.1 (.NET 4.0)
  • CRM 2015: 7.1.1 (.NET 4.5.2)
  • CRM 2016: 8.0.1 (.NET 4.5.2)

January 2, 2016

Another year with the MVP community

Yesterday I received my second MVP Award and it's a great way to start the New Year!
Be in the first quarter (or in the second one due to April Fools' Day) is a bit thrilling but worth the wait. Last October the MVP program made some changes, so my award category is now called Business Solutions and includes MVPs from the Dynamics family (CRM, AX, GP, NAV) and from Microsoft Project.

An anniversary is also a moment to look back and see what was done last year. Personally I contribute to these sites: Dynamics Community, MSDN Forums and StackOverflow.

I start with the last. StackOverflow doesn't have a so good reputation inside the Dynamics CRM community, probably because it's not the right place to ask all the questions, it's a site for developers, it has rules and you can't just drop there and ask: "How do I install the Email router?", but it's a great community with extraordinary members.

MSDN has forums for CRM questions, but I noticed in the last year that the number of questions asked there is lower, there are also some regional CRM forums (like in German or French) but I don't know if this trend I noticed affected also that part.

Dynamics Community had a huge increment of participation: more questions, more active users, more feedback. It's a site for everybody, beginners and experts, a great community, probably THE Dynamics CRM community, you can find everybody there.

Last year I also coded a bit and released two small utilities: a library to handle Moment.js multi language inside Dynamics CRM and a Theme Generator.

The library for Moment.js is a small thing, you can find details here but it got a mention on CRM Tip Of The Day.

The Dynamics CRM Theme Generator had more success, it has daily visitors and was mentioned or reviewed in some CRM articles, in particular this one that I like very much:

My Eyes! How to Create Jarring Themes Using the Theme Generator

2015 was a long and full year and I hope 2016 will be Harder, Better, Faster, Stronger!

Guido
ps: I started a new blog (8086.it)

December 2, 2015

India and Canada Data Center Discovery URLs

In the last months Microsoft announced the opening of new data centers for Dynamics CRM Online in India and Canada.
These data centers are not available yet, but Microsoft defined the Discovery Web Service URLs. The addresses are:
https://disco.crm8.dynamics.com/XRMServices/2011/Discovery.svc (India)
https://disco.crm3.dynamics.com/XRMServices/2011/Discovery.svc (Canada)

The Indian data center is identified by the crm8 host, the Canadian one by crm3. This will affect also the organization url, for example if Contoso Ltd. has two branches, one in India and the other one in Canada, their urls will be:
https://contosoindia.crm8.dynamics.com
https://contosocanada.crm3.dynamics.com

The relative MSDN page has NOT been updated to include this change, the next table is a recap:

HostLocation
crmNorth America
crm4EMEA
crm5APAC
crm2South America
crm9North America 2 (CRM Online for Government)
crm7Japan
crm6Australia
crm8India
crm3Canada

July 21, 2015

Visual Studio 2015 Shared Projects and CRM Plugins Development

Visual Studio 2015 is now available and inside the new features and improvements there is one that I like very much: Shared Projects. A Shared Project is intended to share easily the code between different platforms.
For C# projects this was already possible using (portable) class libraries and adding the assembly to the main project references.

Which is the big advantage of Shared Projects for a Dynamics CRM developer?
With Shared Projects the code is NOT compiled to a separate assembly but directly inside the main assembly of your project.

This is can be very useful for Plugin and Custom Workflow Activity development, because now we can create a common library with CRM methods to be used inside our plugins WITHOUT the need to use ILMerge for creating a single assembly in order to be registered inside Dynamics CRM.

As example I created a small shared project containing a simple method to check the current CRM Version:
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
namespace CRM.Common
{
    public static class Utils
    {
        public enum CRMVersion
        {
            Unknown = 0, CRM2011 = 2011, CRM2013 = 2013, CRM2015 = 2015
        }

        public static CRMVersion GetCRMVersion(IOrganizationService service)
        {
            RetrieveVersionRequest versionRequest = new RetrieveVersionRequest();
            RetrieveVersionResponse versionResponse = (RetrieveVersionResponse)service.Execute(versionRequest);
            string version = versionResponse.Version;
            if (version.StartsWith("5")) { return CRMVersion.CRM2011; }
            if (version.StartsWith("6")) { return CRMVersion.CRM2013; }
            if (version.StartsWith("7")) { return CRMVersion.CRM2015; }
            return CRMVersion.Unknown;
        }
    }
}
After I created a normal Class Library project for my plugin and I added the Shared Project to the solution and a reference inside the Class Library:

The plugin code is very simple, it throws an exception indicating the CRM version:
using Microsoft.Xrm.Sdk;
using System;
using static CRM.Common.Utils;

namespace CRM.MyPlugin
{
    public class MyPlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            CRMVersion version = GetCRMVersion(service);
            throw new InvalidPluginExecutionException(version.ToString());
        }
    }
}
If we compile the solution only a single assembly is generated and if we analyze it using ILSpy we can confirm that the Shared Project is compiled inside the main assembly:

And the plugin works without issues:


Shared Projects can be very useful for Dynamics CRM development because will definitely improve the quality and the reuse of the code.

Shared Projects are also available for Visual Studio 2013 as separate addon, you can download from here: Shared Project Reference Manager

July 14, 2015

New Dynamics CRM for phones iOS App and getFormFactor method

Yesterday Microsoft released the new iOS App for Dynamics CRM (iTunes link). The app only works with CRM versions 7.1 and higher, this means currently can be used only with CRM Online instances that received the Update 1.

The new App supports JavaScript so I wanted to test a little script with the method getFormFactor.

getFormFactor (MSDN link) is a new method introduced with CRM Online 2015 Update 1 returning a numeric value based on the current device. The possible values are:

ValueForm Factor
0Unknown
1Desktop
2Tablet
3Phone

The script I wrote is:
function accountOnLoad() {
    var factor = Xrm.Page.context.client.getFormFactor();
    var message = "The Form Factor is: " + factor;
    Xrm.Utility.alertDialog(message);
}
And the result is:

Note: the script is not called when a new record is created.

June 18, 2015

Hide fields dynamically based on Field Level Security privileges

When we set up Field Level Security for a field we can assign three different privileges (Read, Update, Write) to the users.
It is also possible to check the privilege using a supported JavaScript method: getUserPrivilege
This method returns an object with three boolean properties: canRead, canUpdate, canCreate.

We can use these properties in combination with the forEach method of attributes and controls collections in order to dynamically hide the fields.

The following code will hide dynamically all the fields protected by Field Level Security that the user can't read:
function HideSecuredFields() {
    // fetch all attributes on the form
    Xrm.Page.data.entity.attributes.forEach(
        function(attribute, aIndex) {
            // get the FLS privileges
            var privileges = attribute.getUserPrivilege();
            // check if the user can't read the field
            if (privileges.canRead == false) {
                // fetch all the controls related to the attribute
                attribute.controls.forEach(
                    function (control, cIndex) {
                       // hide the controls
                       control.setVisible(false);
                    }
                );
            }
        }
    );
}