In this post we will see how to create a Soap Request Message in AL. I have created a set of function which will be helpful when you work with SOAP Web Services.
I have created the functions in a new codeunit and invoked them in the page for testing. I have used the XML DOM Mgt. Code unit for creating the Soap Message.
Below is the output from the web client:
Code:
page 50100 "Soap Test Page" { PageType = Card; Caption = 'Soap Test Page'; actions { area(processing) { action("Create Soap Message") { Caption = 'Create Soap Message'; ApplicationArea = All; Image = XMLFile; trigger OnAction() var XML: Codeunit "SOAP Document"; begin XML.CreateSoapMessage; end; } } } } codeunit 50102 "SOAP Document" { var XMLDomMgt: Codeunit "XML DOM Mgt."; //https://diveshboramsdnavblog.wordpress.com/2018/03/09/vs-code-xml-dom-management-part-2/ SoapNS11: Label 'http://schemas.xmlsoap.org/soap/envelope/', Locked = true; SoapNS12: Label 'http://www.w3.org/2003/05/soap-envelope', Locked = true; XsiNS: Label 'http://www.w3.org/2001/XMLSchema-instance', Locked = true; XsdNS: Label 'http://www.w3.org/2001/XMLSchema', Locked = true; //Use this function to Create a Soap Message procedure CreateSoapMessage(); var lXmlDocument: XmlDocument; lEnvolopeXmlNode: XmlNode; lHeaderXmlNode: XmlNode; lBodyXmlNode: XmlNode; lTempXmlNode: XmlNode; lXMLText: Text; begin CreateSoapDocument(lXmlDocument, 1, lEnvolopeXmlNode, lHeaderXmlNode, lBodyXmlNode); //Add Additional Nodes to the Soap Headers if Needed- Below is the Sample XMLDomMgt.AddElement(lHeaderXmlNode, 'SampleHeaders', 'Test', SoapNS12, lTempXmlNode); //You can add/append an existing Node to the Soap Body using XmlNode.AsXmlElement.InnerXml - Below is the Sample XMLDomMgt.AddElement(lBodyXmlNode, 'SampleBody', 'Test', SoapNS12, lTempXmlNode); lXmlDocument.WriteTo(lXMLText); Message(lXMLText); end; //Use this function to Create a Soap Document with Soap Version 1.1 & 1.2. This function will return the XML Document along with the reference of the created nodes like Envelope, Header & Body. procedure CreateSoapDocument(var pXmlDocument: XmlDocument; pVersion: Option "1.1", "1.2"; var pEnvelopeXmlNode: XmlNode; var pHeaderXmlNode: XmlNode; var pBodyXmlNode: XmlNode); begin CreateEnvelope(pXmlDocument, pEnvelopeXmlNode, pVersion); CreateHeader(pEnvelopeXmlNode, pHeaderXmlNode, pVersion); CreateBody(pEnvelopeXmlNode, pBodyXmlNode, pVersion); end; //Use this function to Create a Soap Document with Soap Version 1.1 & 1.2. This function will return the XML Document along with the reference of the created Body node. procedure CreateSoapDocumentBody(var pXmlDocument: XmlDocument; pVersion: Option "1.1", "1.2"; var pBodyXmlNode: XmlNode); var lEnvelopeXmlNode: XmlNode; lHeaderXmlNode: XmlNode; begin CreateSoapDocument(pXmlDocument, pVersion, lEnvelopeXmlNode, lHeaderXmlNode, pBodyXmlNode); end; //This function will create a Soap Envelope procedure CreateEnvelope(var pXmlDocument: XmlDocument; var pEnvelopeXmlNode: XmlNode; pVersion: Option "1.1", "1.2"); begin pXmlDocument := XmlDocument.Create; With XMLDomMgt do begin AddDeclaration(pXmlDocument, '1.0', 'UTF-8', 'no'); if pVersion = pVersion::"1.1" then AddRootElementWithPrefix(pXmlDocument, 'Envelope', 'Soap', SoapNS11, pEnvelopeXmlNode) else AddRootElementWithPrefix(pXmlDocument, 'Envelope', 'Soap', SoapNS12, pEnvelopeXmlNode); AddPrefix(pEnvelopeXmlNode, 'xsi', XsiNS); AddPrefix(pEnvelopeXmlNode, 'xsd', XsdNS); end; end; //This function will create a Soap Header procedure CreateHeader(var pEnvelopeXmlNode: XmlNode; var pHeaderXmlNode: XmlNode; pVersion: Option "1.1", "1.2"); begin if pVersion = pVersion::"1.1" then XMLDOMMgt.AddElement(pEnvelopeXmlNode, 'Header', '', SoapNS11, pHeaderXmlNode) else XMLDOMMgt.AddElement(pEnvelopeXmlNode, 'Header', '', SoapNS12, pHeaderXmlNode); end; //This function will create a Soap Body procedure CreateBody(var pSoapEnvelope: XmlNode; var pSoapBody: XmlNode; pVersion: Option "1.1", "1.2"); begin if pVersion = pVersion::"1.1" then XMLDOMMgt.AddElement(pSoapEnvelope, 'Body', '', SoapNS11, pSoapBody) else XMLDOMMgt.AddElement(pSoapEnvelope, 'Body', '', SoapNS12, pSoapBody); end; }
Hi Divesh,
Your post is quite helpful for the web service Integration with D365.
I was following your post and I am stuck with Cdata section. I did research on XMLCdata Azure Function in Business Central but didn’t find ant examples for the XMLCdata datatype usage.
I have also psoted this issue on the below link.
https://community.dynamics.com/business/f/758/t/300981
Can you help me in generating below xml File .Actually only Cdata Section.
<![CDATA[XXXXXXXXTEST03027515]]>
LikeLike
Hi Manish,
I have checked your code and fixed the issue. Modification is required in the function CreateBody.
procedure CreateBody(var pSoapEnvelope: XmlNode; var pSoapBody: XmlNode; var pOrderNo: code[8]);
begin
Clear(StrxmlNodeValue);
XMLDOMMgt.AddElement(pSoapEnvelope, ‘Body’, ”, SOAPNamespaceLbl, pSoapBody);
StrxmlNodeValue := ” + ‘XXX’ + ” +
‘XXXXX’ + ” + ‘TEST’ +
” + pOrderNo + ”;
xmlcd := XmlCData.Create(StrxmlNodeValue);
XMLDOMMgt.AddElement(pSoapBody, ‘OrderStatus’, ”, XSDNamespaceLbl, OrdStatusNode);
OrdStatusNode.AddAfterSelf(xmlcd);
end;
LikeLike