Sample code for 30+ languages & platforms
SQL Server

Royal Mail OBA Web Service -- Get Contracts

See more Royal Mail OBA Examples

Royal Mail OBA Web Service -- Get the contracts for an account.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    -- Create the following XML:

    -- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:epro="urn:schemas-royalmail-com/webservice/epro">
    --    <soapenv:Header>
    --       <epro:Authentication>
    --          <epro:Username>my_username</epro:Username>
    --          <epro:Password>my_password</epro:Password>
    --          <epro:AccessCode>my_accessCode</epro:AccessCode>
    --          <epro:Version>1.0</epro:Version>
    --          <epro:SecurityToken>mysecuritytoken</epro:SecurityToken>
    --       </epro:Authentication>
    --    </soapenv:Header>
    --    <soapenv:Body>
    --       <epro:GetContracts>
    --         <epro:Account>account_number</epro:Account>
    --       </epro:GetContracts>
    --    </soapenv:Body>
    -- </soapenv:Envelope>

    DECLARE @xml int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @xml, 'EmitXmlDecl', 0
    EXEC sp_OASetProperty @xml, 'Tag', 'soapenv:Envelope'
    EXEC sp_OAMethod @xml, 'AddAttribute', @success OUT, 'xmlns:soapenv', 'http://schemas.xmlsoap.org/soap/envelope/'
    EXEC sp_OAMethod @xml, 'AddAttribute', @success OUT, 'xmlns:epro', 'urn:schemas-royalmail-com/webservice/epro'

    -- Replace my_username, my_password, and my_accessCode with your actual values.  Do not replace "mysecuritytoken".
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Header|epro:Authentication|epro:Username', 'my_username'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Header|epro:Authentication|epro:Password', 'my_password'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Header|epro:Authentication|epro:AccessCode', 'my_accessCode'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Header|epro:Authentication|epro:Version', '1.0'

    -- Literally use the string "mysecuritytoken" here.
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Header|epro:Authentication|epro:SecurityToken', 'mysecuritytoken'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|epro:GetContracts|epro:Account', 'account_number'

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'SOAPAction', 'urn:schemas-royalmail-com/webservice/epro/GetContracts'

    -- Use the sandbox endpoint:   http://interfaces.oba-v.royalmail.com/legacyws/EProImport.asmx 
    -- The live endpoint would be: http://www.epro.royalmail.com/WebServices/import/eproimport.asmx
    DECLARE @endpointUrl nvarchar(4000)
    SELECT @endpointUrl = 'http://interfaces.oba-v.royalmail.com/legacyws/EProImport.asmx'

    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT
    EXEC sp_OAMethod @http, 'HttpStr', @success OUT, 'POST', @endpointUrl, @sTmp0, 'utf-8', 'text/xml', @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @xml
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END


    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT 'response status code = ' + @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0

    -- The response looks like this:

    -- <?xml version="1.0" encoding="utf-8"?>
    -- <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    --     <SOAP-ENV:Header/>
    --     <SOAP-ENV:Body>
    --         <ns1:GetContractsResponse xmlns:ns1="urn:schemas-royalmail-com/webservice/epro">
    --             <ns1:GetContractsResult Version="1">
    --                 <ns1:Contracts>
    --                     <ns1:ContractDetails ServiceName="ADV MAIL + RESPONSE UNSORTED 2C" ServiceReg="AC2" ServiceLevel="01" Contract="DEMOTEST1"/>
    --                     <ns1:ContractDetails ServiceName="PLANNED ENVELOPE POLL CARD MMK-2C      " ServiceReg="AG0" ServiceLevel="01" Contract=""/>
    -- 			...
    --                 </ns1:Contracts>
    --             </ns1:GetContractsResult>
    --         </ns1:GetContractsResponse>
    --     </SOAP-ENV:Body>
    -- </SOAP-ENV:Envelope>

    -- 

    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @sTmp0

    DECLARE @serviceName nvarchar(4000)

    DECLARE @serviceReg nvarchar(4000)

    DECLARE @serviceLevel nvarchar(4000)

    DECLARE @contract nvarchar(4000)

    DECLARE @i int
    SELECT @i = 0
    DECLARE @numContracts int
    EXEC sp_OAMethod @xml, 'NumChildrenHavingTag', @numContracts OUT, 'SOAP-ENV:Body|ns1:GetContractsResponse|ns1:GetContractsResult|ns1:Contracts|ns1:ContractDetails'
    WHILE @i < @numContracts
      BEGIN
        EXEC sp_OASetProperty @xml, 'I', @i
        EXEC sp_OAMethod @xml, 'ChilkatPath', @serviceName OUT, 'SOAP-ENV:Body|ns1:GetContractsResponse|ns1:GetContractsResult|ns1:Contracts|ns1:ContractDetails[i]|(ServiceName)'
        EXEC sp_OAMethod @xml, 'ChilkatPath', @serviceReg OUT, 'SOAP-ENV:Body|ns1:GetContractsResponse|ns1:GetContractsResult|ns1:Contracts|ns1:ContractDetails[i]|(ServiceReg)'
        EXEC sp_OAMethod @xml, 'ChilkatPath', @serviceLevel OUT, 'SOAP-ENV:Body|ns1:GetContractsResponse|ns1:GetContractsResult|ns1:Contracts|ns1:ContractDetails[i]|(ServiceLevel)'
        EXEC sp_OAMethod @xml, 'ChilkatPath', @contract OUT, 'SOAP-ENV:Body|ns1:GetContractsResponse|ns1:GetContractsResult|ns1:Contracts|ns1:ContractDetails[i]|(Contract)'

        PRINT @serviceName
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @xml
    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @resp


END
GO