Question

I need to make an AJAX request to some script from the onSave event of a form in MS CRM Dynamics 4.0. The code I have now is

var http_request;
// Prepare the xmlHttpObject and send the request.
try{
    http_request = new ActiveXObject("Msxm12.XMLHTTP");
}catch(e){
    try{
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(e){
        alert("Something went wrong..");
    }
}
var poststr = "foo=bar";
http_request.open("POST", "/folder/index.html", false);
http_request.setRequestHeader("Content-Type","text/xml; charset=utf-8"); 
http_request.send(escape(poststr));
// Capture the result.
var resultXml = http_request.responseText;
alert(resultXml);

The alert now gives me the content of a 404-type error. I'm sure the page is there, it's available through the browser. If I change the
http_request.open("POST", "/folder/index.html", false);
to
http_request.open("POST", "localhost:5555/folder/index.html", false);
the open() fails, saying "Permission denied".

UPDATE (7 dec 2009);

I've created a virtual directory in the ISV folder of CRM and uploated an ASP.NET application. Now, if I go to crm.url.nl:5555/ISV/Default.aspx I get;

'Microsoft.Crm.WebServices.Crm2007.CookieAndSoapHeaderAuthenticationProvider, Microsoft.Crm.WebServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' doesn't exist. Parameter name: Microsoft.Crm.WebServices.Crm2007.CookieAndSoapHeaderAuthenticationProvider, Microsoft.Crm.WebServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

With stacktrace;

[ArgumentException: 'Microsoft.Crm.WebServices.Crm2007.CookieAndSoapHeaderAuthenticationProvider, Microsoft.Crm.WebServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' doesn't exist.
Parameter name: Microsoft.Crm.WebServices.Crm2007.CookieAndSoapHeaderAuthenticationProvider, Microsoft.Crm.WebServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]
   Microsoft.Crm.Authentication.BaseAuthenticationSettings.CreateType(String typeName, Type requiredBaseType) +265
   Microsoft.Crm.Authentication.BaseAuthenticationSettings.CreateProvider(String typeName, IDictionary`2 configuration) +28
   Microsoft.Crm.Authentication.AuthenticationPipelineSettings.LoadPipeline() +262
   Microsoft.Crm.Authentication.AuthenticationPipelineSettings.get_AuthenticationProvider() +16
   Microsoft.Crm.Authentication.AuthenticationEngine.Execute(Object sender, EventArgs e) +524
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +68
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

Does anyone have any idea? The .NET application only writes a single word to Response, so there's nothing special there...

Was it helpful?

Solution

Okay, I found out what the idea is. If you want to do an AJAX call to a dynamic .NET application from CRM 4.0, here's what you do.

Put the assemblies of your .NET application in the CRMWeb/bin folder in the CRM folder. Put you aspx files in a folder in the ISV folder. I used the stunnware.com folder, because it was there, but you may want to create another folder for the sake of tidyness. Then, in the onSave (or any on- event) put something like this;

var xmlHttp = null;
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

var getstr = "foo=bar&foo2=bar2";
var url = prependOrgName("/ISV/*YOURFOLDER*/Default.aspx?"+getstr);

xmlHttp.open("GET",url,true);
xmlHttp.send(null);

Of course, if you want CRM to do anything with the response of the xml-request you can use the onreadystatechange for that.

Hope this may help other people. It may seem straightforward, but it took me quite a while before I figured out how to do it (although I knew how to use AJAX and .NET and stuff). I think it's a shame that the CRM developers are left out in the cold by Microsoft. They should really put some more time in documenting the SDK and how to do things like this.

OTHER TIPS

Just thinking of things to try here:

The path to your file is within the CRMWeb directory? Do you have a virtual directory set up or anything?

Does the /folder/index.html work in an IFRAME on the same form?

Maybe try something you know will work just to make sure: http://stackoverflow.com or http://google.com.

Also note that MS recommends putting all customizations in the /ISV/ folder. It shouldn't cause 404 errors, but I'm not sure if they'll consider this unsupported.

Are you really calling a html extension? I believe you need to use prependOrgName (see SDK) for aspx and asmx extensions. I'm not sure if it would cause a 404 or just use your default organization off hand though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top