Important Update: Some Community URL Redirects are Under Maintenance. Learn More. .

cancel
Showing results for 
Search instead for 
Did you mean: 

Call SOAP WebAPI from Custom iView

JoyVanBuskirk
Collaborator II

Hi guys,

I'm trying to use the WebAPI from a Custom iView using JavaScript. I've not tried it from a Custom iView before and am getting a 500 error when I try to connect. Unfortunately, I don't have access to the server logs to get anything more detailed than the 500 error that I'm getting from the browser. Hoping one of you might be able to spot the issue?

Below is my code:

 

<html>
<head>
    <title>SOAP JavaScript Client Test</title>
    <script type="text/javascript">
        function soap() {
   if (window.sessionStorage) {
                csrfToken = window.sessionStorage.getItem("x-csrf-token");
                //csrfToken= parent.parent.ArcherSessionState.currentStateId;
            } else {
                csrfToken = parent.parent.ArcherApp.globals['xCsrfToken'];
                //csrfToken= parent.parent.ArcherSessionState.currentStateId;
            }
   console.log(csrfToken);
            var xmlhttp = new XMLHttpRequest();

            xmlhttp.open('POST', '/ws/search.asmx', true);
   xmlhttp.setRequestHeader('SOAPAction', '"http://archer-tech.com/webservices/GetRecordById"');
   xmlhttp.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
            // build SOAP request
            var sr =
                '<?xml version="1.0" encoding="utf-8"?>' +
                '<soap:Envelope ' +
                    'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
                    'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
                    'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">' +
                    '<soap:Body>' +
      '<GetRecordById xmlns="http://archer-tech.com/webservices/">' +
      '<sessionToken>' + csrfToken + '</sessionToken>' +
      '<moduleId>152</moduleId>' +
      '<contentId>5299172</contentId>' +
      '</GetRecordById>' +
                    '</soap:Body>' +
                '</soap:Envelope>';


            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {
                        alert(xmlhttp.responseText);
                    }
                }
            }
            xmlhttp.send(sr);
        }
    </script>
</head>
<body>
    <form name="Demo" action="" method="post">
        <div>
            <input type="button" value="Soap" onclick="soap();" />
        </div>
    </form>
</body>
</html>

1 ACCEPTED SOLUTION

Accepted Solutions

DavidPetty
Archer Employee
Archer Employee

Joy, the csrfToken isn't the same as the user session token.

 

To get the user's session token use:

parent.parent.ArcherApp.globals.workpointFeatures.SessionToken (For Archer 6.4 or lower)

or

JSON.parse(JSON.parse(atob(parent.parent.ArcherApp.globals.workpointFeatures))).SessionToken (For Archer 6.4 P1 or greater)

 

I also suggest using some console.log() in your code to output variable and such to the developer tools console to see where the errors are occurring.

 Advisory Consultant

View solution in original post

2 REPLIES 2

DavidPetty
Archer Employee
Archer Employee

Joy, the csrfToken isn't the same as the user session token.

 

To get the user's session token use:

parent.parent.ArcherApp.globals.workpointFeatures.SessionToken (For Archer 6.4 or lower)

or

JSON.parse(JSON.parse(atob(parent.parent.ArcherApp.globals.workpointFeatures))).SessionToken (For Archer 6.4 P1 or greater)

 

I also suggest using some console.log() in your code to output variable and such to the developer tools console to see where the errors are occurring.

 Advisory Consultant

Thanks David! You're right--the token issue was one of my problems. I've fixed that and the other two as well now. For anyone it would help, here's the working example. I'm all set now!

 

<html>
 <head>
  <script type="text/javascript">
   function makeSoapCall() { 
    var sessionToken = JSON.parse(JSON.parse(atob(parent.parent.ArcherApp.globals.workpointFeatures))).SessionToken; 
    var xmlhttp = new XMLHttpRequest();

    xmlhttp.open('POST', '/ws/record.asmx', true);
    xmlhttp.setRequestHeader('SOAPAction', 'http://archer-tech.com/webservices/GetRecordById');
    xmlhttp.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
    var xml =
     '<?xml version="1.0" encoding="utf-8"?>' +
     '<soap:Envelope ' +
      'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
      'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
      'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
      '<soap:Body>' +
       '<GetRecordById xmlns="http://archer-tech.com/webservices/">' +
       '<sessionToken>' + sessionToken + '</sessionToken>' +
       '<moduleId>152</moduleId>' +
       '<contentId>5299172</contentId>' +
       '</GetRecordById>' +
      '</soap:Body>' +
     '</soap:Envelope>';
    xmlhttp.onreadystatechange = function () {
     if (xmlhttp.readyState == 4) {
      if (xmlhttp.status == 200) {
       alert(xmlhttp.responseText);
      }
     }
    }
    xmlhttp.send(xml);
   }
  </script>
 </head>
 <body>
  <form name="Demo" action="" method="post">
   <div>
    <input type="button" value="Make SOAP Call" onclick="makeSoapCall();" />
   </div>
  </form>
 </body>
</html>