Sonoma Partners Microsoft CRM and Salesforce Blog

Create Dynamics 365 Plugins with VS Code and the .NET Core SDK

Today's blog post was written by Ian Moore, Senior Developer at Sonoma Partners.

All the buzz in the Microsoft development ecosystem in recent months has been about .NET Core. If you haven’t been able to keep up with all the new technology and terms, here is a quick recap:

  • .NET Framework – The version of .NET you know and love. It is installed at the operating system level and runs on Windows.
  • .NET Core – A new implementation and runtime of .NET that is cross-platform across Windows, Linux, and MacOS. Applications that target .NET Core include the necessary framework assemblies with the application.
  • Xamarin – A .NET implementation that runs on mobile devices with Android or iOS.
  • .NET Standard – This is specification of .NET APIs – things like System.Xml, System.Linq, etc. – that are available on a given .NET runtime. Code that is targeting .NET Standard can run on .NET Framework, .NET Core, and Xamarin.
  • NET Core – This is the latest version of the ASP.NET web application framework. It can run on both .NET Core and .NET Framework.
  • .NET Core SDK – This is a new, cross-platform, standalone SDK for .NET applications. It includes a new project file format, NuGet enhancements, and a command line tool that can build and run .NET projects. This SDK can build projects for .NET Core, .NET Standard, and .NET Framework.

Okay, not so quick after all – but there’s a lot of new and great things happening in the .NET world these days. What do these .NET Core developments mean for developers who work with Dynamics 365? Since the Dynamics SDK Assemblies are still built for .NET Framework, we can’t use them to build anything cross-platform. But we can use the new .NET Core tools to develop plugin assemblies and console apps that connect to Dynamics 365. With Visual Studio Code, a new text editor from Microsoft, we can do all the development we need without ever opening Visual Studio.

Here’s what you need to get started:

1. Install the .NET Core SDK from Microsoft - https://www.microsoft.com/net/core#windowscmd

2. Open a command prompt and run ‘dotnet’ to confirm the SDK command line tools were installed successfully.

3. Install Visual Studio Code - https://code.visualstudio.com/

4. Start Visual Studio Code and open the Extensions menu. Search for the ‘C#’ extension and install it.

With those installed, we can make a Dynamics 365 plugin project with a few simple steps.

1. Open VS Code and go to File -> Open Folder and choose a new folder for your plugin source code.

2. Open the VS Code Integrated Terminal from the View menu or using Ctrl+` . This terminal will be PowerShell or cmd depending on your VS Code settings. https://code.visualstudio.com/docs/editor/integrated-terminal

3. In the terminal, run dotnet new classlib to create a new Class Library project in your current directory.

Ian moore blog 1

4. The command places two files in your directory: a stub .cs class file, and a .csproj file named after your directory. Open the .csproj file in the editor.

5. If you have ever looked at a csproj file in Visual Studio before, you will immediately notice the new format is significantly smaller. However, we need to change the TargetFramework to .NET Framework 4.5.2 to match the Dynamics SDK.

Ian moore blog 2

6. Now we need to add the Dynamics SDK package from NuGet. In the terminal run dotnet add package Microsoft.CrmSdk.CoreAssemblies. You will see this command adds a PackageReference to your project file.

7. Run dotnet restore to resolve the package reference.

Ian moore blog 3

8. Rename the Class1.cs file to MyPlugin.cs and open the file in the editor. Try making a simple plugin and notice that you have full intellisense support.

Ian moore blog 4

Here is an example plugin I wrote:

Ian moore blog 5

9. Back in the terminal, run dotnet build to build the project. You will find the output DLL in the bin/Debug/net452 folder.

Ian moore blog 6

10. There is still one more step required before we can register the assembly in Dynamics 365. We need to give the assembly a strong name using the strong name tool (sn.exe).

11. If you have a strong name key file already, you can skip this step. Otherwise, search for “Developer Command Prompt for VS2015” in the Start Menu and create a new key file with the command sn -k [YourProject].snk.

Ian moore blog 7

12. Back in VS Code, add the following lines to your csproj file within the Project element:

<PropertyGroup>
    <SignAssembly>true</SignAssembly>
    <AssemblyOriginatorKeyFile>CorePlugins.snk</AssemblyOriginatorKeyFile>

</PropertyGroup>

13. Your assembly will now have a strong name. Run dotnet build again from the command line to rebuild your project.

14. Now you can register your DLL with Dynamics 365 through the Plugin Registration Tool

Ian moore blog 8

With these steps complete, you can continue to add new plugin types by adding .cs files and including them in the .csproj file. This is exciting news for Dynamics 365 developers - it allows you to have all the tools needed to build plugins with the fully configurable VS Code editor. While Visual Studio will continue to be the go-to development solution for Dynamics development, it is nice to have a new and reliable option moving forward.

Topics: Microsoft Dynamics 365