{"id":1796,"date":"2015-10-05T18:08:09","date_gmt":"2015-10-05T12:38:09","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=1796"},"modified":"2015-10-05T18:08:09","modified_gmt":"2015-10-05T12:38:09","slug":"make-cross-domain-web-service-request-through-client-side-scripting-in-dynamics-crm","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2015\/10\/make-cross-domain-web-service-request-through-client-side-scripting-in-dynamics-crm\/","title":{"rendered":"Make Cross Domain Web Service Request through Client Side Scripting in Dynamics CRM"},"content":{"rendered":"<p>To perform CRUD operations with CRM data at client side we use ODATA and SOAP services of Microsoft Dynamics CRM. We use XMLHttpRequest to send request to these services to get the response.<\/p>\n<p>Microsoft Dynamics CRM provides below WCF services that we used to manipulate CRM data,<\/p>\n<p>Organization Data Service \u2013 Protocol:\u00a0 OData (REST)<\/p>\n<p>http:\/\/&lt;server&gt;\/XRMServices\/2011\/OrganizationData.svc\/<\/p>\n<p>Organization Service &#8211; Protocol: SOAP<\/p>\n<p>http:\/\/&lt;server&gt; \/XRMServices\/2011\/Organization.svc<\/p>\n<p>The request to above WCF services from JavaScript code works because these services are in same domain as the CRM server.<\/p>\n<p>But sometimes we may need to create our own service which accepts data from CRM and sends response back to it at client side and which is hosted on different domain than CRM domain. If we try to call such web service we get an error as <em>\u201c<\/em><em>No &#8216;Access-Control-Allow-Origin&#8217; header is present on the requested resource<\/em><em>\u201d<\/em>.<\/p>\n<p>This blog will illustrate how to get the response correctly from the WCF service that are hosted on different domain than the domain of MS Dynamics CRM.<\/p>\n<p>For example, at client side using scripting we need to send name of City of account record to WCF service (.svc) (which is hosted on different domain than CRM) to get respective code for that city.<\/p>\n<p>The request to service which is not in same domain as of CRM it\u2019s called Cross Domain access\/request.<\/p>\n<p>If we do XMLHttpRequest to different domain than our CRM or Page is on, the browser blocks this request for security reasons.<\/p>\n<p>There are ways to rid of this situation. The JSONP method is use to make cross domain request. We can send CROSS DOMAIN AJAX request using JSONP.<\/p>\n<p>Below is the code snippet that sends cross domain AJAX request,<\/p>\n<p>$.ajax({<\/p>\n<p>url: &#8220;http:\/\/ad4:51643\/WCFService\/Services\/ProcessData.svc\/GetCode&#8221;,<\/p>\n<p>dataType: &#8220;jsonp&#8221;, \/\/set data type as jsonp for cross domain request<\/p>\n<p>contentType: &#8220;application\/json; charset=utf-8&#8221;,<\/p>\n<p>data: &#8220;city=Florida&#8221;, \/\/pass value for city parameter<\/p>\n<p>success: function (data) {<\/p>\n<p>alert(&#8220;Name:&#8221; + data);<\/p>\n<p>},<\/p>\n<p>error: function (data) {<\/p>\n<p>if (data.responseText != &#8220;done&#8221;) {<\/p>\n<p>alert(&#8220;ERROR: &#8221; + data.responseText);<\/p>\n<p>}<\/p>\n<p>else {<\/p>\n<p>alert(_msgSendRequest);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p>Below is the code snippet of WCF service (.svc) function that accept string city and send response in JSON format,<\/p>\n<p>\/\/Data Contract part of WCF service<\/p>\n[DataContract]\n<p>public class Company<\/p>\n<p>{<\/p>\n[DataMember]\n<p>public string CityCode;<\/p>\n<p>}<\/p>\n<p>\/\/Service Contract and OperationContract part of WCF service<\/p>\n[ServiceContract(Namespace = &#8220;JsonpAjaxService&#8221;)]\n[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]\n<p>public class ProcessData<\/p>\n<p>{<\/p>\n<p>\/\/ To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)<\/p>\n<p>\/\/ To create an operation that returns XML,<\/p>\n<p>\/\/\u00a0\u00a0\u00a0\u00a0 add [WebGet(ResponseFormat=WebMessageFormat.Xml)],<\/p>\n<p>\/\/\u00a0\u00a0\u00a0\u00a0 and include the following line in the operation body:<\/p>\n<p>\/\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 WebOperationContext.Current.OutgoingResponse.ContentType = &#8220;text\/xml&#8221;;<\/p>\n[OperationContract]\n[WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]\n<p>&nbsp;<\/p>\n<p>public void GetCode(string callback, string city)<\/p>\n<p>{<\/p>\n<p>Dictionary&lt;string, string&gt; cityCodes = new Dictionary&lt;string, string&gt;();<\/p>\n<p>cityCodes.Add(&#8220;Florida&#8221;, &#8220;FL&#8221;);<\/p>\n<p>cityCodes.Add(&#8220;New York&#8221;, &#8220;NY&#8221;);<\/p>\n<p>StringBuilder sb = new StringBuilder();<\/p>\n<p>System.Web.Script.Serialization.JavaScriptSerializer s = new System.Web.Script.Serialization.JavaScriptSerializer();<\/p>\n<p>Company p = new Company() { CityCode = cityCodes[city] };<\/p>\n<p>string json = s.Serialize(p);<\/p>\n<p>sb.Append(callback + &#8220;(&#8220;);<\/p>\n<p>sb.Append(json);<\/p>\n<p>sb.Append(&#8220;);&#8221;);<\/p>\n<p>System.Web.HttpContext.Current.Response.Clear();<\/p>\n<p>System.Web.HttpContext.Current.Response.ContentType = &#8220;application\/json&#8221;;<\/p>\n<p>System.Web.HttpContext.Current.Response.Write(sb.ToString());<\/p>\n<p>}<\/p>\n<p>\/\/ Add more operations here and mark them with [OperationContract]\n<p>}<\/p>\n<p>\/\/web.config file of WCF service<\/p>\n<p>&lt;configuration&gt;<\/p>\n<p>&lt;system.serviceModel&gt;<\/p>\n<p>&lt;behaviors&gt;<\/p>\n<p>&lt;serviceBehaviors&gt;<\/p>\n<p>&lt;behavior name=&#8221;ProcessDataAspNetAjaxBehavior&#8221; &gt;<\/p>\n<p>&lt;!&#8211; Add the following element to your service behavior configuration. &#8211;&gt;<\/p>\n<p>&lt;serviceMetadata httpGetEnabled=&#8221;true&#8221; \/&gt;<\/p>\n<p>&lt;\/behavior&gt;<\/p>\n<p>&lt;\/serviceBehaviors&gt;<\/p>\n<p>&lt;endpointBehaviors&gt;<\/p>\n<p>&lt;behavior name=&#8221;ProcessDataAspNetAjaxBehavior&#8221;&gt;<\/p>\n<p>&lt;webHttp \/&gt;<\/p>\n<p>&lt;\/behavior&gt;<\/p>\n<p>&lt;\/endpointBehaviors&gt;<\/p>\n<p>&lt;\/behaviors&gt;<\/p>\n<p>&lt;serviceHostingEnvironment aspNetCompatibilityEnabled=&#8221;true&#8221;<\/p>\n<p>multipleSiteBindingsEnabled=&#8221;true&#8221; \/&gt;<\/p>\n<p>&lt;standardEndpoints&gt;<\/p>\n<p>&lt;webScriptEndpoint&gt;<\/p>\n<p>&lt;standardEndpoint crossDomainScriptAccessEnabled=&#8221;true&#8221;&gt;&lt;\/standardEndpoint&gt;<\/p>\n<p>&lt;\/webScriptEndpoint&gt;<\/p>\n<p>&lt;\/standardEndpoints&gt;<\/p>\n<p>&lt;services&gt;<\/p>\n<p>&lt;service name=&#8221;ProcessData&#8221; behaviorConfiguration=&#8221;ProcessDataAspNetAjaxBehavior&#8221;&gt;<\/p>\n<p>&lt;endpoint address=&#8221;&#8221; behaviorConfiguration=&#8221;ProcessDataAspNetAjaxBehavior&#8221;<\/p>\n<p>binding=&#8221;webHttpBinding&#8221; contract=&#8221;ProcessData&#8221; \/&gt;<\/p>\n<p>&lt;\/service&gt;<\/p>\n<p>&lt;\/services&gt;<\/p>\n<p>&lt;\/system.serviceModel&gt;&lt;\/configuration&gt;<\/p>\n<p>Important points to make a successful cross domain request,<\/p>\n<ul>\n<li>In Ajax request set dataType as \u201cJSONP\u201d<\/li>\n<li>Pass required parameter to web service<\/li>\n<li>In WCF service (.cs) file add \u201dScriptMethod\u201d web attribute as above<\/li>\n<li>Along with all required parameter add extra string parameter named as \u201ccallback\u201d, in above code the \u201cGetCode\u201d function accepts \u201ccallback\u201d and \u201ccity\u201d as parameter. We do not need pass value for callback parameter. When our function defined in \u201cOperationContract\u201d has parameter named as \u201ccallback\u201d internally JQuery pass value for it.<\/li>\n<li>Append the result with callback function and write result in HttpContext.Current.Response. The string that we write to response should be in given\u00a0format, callback(\u201c{CityCode:\u201dFL\u201d}\u201d);<\/li>\n<\/ul>\n<p>So this way we can call cross domain request and we get the response in success callback.<\/p>\n<p>Hope this helps!<\/p>\n<p><span style=\"color: #993300;\">Make your Dynamics CRM more better with Inogic <a style=\"color: #993300;\" href=\"http:\/\/bit.ly\/1KD7s8e\" target=\"_blank\" rel=\"noopener noreferrer\">Add-ons<\/a> for Dynamics CRM.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To perform CRUD operations with CRM data at client side we use ODATA and SOAP services of Microsoft Dynamics CRM. We use XMLHttpRequest to send request to these services to get the response. Microsoft Dynamics CRM provides below WCF services that we used to manipulate CRM data, Organization Data Service \u2013 Protocol:\u00a0 OData (REST) http:\/\/&lt;server&gt;\/XRMServices\/2011\/OrganizationData.svc\/\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2015\/10\/make-cross-domain-web-service-request-through-client-side-scripting-in-dynamics-crm\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15,19,33,66],"tags":[433,434,1812],"class_list":["post-1796","post","type-post","status-publish","format-standard","hentry","category-development","category-dynamics-crm","category-javascript","category-web-resources-2","tag-cross-domain-web-service-request","tag-cross-origin-ajax-request","tag-wcf-service"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/1796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=1796"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/1796\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=1796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=1796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=1796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}