Creating URL string for call to OrganizationData.svc using JavaScript

心已入冬 提交于 2019-12-13 02:47:55

问题


Currently, I have two different methods to decide which version of CRM that my JS runs on. The addresses are similar but differ still, depending on whether it's on-line or on-premise. I've tried to integrate them but it's still depending on a variable as the code below shows. What would be a neater solution (one that will pick the right URL to the service for organization data)?

var isOnLine = true;
var organizationName = Xrm.Page.context.getOrgUniqueName();
var organizationUrl 
  = "http" + (isOnLine ? "s" : "") + "://" 
  + parent.window.location.host 
  + (isOnLine ? "" : "/" + organizationName)
  + "/XRMServices/2011/OrganizationData.svc";

Most of all, I'd like a method of obtaining the service's URL by a call to something (not sure what, though). If it's not possible, I'd like to be suggested a method of detecting if the currently run script is deployed in on-line or on-premise installation.

It's also of some importance that the method (if any available) will work independently of the version (or at the very least that it checks using if statement what version that is the currently run).


回答1:


  1. I wouldn't use parent.window.location.host (not even sure if it's supported).
  2. You get organizationName disregarding whether it's on-line version or on-premise (needlessly).
  3. The call will fail if an on-premise is set up on secured HTTP (or you'll need yet an other flag).
  4. My suggestion would be to focus on CRM 2011 (the older version 4 uses SOAP, not REST).
  5. For the actual OrganizationData service you can use the function below (or just embed the line).
function getOrganizationDataService(){
  return Xrm.Page.context.getServerUrl() 
    + "/XRMServices/2011/OrganizationData.svc";
}



回答2:


I would suggest having a look at this sample: Sample: Create, Retrieve, Update and Delete Using the REST Endpoint with JavaScript.

In particular:

sample_/Scripts/SDK.REST.js A reusable, generic library that simplifies asynchronous data operations using the REST Endpoint for Web resources.

In particular the first few functions which effectively build REST OrganizationData endpoint like so (I've cut out a couple of steps here, so make sure to look at the sample code):

Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/";


来源:https://stackoverflow.com/questions/13815756/creating-url-string-for-call-to-organizationdata-svc-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!