Sharing record with teams

It is a fact that sharing in Dynamics CRM don’t scale well, but sometimes sharing still can be a preferred way of solving a problem. The main reason behind the scaling problem is that CRM have to check the PrincipleObjectAccess table for permission, and ( at least in earlier versions ) this table had a tendency to be large.

In a recent requirement we wanted to share some records with a team that contains 1000+ users, and had worries on how this would affect the POA table. We did som experiments, and here is the result:

Added one record A to entity B
As expected the PrincipalObjectAccess table did not grow.

Shared record A with one user
The PrincipalObjectAccess table grew with one row.

Sharerd record a with a team with 5 users
The PrinicpalObjectAcces table grew with one row. ( not 5, phew… )

Added a user to the team used for sharing
The PrincipalObjectAccess table did not grow.

Luckily only one row is added to the POA table when sharing with a team. The PrincipalTypeCode column also points to a different entity type.


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.

Retrieve data from OData using jQuery.getJSON and

Dynamics CRM 2011 exposes an OData service that can be used for easy retrieval of data from client side code. Here is an example on how you can use jQuerys $.getJSON to retrieve data, and $.map to map the data to a javascript object.

The goal of this example is to create a “phone-book” of all accounts in the system. This will be presented in a html webresource on a dashboard. It will not have any sorting, paging etc, but this is merely a simple example on how to retrieve data in a clean manner.

First you have to create a htm file and add a reference to jQuery. I have chosen the CDN hosted version in this example, but you could also upload it as a web reference and reference it from there.

<script type="text/javascript" src=""></script>
<script src="ClientGlobalContext.js.aspx"></script>

I have also added a reference to ClientGlobalContext.js.aspx. This is included as default in Dynamics CRM 2011 and gives you access a function called GetGlobalContext() that you in turn use, among other things, to get the server url for your CRM installation.

var context = GetGlobalContext(),
serverUrl = context.getServerUrl(),
odataEndpoint = "/XRMServices/2011/OrganizationData.svc";

function Account(data) { = data.Name;
this.telephone = data.Telephone1; = data.EMailAddress1;

In addition to setting necessary variables we also define a structure for Accounts that we are going to retrieve from CRM.
Now we are ready to retrieve the data:

var query = "/AccountSet?$select=*";
$.getJSON(serverUrl + odataEndpoint + query, function (data) {
// Run when data returns
var accounts = $.map(data.d.results, function (item) { return new Account(item); });

TIP: Use this tool ( OData Query Designer ) if you need help building your OData queries.

Note that we use $.map to iterate the result from the query. The alternative to this would be something like this:

var accounts = [];
for (var i = 0; i < data.d.results.length; i++) {
accounts.push(new Account(data.d.results[i]));

At my opinion $.map gives a much cleaner code. The use of the helper method $.getJSON also saves allot of code compared to for example using $.ajax directly.

To finish it off we must have some code to inject the result into the DOM:

function displayAccountList(accounts) {
$.each(accounts, function (index, account) {
$("#accountsTable").append("<tr><td>" + + "</td><td>" + + "</td><td>" + account.telephone + "</td></tr>");

And voila!

The same method for retrieving OData can just as well be used in a script on a form. I have only chosen a html webresource for simplicity.

You can download the a complete example html file and play with it here. Just upload is as a webresource and add it to the dashboard.

CDN hosted JavaScript in Dynamics CRM 2011

Dynamics CRM 2011 introduced a new concept called web resources. This makes life a lot easier for JavaScript developers and makes JavaScript customizations a lot more managable. But what if you want to use the CDN hosted version of a script instead of hosting it yourself, like for example JQuery? From what I can see there is no way to add a reference to a script like this in GUI. 

So, we have to look back at some lessons learned from writing JavaScript for Dynamics CRM4. The common way of centralizing JavaScript was to only use the form script to include a JavaScript from a folder on the server. A lot of pople have blogged about this over time. So, even if the problem with centralizing JavaScript is solved in 2011, we can still take adventage of this method for including externally hosted JavaScript. Here is an example on how to include JQuery from Google CDN in your JavaScript: 

	var scriptElement = document.createElement("<script type=text/javascript>");
	scriptElement.src = "";
	document.getElementsByTagName("head")[0].insertAdjacentElement("afterBegin", scriptElement);

There is one more thing before you are ready to use JQuery. My experience is that $(document).ready(function() {… simply don’t work because the JQuery library ($) isn’t ready when the script is first loaded. To work around this, you have to make your code execute after the forms onLoad event. 


Add this code to your script, and you should be able to use CDN hosted JQuery:

function Start() {
    // Verify that JQuery is loaded
    $(document).ready(function () {

You can also use this method to make your own CDN hosted JavaScripts, and include them in multiple CRM installations. This way you  don’t only get one place to change the script for one installation, but one place to change for multiple installations. This does not fit all solutions, but I’ve found several places where this is very useful!

I have attached a sample that you can download and try on your own. This sample hides all input fields on the form were it’s included as a “proof of concept”.