Most of us would have faced the problem of "Access Denied" or "Transport Error" while accessing cross domain calls from CRM.
I have a small example here with a sample WCF service along with JSONP Query below to fetch the data from Cross Domain Service
Create a Sample WCF Application by selecting the "WCF Service Application" template from Projects as shown below.
We are just creating a sample method which takes bookid as parameter and returns the same. Use the following code to create an interface and class method.
///Interface which has the method GetBookByID
namespace WcfRestSample
{[ServiceContract]
[System.Web.Script.Services.ScriptService]
public interface IBookService
{
[OperationContract]
[WebInvoke(Method="GET",ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped,UriTemplate = "Book/{id}")]
Book
GetBookById(string id);
}}
If you observe the attributes "WebMessageFormat.Json" specifies that the response format from the service shall be JSON and we also provided the rest format like "Book/{id}" to call the service.
We have a class implementing the service which provides the implementation of the method
If you observe keenly there are two major things you need to consider , one to enable the service as RestBased Service you need to set Binding as "webHttpBinding" and the other crossDomainScriptAccessEnabled="true" , this makes the service accessible for cross domain request as well.
Now your service is ready. Let us see how client calls the same using $.getJSON and the other way by using original javascript to read a JSON request. No difference if called in Dynamics CRM or through a normal ASP.Net page
There are two ways to consume the service from Javascript using JSONP
Method 1: $.getJSON to read a JSONP request
Method 2 original JavaScript to read a JSONP request
Refer to the following link in MSDN for more details





No comments:
Post a Comment