从MS CRM事件发出AJAX请求

时间:2009-12-01 13:10:08

标签: dynamics-crm dynamics-crm-4

我需要从MS CRM Dynamics 4.0中的表单的onSave事件向某些脚本发出AJAX请求。 我现在的代码是

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);

警报现在为我提供了404类型错误的内容。我确定页面在那里,可以通过浏览器访问。 如果我改变了 http_request.open("POST", "/folder/index.html", false);

http_request.open("POST", "localhost:5555/folder/index.html", false);
open()失败,说“权限被拒绝”。

更新(2009年12月7日);

我在CRM的ISV文件夹中创建了一个虚拟目录,并上传了一个ASP.NET应用程序。现在,如果我去crm.url.nl:5555/ISV/Default.aspx我得到;

'Microsoft.Crm.WebServices.Crm2007.CookieAndSoapHeaderAuthenticationProvider,Microsoft.Crm.WebServices,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'不存在。 参数名称:Microsoft.Crm.WebServices.Crm2007.CookieAndSoapHeaderAuthenticationProvider,Microsoft.Crm.WebServices,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35

使用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

有没有人有任何想法? .NET应用程序只将一个单词写入Response,因此没有什么特别的......

2 个答案:

答案 0 :(得分:1)

好的,我发现了这个想法。 如果您想从CRM 4.0对动态.NET应用程序进行AJAX调用,这就是您的工作。

将.NET应用程序的程序集放在CRM文件夹中的CRMWeb / bin文件夹中。将您的aspx文件放在ISV文件夹中的文件夹中。我使用了stunnware.com文件夹,因为它在那里,但你可能想要为了整洁而创建另一个文件夹。 然后,在onSave(或任何on-event)中放置这样的东西;

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);

当然,如果您希望CRM对xml-request的响应做任何事情,您可以使用onreadystatechange。

希望这可以帮助其他人。这看起来很简单,但在我弄清楚如何做之前我花了很长时间(虽然我知道如何使用AJAX和.NET等等)。我认为CRM开发人员被微软冷落了是一种耻辱。他们应该花更多的时间来记录SDK以及如何做这样的事情。

答案 1 :(得分:0)

想到要尝试的事情:

您的文件路径是否在CRMWeb目录中?您是否设置了虚拟目录?

/folder/index.html是否在同一表单上的IFRAME中工作?

也许尝试一些您知道的工作只是为了确保:http://stackoverflow.comhttp://google.com

另请注意,MS建议将所有自定义项放在/ ISV /文件夹中。它不应该导致404错误,但我不确定他们是否会认为这不受支持。

你真的在调用html扩展吗?我相信你需要为aspx和asmx扩展使用prependOrgName(参见SDK)。我不确定它是否会导致404或仅仅使用您的默认组织。