Showing posts with label web service. Show all posts
Showing posts with label web service. Show all posts

Sep 24, 2012

Using CRM Webservice from custom web page (CRM 2011)

One of the previous posts we illustrated the way of using CRM service in custom page for CRM 4.0. Check it here.

Now we will see how the same task is accomplished in CRM 2011

If we put it in a method;

public static IOrganizationService getService()
{
ClientCredentials _cred = new ClientCredentials();

_cred.Windows.ClientCredential = new NetworkCredential("CRM_ADMIN_01", "F1xit@OR5", "CRMBIZDOMAIN");
string _url = "http://crmbiz:5555/abcltd/XRMServices/2011/Organization.svc";

OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(new Uri(_url), null, _cred, null);

return (IOrganizationService)serviceProxy;
}

Now the calling part;

Aug 21, 2012

Using CRM Webservice from custom web page (CRM 4.0)

Suppose we need to do any operation in CRM through a custom webpage. Now we need to use CRM web service in the most standard way. Below is the way we can instantiate and authenticate the service which could be used in our page.

If we put it in a method;

public static CrmService getCrmWebService(string _serverName, string _orgName)
        {

         CrmAuthenticationToken token = new CrmAuthenticationToken();
         token.AuthenticationType = 0;
         token.OrganizationName = _orgName;

         CrmService _service = new CrmService();
         _service.Url = "http://" + _serverName + "/mscrmservices/2007/crmservice.asmx";
         _service.CrmAuthenticationTokenValue = token;

         NetworkCredential _cred = new NetworkCredential();
         _cred.Domain = "CRMBIZDOMAIN";
         _cred.UserName = "CRM_ADMIN_01";
         _cred.Password = "F1xit@OR5";
         _service.Credentials = _cred;

         return _service;
        }

Calling part;

CrmService _CrmService = CrmCore.getCrmWebService("corp-biz01:5555", "AxCRM01");

Now any operation is possible.

 
To check how to do the same thing in CRM 2011, click here.

Jul 2, 2012

Calling WCF service within a plug-in

Calling a third party service through plug-in could be an important aspect of a CRM development. I of course had a difficult time to figure out this. At the end of the day below sequence worked for me.

1)  Adding the service to Plug-in code
This step was as same as calling a service from general web application. Add the service URL and you will see it added to the Service Reference section.


Nor difference in calling part also. Something like below...

_service = new Rate();
_service.ChannelFactory.Credentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;
 
_service.ChannelFactory.Credentials.Windows.ClientCredential.Domain = "<Domain>";
_service.ChannelFactory.Credentials.Windows.ClientCredential.UserName = "<User Name>";
_service.ChannelFactory.Credentials.Windows.ClientCredential.Password = "<Password>";

selltingRate = _service.ReturnSelltingRate(_prodId);

2) Defining the end points

Here is the tricky part. Now you got to define the endpoint of the service in the config of the CRM. You will find web.config of the CRM in C:\Program Files\Microsoft Dynamics CRM\CRMWeb. (Please make sure you get back up before modifying this file, since if it’s modified wrong, CRM will not load at all!)

Now go back to the Visual Studio project and open the app.config file of the same plug-in project. Now grab the endpoint of your service and relevant binding tag. You can select the right endpoint by searching by URL of the service. Then get the “name” attribute of particular tag and search for the Binding tag of same name.

Now paste them in the web.config file of the CRM. Under <system.serviceModel> tag you will see two sections called “client” and “bindings” Endpoint should go under client section. Binding tag should go under bindings section, within the tag either <basicHttpBinding> or <wsHttpBinding>, depending on binding type. Identifying the binding type is not hard since name attribute of binding tag contains the type.

You will find something like this in the web.config of the CRM, if you do it correctly.

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IRateService" closeTimeout="00:01:00"
                        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                        allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                        maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                        messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                        useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
          maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="Transport">
        <transport clientCredentialType="Windows" proxyCredentialType="None"
            realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="https://xxxxxxxxxxxxxxxxxx/webservices/xxxxxxxxxxxxxxx/Rate.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IRateService"
            contract="BusinessServiceClient.IRateService" name="BasicHttpBinding_IRateService" />
</client>
</system.serviceModel>  

Click this to read the previous articles explained how to call a WCF from a client side. Please note some services will work fine for plug-ins but not client side due to security reasons. Only solution for that is to create your own WCF service to consume the third party service and then use your service to read values to CRM forms.

Feb 8, 2012

Consuming a WCF from JavaScript

This is just to give simple steps to call a WCF from client side.

1) Browse the web service URL. Then you know your web service is available. You will see a page like below one.


2) Now click relevant link in the page. Search for SOAPAction tag relevant to method you wish to use, So that you know the SOAPAction value.


3) Now get the request SOAP envelops.

a) If you have the code of WCF service run the code in Visial Studio that results a pop up of WCF Test Client. Then double click the method you need and pass some values to parameters shown in Formatted tab. Then click the XML tab to grab the Request SOAP envelop. Just omit the Header Tag from the XML you get here.


b) If the web service is provided by some other party, you can simply request the soap envelops.

4) Now we got everything we need, call the relevant method of the Web Service through below code. xmlHttp.ResponseXML will return the result.

        xmlhttp = new XMLHttpRequest();
        xmlhttp.open('POST', 'http://localhost:5555/ISV/PricingWCF/WcfIntermediateService.Service1.svc', false);
        xmlhttp.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
        xmlhttp.setRequestHeader('SOAPAction', 'http://tempuri.org/IService1/GetSalesRate');

        var data = '';
        data += '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">';
        data += '<s:Body>';
        data += '<GetSalesRate xmlns="http://tempuri.org/">';
        data += '<productcode>AEC0023678</productcode>';
        data += '</GetSalesRate>';
        data += '</s:Body>';
        data += '</s:Envelope>';

        xmlhttp.send(data);

Click this to see how you call WCF service from plug-in.