Javascript-从MS Dynamics CRM Online添加和检索数据

Javascript - Add and retrieve data from MS Dynamics CRM Online

本文关键字:添加 检索 数据 Online CRM MS Dynamics Javascript-      更新时间:2023-09-26

我想使用纯Javascript从MS Dynamics CRM Online 2011添加/检索数据。我搜索了一整天,但只能用DynamicsSDK、C#、VB或JScript来完成。

在纯javascript中有什么方法可以做到这一点吗?我只需要找到一个web服务来发送/获取数据,但找不到。有这样的web服务或api吗???请帮帮我,我完全糊涂了!!!谢谢

如果其他人提到需要,您可以使用SOAP进行连接(可能不推荐,但您想知道)。如果您不知道如何使用SOAP/JavaScript,那么我建议您阅读以下内容:最简单的SOAP示例

若要使用Office 365连接到CRM Online(所有Windows Live帐户都迁移到Office 365),则您需要获取安全令牌,然后将其用于您的请求。

以下是对PHP中的令牌的请求,您应该能够为JavaScript重构这些令牌。

$TokenSOAP = '<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
                              <s:Header>
                                <a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action>
                                <a:MessageID>urn:uuid:%s</a:MessageID>
                                <a:ReplyTo>
                                  <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
                                </a:ReplyTo>
                                <a:To s:mustUnderstand="1">%s</a:To>
                                <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                                  <u:Timestamp u:Id="_0">
                                    <u:Created>%sZ</u:Created>
                                    <u:Expires>%sZ</u:Expires>
                                  </u:Timestamp>
                                  <o:UsernameToken u:Id="uuid-cdb639e6-f9b0-4c01-b454-0fe244de73af-1">
                                    <o:Username>%s</o:Username>
                                    <o:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">%s</o:Password>
                                  </o:UsernameToken>
                                </o:Security>
                              </s:Header>
                              <s:Body>
                                <t:RequestSecurityToken xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust">
                                  <wsp:AppliesTo xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
                                    <a:EndpointReference>
                                      <a:Address>urn:crmapac:dynamics.com</a:Address>
                                    </a:EndpointReference>
                                  </wsp:AppliesTo>
                                  <t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType>
                                </t:RequestSecurityToken>
                              </s:Body>
                            </s:Envelope>';
        $TokenSOAP = sprintf($TokenSOAP, self::gen_uuid(), 'https://login.microsoftonline.com/RST2.srf',  self::getCurrentTime(), self::getNextDayTime(), $username, $password);

根据您的crm区域,根据需要更改端点参考地址

这将返回两个安全令牌和一个密钥标识符。

然后您需要进行添加和检索。如果你在谷歌上搜索"CRM 2011 SOAP"和以下四个函数"创建"、"更新"、"检索"answers"检索多个",你应该会得到很多SOAP示例,例如:

http://www.mscrmconsultant.com/2012/07/create-update-delete-record-using.html

与Javascript和CRM交互的两种方式是通过OData或SOAP服务。如果可能的话,最简单的方法是使用OData。

您将遇到的问题是使用Odata进行身份验证。CRM javascript和Silverlight之外不支持OData访问。(虽然我使用LinqPad创建我的Odata查询,它用它查询CRM也很好,所以我不确定这是怎么回事)

解决这一问题的常见方法是编写自己的Web服务,向CRM进行身份验证,然后使用SDK检索和更新数据,以一种安静的方式将其公开给您。

正如Guido所指出的,您应该能够使用来自Java的SOAP请求,但我也只在CRM中这样做过,我不确定您还会面临什么身份验证问题。