Call Web Service from NAV

SoumyadipSoumyadip Member Posts: 209
edited 2010-01-30 in NAV Three Tier
Hi,

I know how to create page/codeunit web services and to expose them. I can call NAV web services from external (C#) programs.

How do i call an external Web Service from NAV?

Regards,

Soumyadip

Comments

  • dkonedkone Member Posts: 59
  • SoumyadipSoumyadip Member Posts: 209
    Is there any documentation available on it?
  • SoumyadipSoumyadip Member Posts: 209
    Thanks.

    I have gone through these document. With help of this i can expose a Page or Codeunit web service from NAV. I can consume the same from a C# program.

    But, I want to consume a web service from NAV exposed by 3rd Party application. (e.g In customer card, i want to show the current stock (NASDAQ) value of the customer. To do that i need to call any standard web services exposed by different websites. How do i call a web service FROM NAV?)

    I have got some sample code from the download section but was looking for some documentation as we do have some (to call NAV web services from outside) in MSDN/Product DVD.
  • SoumyadipSoumyadip Member Posts: 209
    Can anyone help me here?
  • kinekine Member Posts: 12,562
    I recommend to read SOAP SDK documentation. There is no specific DOC for consuming WS within NAV... (or may be it is but it is not known)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • PhiltooPhiltoo Member Posts: 36
    The way I've gone about this is to create a COM object with C# which interfaces with the service in question. Since the C# is a COM object (.dll) you can define a public interface with properties and functions which can be called from within NAV as an automation object.

    See this article http://msdn.microsoft.com/en-us/library/aa973247.aspx
  • SoumyadipSoumyadip Member Posts: 209
    I was trying this link - http://www.mibuso.com/dlinfo.asp?FileID=539
    I can call the Web Service now. It is generating the XML file in specified path. \:D/

    Still, bit of problem remains...

    When I am passing parameters to WebService, it is not recognizing it. and within the WebService(function) treating the input parameter as blank.

    Pasting two blocks of code -

    NAV CODE-
    CREATE(locautSoapHttpConnector);
    locautSoapHttpConnector.Property('EndPointURL', 'http://localhost:1041/Service1.asmx');
    locautSoapHttpConnector.Connect;
    locautSoapHttpConnector.Property('AuthUser', 'User');
    locautSoapHttpConnector.Property('AuthPassword', 'Password');
    locautSoapHttpConnector.Property('Timeout', 5 * 1000);
    locautSoapHttpConnector.Property('SoapAction','http://temp.com/Math_Cos');
    locautSoapHttpConnector.BeginMessage;
    
    CREATE(locautSoapSerializer);
    locautSoapSerializer.Init(locautSoapHttpConnector.InputStream);
    locautSoapSerializer.StartEnvelope('SOAP','STANDARD');
    locautSoapSerializer.StartBody;
    locautSoapSerializer.StartElement('Math_Cos');
      locautSoapSerializer.StartElement('Angle');
      locautSoapSerializer.WriteString('45');
      locautSoapSerializer.EndElement;
    locautSoapSerializer.EndElement;
    
    locautSoapSerializer.EndBody;
    locautSoapSerializer.EndEnvelope;
    locautSoapHttpConnector.EndMessage;
    
    CREATE(locautXmlDoc);
    locautXmlDoc.load(locautSoapHttpConnector.OutputStream);
    locautXmlDoc.save('C:\Soumya\DealerMngt\Test4Dealer\MathCos.xml');
    

    WebService Code -
    [System.Web.Services.WebMethod(Description = "Test4Dealer Math Cos")]
            public double Math_Cos(double Angle)
            {
                return System.Math.Cos(Angle);
            }
    


    The result i am getting is 1. That is value of Cos 0. I have tried the same with some other functions with different parameter types. But the result proves that the input parameter has been ignored.

    Just to add, all the functions are runing fine when i am running .asmx file in browser.


    Any idea, what am i missing here?
  • kinekine Member Posts: 12,562
    I am using same and it is working, there is only difference in some parameters when initializing the call (variables are named as I use them, but they are same as yours by type etc.):
      lauSoapSerializer.Init(lauSoapConnector.InputStream);
      lauSoapSerializer.StartEnvelope();
      lauSoapSerializer.StartBody('STANDARD');
      lauSoapSerializer.StartElement('Math_Cos','http://temp.com');
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • SoumyadipSoumyadip Member Posts: 209
    No luck with this one as well -
    CREATE(locautSoapSerializer);
    locautSoapSerializer.Init(locautSoapHttpConnector.InputStream);
    locautSoapSerializer.StartEnvelope();
    locautSoapSerializer.StartBody('STANDARD');
    locautSoapSerializer.StartElement('Math_Cos','http://temp.com');
      locautSoapSerializer.StartElement('Angle');
      locautSoapSerializer.WriteString('23');
      locautSoapSerializer.EndElement;
    locautSoapSerializer.EndElement;
    
  • SoumyadipSoumyadip Member Posts: 209
    Can anyone tell me why with the above mentioned block of code, i am being able to call a webservice but it is taking the parameter as 0 or blank?

    Do we have any other example codes apart from the one mentioned above?
  • SoumyadipSoumyadip Member Posts: 209
    I was trying this link - http://www.mibuso.com/dlinfo.asp?FileID=539
    I can call the Web Service now. It is generating the XML file in specified path.

    Still, bit of problem remains...

    When I am passing parameters to WebService, it is not recognizing it. and within the WebService(function) treating the input parameter as blank.

    The difference in my code from
    is in variable type -

    i am using -

    locautSoapHttpConnector = 'Microsoft Soap Type Library v3.0'.HttpConnector30
    locautSoapSerializer = 'Microsoft Soap Type Library v3.0'.SoapSerializer30

    Hope this is not causing the problem ... :(
  • SoumyadipSoumyadip Member Posts: 209
    It is working now \:D/
  • mike.italymike.italy Member Posts: 8
    Soumyadip wrote:
    It is working now \:D/

    How did you sort out the problem of the blank parameter?
    I'm having the same issue...

    Thanks a lot
  • anieanie Member Posts: 14
    I have called the web service method with parameters and also saved the OutputStream in an XML file. This is the content of the file:

    <?xml version="1.0" encoding="utf-8" ?>
    - <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    - <soap:Body>
    - <soap:Fault>
    <faultcode>soap:Server</faultcode>
    <faultstring>Server was unable to process request. ---> Object reference not set to an instance of an object.</faultstring>
    <detail />
    </soap:Fault>
    </soap:Body>
    </soap:Envelope>

    This is the code used from NAV:


    CREATE(locautSoapHttpConnector);
    locautSoapHttpConnector.Property('EndPointURL','http://10.16.16.94/DemoService/Service.asmx');
    locautSoapHttpConnector.Connect;
    locautSoapHttpConnector.Property('AuthUser','User');
    locautSoapHttpConnector.Property('AuthPassword','Password');
    locautSoapHttpConnector.Property('Timeout', 5 * 1000);
    locautSoapHttpConnector.Property('SoapAction','http://tempuri.org/ToUpper');

    locautSoapHttpConnector.BeginMessage;
    CREATE(locautSoapSerializer);
    locautSoapSerializer.Init(locautSoapHttpConnector.InputStream);
    locautSoapSerializer.startEnvelope();
    locautSoapSerializer.startBody('STANDARD');
    locautSoapSerializer.startElement('ToUpper','http://tempuri.org');
    locautSoapSerializer.startElement('input');
    locautSoapSerializer.writeString('navision');
    locautSoapSerializer.endElement;
    locautSoapSerializer.endElement;

    locautSoapSerializer.endBody;
    locautSoapSerializer.endEnvelope;
    locautSoapHttpConnector.EndMessage;

    CREATE(locautXmlDoc);
    locautXmlDoc.load(locautSoapHttpConnector.OutputStream);
    locautXmlDoc.save('c:\temp2.xml');

    Can anybode suggest why this fault code is coming??
  • anieanie Member Posts: 14
    Hi,

    I have hosted the web service in my local machine and now the OutputStream is showing a different fault code:

    System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object. at Service.ToUpper(String input) --- End of inner exception stack trace ---

    Could anyone suggest how to resolve this?

    Thanks a lot.
  • SoumyadipSoumyadip Member Posts: 209
    I think the problem is with you SoapSerializer. More specificaly in this block of the code
    locautSoapSerializer.startElement('input');
    locautSoapSerializer.writeString('navision');
    locautSoapSerializer.endElement
    

    Try changing it with this one
    locautSoapSerializer.WriteXml('<ToUpper xmlns="http://tempuri.org/"><txtInputParam>' +
                                    paramNAVCapInput + '</txtInputParam></ToUpper>');
    
    Here txtInputParam is the parameter absorbed by my WebService and a text variable "paramNAVCapInput" is being used to supply the value
  • anieanie Member Posts: 14
    :mrgreen: It is working fine this way.. Thanks.
  • SoumyadipSoumyadip Member Posts: 209
    Great...

    I was wondering why the more conventional way of building the SOAP message (with StartElement and WriteString) is not working in our case...

    Any idea?
  • howfunhowfun Member Posts: 1
    I have difficulty to install soap component into NAV or computer. I download ms soap tool kit and install on my computer, I cannot find those component in NAV or even on in DCOM component. I did even try to register those component in nav. Please help.

    thanks.

    hong
  • freddy.dkfreddy.dk Member, Microsoft Employee Posts: 360
    Freddy Kristiansen
    Group Program Manager, Client
    Microsoft Dynamics NAV
    http://blogs.msdn.com/freddyk

    The information in this post is provided "AS IS" with no warranties, and confers no rights. This post does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion.
  • EBothaEBotha Member Posts: 6
    Where can I find equivalent code to wat has been mentioned for NAV 2016? I am unable to find the Subtypes for variables : locautSoapHttpConnector = 'Microsoft Soap Type Library v3.0'.HttpConnector30 &
    locautSoapSerializer = 'Microsoft Soap Type Library v3.0'.SoapSerializer30
Sign In or Register to comment.