问题
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:
- I wouldn't use parent.window.location.host (not even sure if it's supported).
- You get organizationName disregarding whether it's on-line version or on-premise (needlessly).
- The call will fail if an on-premise is set up on secured HTTP (or you'll need yet an other flag).
- My suggestion would be to focus on CRM 2011 (the older version 4 uses SOAP, not REST).
- 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