Call SOAP WebService from Azure Function

MathesMathes Member Posts: 21
Dear all,

does someone has experience calling a nav soap webservice from an Azure Function?

Regards

Mathes

Answers

  • ftorneroftornero Member Posts: 522
    Hello @Mathes,

    Yes I have some experience calling a NAV webservice from an Azure Function.

    Regards.
  • MathesMathes Member Posts: 21
    Hi Ftornero, thanks, can you share your experience? How can I write an Azure function to call eg. a NAV Page as a SOAP WebServicE?
  • ftorneroftornero Member Posts: 522
    Hello @Mathes ,

    Assuming that you have published the page 21 as a webservice with the name Customer, this Azure function in Powershell call the webserice.
    # POST method: $req
    $requestBody = Get-Content $req -Raw | ConvertFrom-Json
    $customer = $requestBody.customer
    
    # GET method: each querystring parameter is its own variable
    if ($req_query_name) 
    {
        $name = $req_query_name 
    }
    
    function Execute-SOAPRequest 
    ( 
      [String] $SOAPRequest, 
      [String] $URL,
      [String] $SOAPAction 
    ) 
    { 
      write-host "Enviando petición SOAP al Servidor: $URL" 
      $soapWebRequest = [System.Net.WebRequest]::Create($URL) 
    
      $username = "user_name"
      $password = "password"
      $cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
      $soapWebRequest.Credentials = $cred
    
      $soapWebRequest.Headers.Add("SOAPAction", $SOAPAction)
      $soapWebRequest.ContentType = "text/xml; charset=utf-8"
      $soapWebRequest.UserAgent   = "Microsoft ADO.NET Data Services"
    
      $soapWebRequest.Method      = "POST" 
      $soapWebRequest.Timeout = 800000         
    
      $writer = New-Object System.IO.StreamWriter $soapWebRequest.GetRequestStream();
      $writer.WriteLine($SOAPRequest);
      $writer.Close();      
    
      try {
        $resp = $soapWebRequest.GetResponse() 
        $responseStream = $resp.GetResponseStream() 
        $soapReader = [System.IO.StreamReader]($responseStream) 
        $ReturnXml = [Xml] $soapReader.ReadToEnd() 
        $responseStream.Close() 
        return $ReturnXml 
      } catch {
        Write-Error $Error[0]
      }
    }
    
    $url = "http://xx.xx.xx.xx:7047/DynamicsNAV/WS/COMPANYNAME/Page/Customer"
    
    $soap = '<?xml version="1.0" encoding="utf-8"?>' + 
                '<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">'+
                '<soap:Body>' +
                    '<Read xmlns="urn:microsoft-dynamics-schemas/page/customer">' +
                      '<No>' + $customer +
                      '</No>'+
                    '</Read>' +
                '</soap:Body>' +
                '</soap:Envelope>'
    
    
    $action = '"urn:microsoft-dynamics-schemas/page/Customer"'
    
    $ret = Execute-SOAPRequest $soap $url $action
    
    [xml]$xml = $ret.InnerXML
    
    $cust = $xml.Envelope.Body.Read_Result.Customer 
    
    $hash = @{
              status = 200
             }
    
    $customer_data = @{
                 no   = $cust.No
                 name = $cust.Name
                 address = $cust.Address     
                 city = $cust.City
                 county = $cust.County
                 post_code = $cust.Post_Code
                 phone_no = $cust.Phone_No
    }
        
    $hash.add("customer", $customer_data)
    
    $json = $hash | ConvertTo-Json    
    
    Out-File -Encoding default -FilePath $res -inputObject $json
    


    You need to pass a Json like this, in this case 29754 is de customer no.
    {
        "customer": "29754"
    }
    

    In the Powershell script you need to change with real data this lines
      $username = "user_name"
      $password = "password"
    
      .....
    
      $url = "http://xx.xx.xx.xx:7047/DynamicsNAV/WS/COMPANYNAME/Page/Customer"
    
    
    


    To use Powershell in the Azure function you need to activate this

    da3u1abj1q6n.png


    And then you can create the Powershell function.

    2o0y8ddny16i.png
Sign In or Register to comment.