Consume .NET Webservice (DotNet Data Type)

 

IF ISNULL(DotNetXmlDoc) THEN
DotNetXmlDoc := DotNetXmlDoc.DOMDocumentClass;

IF ISNULL(DotNetXmlHttp) THEN
DotNetXmlHttp := DotNetXmlHttp.XMLHTTPRequestClass;

DotNetXmlHttp.open(‘POST’,’URL’,0,0,0);

DotNetXmlHttp.SetRequestHeader(‘Host’,’ServerName);

DotNetXmlHttp.SetRequestHeader(‘Content-Type’,’text/xml; charset=utf-8′);

DotNetXmlHttp.SetRequestHeader(‘Content-Length’,’Length’);

DotNetXmlHttp.SetRequestHeader(‘SOAPAction’,’http://tempuri.org/MethodName);

DotNetXmlHttp.Send(‘<?xml version=”1.0″ encoding=”utf-8″?>’ +
‘<soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns:xsd=”http://www.w3.org/2001/XMLSchema&#8221; xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”>&#8217; +
‘<soap:Body>’ +
‘<IsCustomerExist xmlns=”http://tempuri.org/”>&#8217; +
‘<customerID>’+CustomerID+'</customerID>’ +
‘</IsCustomerExist>’ +
‘</soap:Body>’ +
‘</soap:Envelope>’);

DotNetXmlDoc.load(DotNetXmlHttp.responseBody);

IF DotNetXmlHttp.status = 200 THEN BEGIN

DotNetXmlNodeList := DotNetXmlDoc.getElementsByTagName(‘soap:Body’);

DotNetXmlNode := DotNetXmlNodeList.item(0);

EVALUATE(Output,UPPERCASE(DotNetXmlNode.text));

END;

DotNetXmlHttp DotNet MSXML.XMLHTTPRequestClass.’Microsoft.MSXML, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’

DotNetXmlDoc DotNet MSXML.DOMDocumentClass.’Microsoft.MSXML, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’

DotNetXmlNodeList DotNet MSXML.IXMLDOMNodeList.’Microsoft.MSXML, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’

DotNetXmlNode DotNet MSXML.IXMLDOMNode.’Microsoft.MSXML, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’

Advertisements

13 comments on “Consume .NET Webservice (DotNet Data Type)

  1. sir can i know how you created response/ request in Send Method .i do want to try but i don’t know How to Create for My codeunit(custom)..

  2. Hey Raja,

    Send method represents the Web-service which you are consuming. You need to open the URL mentioned here – DotNetXmlHttp.open(‘POST’,’URL’,0,0,0) . Once you will open you will find the exact syntax to write.

    Is it a .NET exposed web service which you are trying to consume??

    Many Thanks

  3. Hey Ami,

    First of all you should check what response are you getting.
    You should get some response like 200 400 500.
    200 means successful, 400 means bad request and so on….

    Check what you are getting at this point Messgae(‘%1’,DotNetXmlHttp.status);

    If it’s returning blank with no value that means check the URL – Open the URL in Firefox and check if you can input the parameters there and its in a working mode.
    If there is any credential to login the URL then you need to enter that as well in DotNetXmlHttp.open.OPEN command above instead of last two 0.

    P.S – Once you start getting the responses as 200 400 500 then you can go ahed.

    Many Thanks

  4. Pingback: Dynamics NAV: How To Consume An External SOAP Web Service With .Net/Dotnet Interop | NAVfreak

  5. Hi,

    I am using the same code with changes in the required value but I am getting response of 400.

    2. The second problem is that it is giving error when i tried to take output by Commenting the status = 200 Line –

    Microsoft Dynamics NAV
    —————————

    A DotNet variable has not been instantiated. Attempting to call MSXML.IXMLDOMNode.text in CodeUnit : OnRun
    —————————
    OK
    —————————

    Please help me out in this.

    • Vivek,

      If you are getting response as 400 means your code is even unable to hit the service, make sure that the POST URL is correct which you are trying to connect with proper SOAP action. Open the URL directly in browser and cross verify that it’s working there and if its within domain then you need to check within domain.

      A DotNet variable has not been instantiated. Attempting to call MSXML.IXMLDOMNode.text in CodeUnit : OnRun

      Make sure you are instantiating it with proper connector and cross verify that the RunOnClient property of the DotNet Variable is set to YES.

  6. Hi,

    Thanks for the reply….
    When I am running through the browser url its working fine.

    I am writing down my code for you Please look into this what I have done wrong-

    IF ISNULL(DotNetXmlDoc) THEN
    DotNetXmlDoc := DotNetXmlDoc.DOMDocumentClass;

    IF ISNULL(DotNetXmlHttp) THEN
    DotNetXmlHttp := DotNetXmlHttp.XMLHTTPRequestClass;

    DotNetXmlHttp.open(‘POST’,’localhost/MyWebservice.asmx’,0,0,0);
    DotNetXmlHttp.setRequestHeader(‘Host’,’localhost’);
    DotNetXmlHttp.setRequestHeader(‘Content-Type’,’text/xml; charset=utf-8′);
    DotNetXmlHttp.setRequestHeader(‘Content-Length’,’Length’);
    DotNetXmlHttp.setRequestHeader(‘SOAPAction’,’tempuri.org/mymethodname’);

    DotNetXmlHttp.send(”
    +”
    ”+
    ” +
    ”+”CustNo.”+”+
    ”+FORMAT(Amount)+”+
    ”+
    ”);
    DotNetXmlDoc.load(DotNetXmlHttp.responseBody);
    IF DotNetXmlHttp.status = 200 THEN BEGIN
    DotNetXmlNodeList := DotNetXmlDoc.getElementsByTagName(‘soap:Body’);
    IF ISNULL(DotNetXmlNode) THEN
    DotNetXmlNode := DotNetXmlNodeList.item(0);
    EVALUATE(Output,UPPERCASE(DotNetXmlNode.text(”)));
    // MESSAGE(Output);
    END;

    • Vivek,

      DotNetXmlHttp.open(‘POST’,’localhost/MyWebservice.asmx’,0,0,0);
      Comment – Just to get sure this means there is no ID and PWD for login as you are passing 0 as parameter.

      DotNetXmlHttp.setRequestHeader(‘SOAPAction’,’tempuri.org/mymethodname’);
      Comment – You are pretty sure this SOAPAction is correct

      If the above 2 is correct this there’s 99% chances that this piece of code is the culprit

      DotNetXmlHttp.send(”
      +”
      ”+
      ” +
      ”+”CustNo.”+”+
      ”+FORMAT(Amount)+”+
      ”+
      ”);

      Check this again and make sure you are passing the same way as it is.
      DotNetXmlHttp.send this part holds a major accountability.

  7. Hi,

    Thanks for your precious time for writing the solution for my problem.

    But still I am facing issue.According to your solution I have worked-

    Actually I am on my local machine and also I have mentioned the userid and password for the same but when code run with this
    line of code:

    DotNetXmlDoc.load(DotNetXmlHttp.responseBody);

    Client crashed showing error of –
    Dynamics nav will stop responding.

    2.In SoapAction will contain defaulturl/methodname,I think this is correct.

    3.In code I have also mentioned the send request code-

    IF ISNULL(DotNetXmlDoc) THEN
    DotNetXmlDoc := DotNetXmlDoc.DOMDocumentClass;

    IF ISNULL(DotNetXmlHttp) THEN
    DotNetXmlHttp := DotNetXmlHttp.XMLHTTPRequestClass;

    DotNetXmlHttp.open(‘POST’,’http://localhost/MyWebservice.asmx’,0,’userid’,’pwd’);
    DotNetXmlHttp.setRequestHeader(‘Host’,’http://localhost’);
    DotNetXmlHttp.setRequestHeader(‘Content-Type’,’text/xml; charset=utf-8′);
    DotNetXmlHttp.setRequestHeader(‘Content-Length’,’Length’);
    DotNetXmlHttp.setRequestHeader(‘SOAPAction’,’http://tempuri.org/PurchaseInitiation’);
    DotNetXmlHttp.send(”
    +’soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>’+
    ”+
    ” +
    ”+”CustNo.”+”+
    ”+FORMAT(Amount)+”+
    ”+
    ”+
    ”);

    DotNetXmlDoc.load(DotNetXmlHttp.responseBody);
    IF DotNetXmlHttp.status = 200 THEN BEGIN
    DotNetXmlNodeList := DotNetXmlDoc.getElementsByTagName(‘soap:Body’);
    IF ISNULL(DotNetXmlNode) THEN
    DotNetXmlNode := DotNetXmlNodeList.item(0);
    EVALUATE(Output,UPPERCASE(DotNetXmlNode.text(”)));
    // MESSAGE(Output);
    END;

    Please help me out.

  8. Hi,

    HTML Tag are not coming while posting in your blog-

    ‘(PaymentInitiation xmlns=”http://shopperlocal.com/”)’ +
    ‘(CustomerNo)’+”CustNo.”+'(/CustomerNo)’+
    ‘(Amount)’+FORMAT(Amount)+'(/Amount)’+
    ‘(/PaymentInitiation)’+
    ‘(/soap:Body)’+
    ‘(/soap:Envelope)’);

    I am posting my code with round bracket please consider it angular bracket.

    • Vivek,

      OK, let’s Start tracing your solution from the beginning.

      1. Try opening that Post URL in your Mozilla browser and verify you can input all parameters there and get the out put.
      2. Try opening that URL in your machine OR domain and check it;s behaving the same.
      3. See if you are pretty sure that there’s no problem in URL, Connection String, ID PWD, then do one thing – Instead of hard coding the XML .SEND command. Make a XMLPORT and use that one. As I said previously too if everything goes correct then here is the main problem.

      Try creating a XMLPORT and using it in send command and then see whats the output.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s