Last week I identified a need for an auto-incrementing numbering system and so I hunted around for something to use. At first, I used the old stand by of a basic UTC date conversion but it quickly became apparent that this was inadequate.
So, I came across a free auto-increment counter on this site which uses a plugin. Yay!
I installed the plugin on my development environment and it worked a treat. After some further testing, I then moved it to my Production environment which is where the problems started. No matter what I did, I kept getting a 401 Unauthorised Error. Round and round I went playing with IIS, changing permissions, re-installing the plugin, rebooting until I finally gave up and emailed the writers of the plugin.
Long story short, it seems that there is a bug within CRM 4.0 that causes the plugin to execute permissions using the Organization Friendly Name rather than the Organization Unique Name. Since, like most companies, our friendly name was just that, friendly, complete with spaces, I could see why the plugin was throwing an exception. The exact answer is reproduced below for you interest and also for future reference since this is rather a fundamental bug which could see you throw yourself out of the window in frustration.
Problem
The cause of the problem is that the wrong version of the organization name is being passed to the plug-in by the platform. The organization's friendly name is being used instead of the unique name. A friendly name can contain spaces while the unique name cannot. The platform expects an organization's unique name when doing authentication. The organization name is used internally by the CreateCrmService and CreateMetadataService methods of the IPluginExecutionContext class to authenticate the user in Microsoft Dynamics CRM. This problem only occurs if an organization name containing one or more blank spaces was specified when Microsoft Dynamics CRM 4.0 was installed.
Workaround
(Option 1) Use Deployment Manager to change the name of the organization to remove any blank spaces in the organization's display name. You must first disable the organization in Deployment Manager before you can edit the display name in the Edit Organization Wizard. An easy approach to guarantee using a correct organization name is to simply copy the value in the Name field of the wizard and paste it into the Display name field. After making the change in Deployment Manager wizard and clicking the Apply button, enable the organization and reset IIS. This option fixes the problem without requiring any plug-in code changes and does not change plug-in performance.
(Option 2) Do not use the CreateCrmService or CreateMetadataService methods in your plug-in to obtain an ICrmService or IMetadataService instance. Instead, create an instance of the Web service proxy using the CrmService or MetadataService class just as you would if you were calling Web services outside of a plug-in. When setting the CrmAuthenticationTokenValue property, remember to strip out any spaces in the organization name that you have obtained from the IPluginExecutionContext.OrganizationName property which is passed to the plug-in's Execute method. When a hotfix is made available for this bug, you should change your code back to using the CreateCrmService or CreateMetadataService methods for improved plug-in performance. This option will reduce the performance of your plug-in.
The following sample code is taken from the Walkthroughs\Plugin\CS\AccountCreate SDK sample and shows the affected lines of code.
// Create the task on the Microsoft CRM server.ICrmService service = context.CreateCrmService(true);
service.Create(followup);
Replace the above code with the following code to implement option 2 for the sample plug-in.
// Workaround to bug #33458.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = AuthenticationType.AD;
token.CallerId = context.UserId;
// Remove any blank spaces in the organization's name.
token.OrganizationName = context.OrganizationName.Replace(" ", "");
// Instantiate the CrmService Web service.
CrmService service = new CrmService();
service.UseDefaultCredentials = true;
service.CrmAuthenticationTokenValue = token;
// Obtain the service Url from the registry.
service.Url = (string)(Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\MSCRM").GetValue("ServerUrl"));
service.Url += "/2007/crmservice.asmx";
// Create the task on the Microsoft CRM server.
service.Create(followup);
You will also need to add a using statement (using Microsoft.Win32) to the sample plug-in.
This same technique applies to any plug-in that is affected by this particular bug.
Personally, I favour Option 1 which is to change the Organization Friendly Name to match the Organization Unique Name and hope that Microsoft bring out a hotfix shortly. This is exactly what I did and hey, it works. Option two is something to be aware of if you are developing your own plugin, but for the purposes of my exercise, option 1 would suffice.
The method for implementing Option 1 is the following :
1. Launch Deployment Manager on the Web server
2. Select the Organization, Right Click ->
Disable 3. RightClick Again and Click Edit Organization
4. Change the Organization Name and FriendlyName to be the same
5. Click Next and Finish.
You can view the Knowledge Base article on this issue HERE
Microsoft Resource Centre now available
Microsoft has published their resource centre online. Previously the resource centre was only available through Dynamics itself but now you can access it yourself directly from the web.
Online Resource Centre
Resource centre for on Premise and Service Provider editions