Creating Soap Request Message in AL

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:

SoapRequestMessage

Code:

SoapRequestMessageCode

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;

}

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s