How I develop and unit test CRM 2011 plugins

This is probably the most promised, but never published blog post on my list of potential posts.  I’ve managed to convince enough CRM developers I’ve run into that they should get some test driven development with unit testing religion.  Especially because of the added productivity benefits for CRM plugin development.  This post isn’t to convince you of the benefits of unit testing.  There are plenty of articles/blogs which cover that topic just fine.  I’m not interested in debating the virtues of unit testing.  Love it or leave it;). 

The purpose of this post/video is to show you how I develop and unit test plugins.  There are a few articles out there about how to unit test CRM 2011 plugins.  I haven’t been satisfied with the approaches I’ve seen.  I landed on an approach which combines using the CRM 2011 Plugin Testing Tools for mocking IServiceProvider and Moles fore detouring types used inside the plugin which are difficult or time consuming to mock such as OrganizationServiceContext, Web Service calls, and others.

You can grab the finished example here:

http://sdrv.ms/StxcgU

You can also grab the starter solution I mention in the video here:

http://sdrv.ms/ScFUNf

Once you’ve learned the basics in the video, hop on over to the series below for some more advanced examples:

http://zhongchenzhou.wordpress.com/2012/07/08/dynamics-crm-2011-unit-test-part-1-introduction-and-series-contents/

The series above focuses on using Fakes in VS2012.  Fakes is the successor to Moles. However, the general techniques still apply whether using Fakes/Moles.  If you are using VS2012, then use Fakes.  If you are using VS2010, then use Moles.

I find many people who are new to unit testing have questions about where it fits in relation to other forms of testing.  Unit testing does not replace other forms of testing such as integration testing (i.e. testing the plugin running in CRM).  TDD through unit testing is an upfront developer discipline.  It requires a certain culture and mindset on a dev team.  In my opinion and experience, it helps me produce better code and allows me to catch more (not all) bugs early on, before handing things off to other testers.  It also makes the others testers lives/jobs easier. 

When applying this approach to CRM plugins, there’s a bonus side effect that (for me) allows me to be more productive in authoring my plugin code because I can:

  • Iterate (make code changes, build, debug and/or test, repeat) rapidly without needing to deploy to a CRM server
  • Iterate on my code rapidly without affecting the external systems
  • Use code coverage to identify untested scenarios which enable me to build better, more complete tests. 
    • This, in turn, allows me to build better, less buggy code.
  • The nature of having a suite of automated tests allows you to identify issues and regressions in your code early on as you make changes
    • This becomes more and more valuable over time as you build out your plugins and corresponding tests
  • Other devs don’t have to know how to test your code.  They just run the tests you’ve written

@devkeydet

The Dynamics CRM resource so many have been looking for

http://www.crmfieldguide.com/

The CRM Field Guide is an essential guide to Microsoft Dynamics CRM that everyone should have on their bookshelf. This book offers you details not only on CRM fundamentals and extensibility points but also the tried and true best practices and strategies of the combined experience of some of the most recognizable global experts in the CRM industry. The field guide contains insights from many CRM MVP contributors and is intended to be a book you pick up over and over again as you use CRM.

This book was announced at eXtremeCRM. While I haven’t read it yet myself, it appears to be a resource many of us have been hoping would come about.  Julie Yack has a blog post explaining the genesis of the book:

http://julieyack.blogs.com/my_weblog/2012/10/introducing-the-crm-field-guide-1029-days-in-the-making-crmfieldguide.html

I just bought the electronic copy.  I’m looking forward to reading it.

@devkeydet

CRM 2011, Visual Studio, and Source Control of non-code customizations

In the latest release of the Dynamics CRM 2011 SDK, a new tool was introduced which creates new options for how teams can source control their non-code customizations.  The tool is called SolutionPackager.  I encourage you to read the following SDK documentation:

Solution Tools for Team Development

As the documentation states:

“The tool identifies individual components in the compressed solution file and extracts them out to individual files. The tool can also re-create a solution file by packing the files that had been previously extracted. This enables multiple people to work independently on a single solution and extract their changes into a common location. Because each component in the solution file is broken into multiple files, it becomes possible to merge customizations without overwriting prior changes. A secondary use of the SolutionPackager tool is that it can be invoked from an automated build process to generate a compressed solution file from previously extracted component files without needing an active Microsoft Dynamics CRM server.”

I recommend you thoroughly read through the SDK documentation before continuing.  I will not explain SolutionPackager in great detail since the documentation does a good job of that already.

Since SolutionPackager is a cmd line based tool, it can be used to integrate with any source control system.  However, most Visual Studio developers probably want to integrate SolutionPackager into Visual Studio.  Furthermore, most probably want to integrate it with the Developer Toolkit for 2011.  The video below walks you through a sample I built which demonstrates how to do just that.  You can download my completed sample here.

@devkeydet

UPDATED SAMPLE: CRM 2011 Windows Store App OData helper

I worked with a partner, EastBanc Technologies, who used the approach I blogged about here and here to build a Windows Store App for a Dynamics CRM solution.  The folks at EastBanc Technologies found some bugs.  We worked together to fix the bugs and they were gracious enough to let me share their changes.  I’ve uploaded the updated sample here:

Visual Studio 2012 Source Code: http://sdrv.ms/S3MND1

CRM Unmanaged Solution: http://sdrv.ms/RYzVzj

You will need the WCF Data Services Tools for Windows Store Apps and Visual Studio 2012 RTM.

If you’ve followed the two previous posts, you’ll notice that I uploaded my samples to new location on my SkyDrive.  The reason is that due to the final terminology for Windows Store Apps, I needed to refactor the solution/project files and class names.  While I plan on updating the walkthrough post when I have a spare moment, I wanted to get the changes published.  Hopefully, the naming changes are obvious enough for folks until I get that post updated.

@devkeydet

Building VB.NET plugins with the developer toolkit

Scenario:

“I’ve seen the Developer Toolkit for Microsoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online, but it only ships with C# templates.  I’m a VB.NET developer.  How can I get the developer productivity from the toolkit while still writing my plugin code using VB.NET?”

The good news is that SDK obviously supports VB.NET.  The .NET Framework has always allowed you can make calls from C# code to VB.NET code through assembly reference.  Therefore, we can devise a strategy to just make the generated C# code from the toolkit the entry point, but put our plugin code in a referenced VB.NET assembly.

“But my plugin assemblies are sandboxed and database deployed which don’t support referencing other assemblies.”

True, but there’s this wonderful little tool called ILMerge (also available through NuGet).  With ILMerge and a little refactoring, we can achieve this scenario and VB.NET developers can use their language of choice with the developer toolkit.  Here’s a video with step by step instructions.  I use ReSharper in the video to make the refactoring easier.  Everyone who knows me knows I swear by ReSharper, but I understand not everyone loves it like I do or has the budget to acquire it.  I do my best to call out what you’d need to do manually, but you might have to pause now and then if you want to follow along step by step.

You can download the Visual Studio solution that is the output of the walkthrough here.  Reuse it or just grab the post build commands to get this setup yourself, etc.  Hope this helps anyone looking to get more productive with plugin development using VB.NET and the developer toolkit.

@devkeydet

Updated Developer Tips and Tricks Deck

I spend a lot of time talking to .NET developers who are looking at building business applications using Dynamics CRM.  They journey to understanding how to be a productive Dynamics CRM developer can sometimes be daunting.  There’s a bit to learn on top of your existing .NET / web dev skills.  If you go through any CRM developer training, including the Dynamics CRM 2011 Developer Training Kit, you usually won’t get to the level of productivity building solutions that a seasoned CRM developer has.  My Developer Tips and Tricks deck is aimed at helping people get there faster.  I just spent a good part of the day updating the deck.  It’s a living document where I modify as I learn new things.

@devkeydet

Moving an existing Hyper-V vhd to Windows Azure

I talked about building a CRM dev VM running on Windows Azure from scratch here.  However, you might have an existing VM that you’d prefer to run on Windows Azure instead.  Why?  Here are some reasons I can think of which may or may not apply to you:

  • You can take advantage of better hardware.
    • Even though I have a quad core 16 GB laptop, everything just feels like it runs faster in the Windows Azure VM.  Placebo effect?  Maybe.
  • You can access your dev box from just about anywhere with a Windows PC and an internet connection.
  • Windows Azure has better bandwidth than you might have.
    • Run a speed test to compare…make sure you pick a test server closest to your Azure data center for accurate comparison

I am sure there are others.  Feel free to share in the comments.  To get started, you are going to have to upload your .vhd file to your Windows Azure Storage account.  To do this follow the instructions starting at Step 5 from the Creating and Uploading a Virtual Hard Disk that Contains the Windows Server Operating System right until the Upload the VHD file section.  Use the following command line switches for csupload instead (make you have the June 2012 Azure SDK):

csupload Add-Disk –Destination “<full blob url you want for your vhd>” –Label “<whatever you want>” –LiteralPath “<path to your local .vhd>” –OS Windows

After the upload completes, you should be able to go into the management portal, create a new VM using this vhd file.

@devkeydet

Resizing an Azure VM vhd file

UPDATE (09SEP2013): There’s a faster way.  See: https://github.com/maartenba/WindowsAzureDiskResizer

I got into a bit of a predicament where the C drive of the VM I created here started to fill up.  I needed to install some software that only gave me the option to install to the C drive.  The installer was telling me there wasn’t enough disk space.  Here’s what I did to increase the size of the OS disk of my Azure VM:

  • Delete the VM
  • Download the .vhd
  • Resize the downloaded .vhd
  • Delete the original .vhd from blob storage
  • Upload the resized .vhd
  • Recreate the VM
  • Use diskpart to get access to the unallocated part of C

Delete the VM

image

Download the .vhd

Cloud Storage Studio is my favorite storage tool for Windows Azure.  If you’d prefer a free option, then my next favorite is Azure Storage Explorer.  Use one of these to download the .vhd file.  This part is going to require a local hard drive with lots of disk space.  You will need at least 2x the size of the original VHD + the size you want to grow it by.  A fast internet connection will be a big advantage here as well.

Resize the downloaded .vhd

You can use Hyper-V Manager to resize your .vhd file:

image

My challenge was that the PC with the disk space necessary to store the downloaded and expanded .vhd wasn’t Hyper-V capable.  Therefore, I used a tool called VHD Resizer from here.  There is a nice walkthrough of how to use it here.

Delete the original .vhd from blob storage

image

Upload the resized .vhd

You need to upload the resized .vhd back to blob storage.  I figured out how to upload it correctly after reviewing Step 5 from the Creating and Uploading a Virtual Hard Disk that Contains the Windows Server Operating System instructions.  However, the command line switches for csupload.exe need to look like this:

csupload Add-Disk –Destination “<full blob url you want for your vhd>” –Label “<whatever you want>” –LiteralPath “<path to resized .vhd>” –OS Windows

Recreate the VM

I created a new VM via the management portal and pointed it to the .vhd file I uploaded with csupload.  In my case, I had to re-attach an additional .vhd drive since my original .vhd was configured with an F drive where my database files were stored.  Once your VM boots up, log in using Remote Desktop.  For me, all of my SQL Server 2012 databases were in a recovering state.  Not sure why.  After I rebooted my server, everything was fine. 

Use diskpart to get access to the unallocated part of C

Your C drive will still report the old size in Windows Explorer.  You need to claim the additional unallocated space in the expanded drive.  To do this, open a command prompt and execute the following series of commands in italics:

diskpart

Next execute:

list disk

Make sure you select the os disk.  For me, it was disk 0:

select disk=0

Find the partition that needs extending:

list partition

Select the partition.  For me, it was partition 1:

select partition=1

Finally, extend the partition:

extend

Now, you have the additional space in the drive you need to install more stuff Smile.

@devkeydet

Preventing charges for an Azure VM when not in use

When you shutdown an Azure VM, you still get charged for usage.  That’s because the hardware for your VM is still allocated/dedicated to you.  It’s just not being used.  To prevent charges, you can delete the VM.  When you do that, your .vhd file(s) don’t’ get deleted.  Therefore, you can recreate the VM rather quickly when you want to start using it again.  While you can do this manually, it can become tedious depending on how often you need to do it.  The good news is that you can automate this process using PowerShell.  You’ll need to make sure you’ve run through Getting Started with Windows Azure PowerShell first.  Then you can follow the instructions from Importing and Exporting Virtual Machine Settings.  I keep this post handy since I create and delete my VM as needed so I am only charged for the time I am actually using it.

@devkeydet

CRM Solution Manager

Full disclosure, I received a free license from the authors of the tool.  However, I do believe it’s definitely worth the license fee.

I’ve been using the CRM Solution Manager add in for Visual Studio quite a bit lately.  I’ve grown fond of using it over the Developer Toolkit for web resource development.  The biggest reason is because I can right click a set of web resources, deploy AND publish in one click:

image

Another nice feature is that it translates folder structures into web resource names that follow the relative url naming recommendations here:

image

image

One more web resource feature that’s nice is that it allows you to configure JavaScript and CSS minification during the deploy/publish process:

image

I uncheck these during development so I can debug the code, but it’s a nice way to get everything minified for your production release.  While I still find the Developer Toolkit useful for plugins and workflow activities, the CRM Solution Manager has become my tool of choice for web resource development.  It also has other nice features that I haven’t dug into yet.  Check it out! 

@devkeydet