Monthly Archives: June 2013

Dynamics CRM toolbox using scriptcs

A lot of time when working with Dynamics CRM you need to run some sort of code snippet. Even more if you running Dynamics CRM Online, and don`t has access to the database. Example of such tasks could be to reopen a finished task or cleanup old audit logs. Also, often you want to do a quick test on your code before deploying to plugins or some other environment.

filesSo, how do we solve this? Often you end up having a lot of console apps lying around, all of them with a complete solution structure and boilerplate code to set up a connection to CRM. In addition to these, I have a lot of console apps i only use for testing simple code snippets such as QueryExpressions.

So, what is scriptcs And how do scriptcs help us? scriptcs are a project based on the “Roslyn” compiler as a service that makes it easy to write and execute C#. This means that we can have a collection of  scripts, written in C#, without a complete Visual Studio project and solution. scriptcs also allow you to write scripts without namespace and class declaration.

Getting started with scriptcs is really easy. Just follow the “Getting scriptcs” part on and you are good to go.

To simplify the creation of Dynamics CRM scripts I have created a “script pack” that brings in the necessary references and boilerplate code to do authorization. This will provide you with an OrganizationService with only two lines of code. scriptcs let you install script packs from NuGet (as well as regular NuGet packages), and also provide a convenient method to import this into you script.

To install the ScriptCs.DynamicsCrm package, simply open a Console window and navigate to the folder where you want to place your scripts and type:

scriptcs -install ScriptCs.DynamicsCrm

This will bring down the references needed and put them in a bin folder. Now you are ready to write some Dynamics CRM scripts!

Create a new file with a *.csx extension in the same folder and edit it with your favorite text editor. If you want to test it, you can paste in the following code. Just replace the discovery server URL and organization name.

var crm = Require<DynamicsCrm>();
var orgService = crm.GetOrganizationService("http://crmserver/XrmServices/2011/Discovery.svc", "organization name");
var userResponse = (WhoAmIResponse)orgService.Execute(new WhoAmIRequest());
Console.WriteLine("You are userid: " + userResponse.UserId);

Then run your script by typing:

scriptcs yourscriptname.csx

You now should see something like this:

The example above will use your current credentials to access your CRM installation. If you want to use another domain user, you can specify this as well:

var orgService = crm.GetOrganizationService("http://crmserver/XrmServices/2011/Discovery.svc", "organization name", "username", "password", "domain");

Or, if you are using Dynamics CRM Online:

var orgService = crm.GetOrganizationService("http://crmserver/XrmServices/2011/Discovery.svc", "organization name", "username", "password");

The path to the discovery service and organization name can be found in Dynamics CRM under Customizations, Developer Resources. NOTE: I have not tested Dynamics CRM Online, but the code used for authorization is a slightly modified version of the one found in the SDK – so it should work. If not – please tell me!

All this enables us to organize our scripts, and put them in a centralized location ( Read: source control system ) for better reuse.

To get more info, and get more example scripts – go to the project github. You can also find the NuGet package here. If you create some useful script that can be valuable to others, I’m more than happy to add it to the “Examples” folder! Send me a mail, fork the project on Github or leave a comment here! The goal is to collect a nice collection scripts that can make the day better for all users of this package. In the future I`m also considering delivery of the samples folder with the NuGet package.

Please leave a comment if you find this useful, or have any suggestions for improvement.