WebException (HTTP 401) thrown in 4.0 plug-ins
We have recently discovered a bug in the Microsoft Dynamics CRM 4.0 platform related to plug-ins. If your plug-in is throwing a System.Net.WebException exception and a 401 error (unauthorized) is being displayed to the user, read on to find out what may be happening and what you can do about it.
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.
PeterH
Answers
- There is a fix being worked on. The fix is not available at this time.
All Replies
You're a Hero! How did you find that out?
Had the same error. Hopefully ms deploys soon a rollup, it's really necessary!
- Great info Peter - is there a hotfix in the works for that?
- There is a fix being worked on. The fix is not available at this time.
I've just fallen foul of this - any fix available yet?
The quick one is using deployment manager to make your organization friendly name match the unique name so if you had "My Org" as the friendly and "MyOrg" as the unique change hte friendly to be "MyOrg" as well.Thanks David I've worked around it already using this technique - I was looking for something a little more "complete"
.
- I believe a hotfix is now available at http://support.microsoft.com/kb/948746

