Thursday, October 12, 2006

Coldfusion: Consuming Web Services with Coldfusion

Since I had developed such a pretty Web Service in Web Designer 2005 Express previously, I wanted to see the different methods to consume this service in various languages. I already demonstrated how to do so in ASP .Net, so now I have decided to go a little off the beaten path and do so in Coldfusion MX. Fortunately, doing so in Coldfusion MX using Dreamweaver as the development environment is incredibly easy.



In Dreamweaver 8, there is a really cool Web Service Component tool under the Application/Component tab. If you click on the “+” after selecting “Web Service” from the drop down, you will get a little wizard asking for the URL to retrieve the WSDL for the web service. That’s it; the Proxy generator will take care of the rest.



To consume the service, you can drag over the various methods into your Coldfusion page, or you can manually put in the Coldfusion tags to do so. In my case, I want to use the “getEmployeeAndTranscript” method in my web service, and return an Employee object. I need to pass in an ID as well, so I would use the CFINVOKE take like so:



<cfinvoke webservice="http://training.citibankus.citigroup.net/test/ws/getEmployeeTranscript/Service1.asmx?WSDL"

method="getEmployeeAndTranscript"

returnvariable="aEmployee">

     <cfinvokeargument name="id" value="0005003335"/>

</cfinvoke>



This will return my Employee object with the Transcript brought back as an array. Since the transcript is an array, I can loop through the results by retrieving the array length. Below is the code I used to return my results:



<cfoutput>

     #aEmployee.name#<br />

     #aEmployee.ID#<br />

     Total Courses: #ArrayLen(aEmployee.transcript.transcriptEntry)#<br />

     <table border="1">

       <tr>

          <td> </td>

          <td><strong>Course Code</strong></td>

          <td><strong>Course Name</strong></td>

          <td><strong>Completion Date</strong></td>

       </tr>

       

       <cfloop index="i" from="1" to="#ArrayLen(aEmployee.transcript.transcriptEntry)#">

          <tr>

          <td>#i#</td>

          <td>#aEmployee.transcript.transcriptEntry[i].CourseCode#</td>

          <td>#aEmployee.transcript.transcriptEntry[i].CourseName#</td>

          <td>#DateFormat(aEmployee.transcript.transcriptEntry[i].completionDate, "dd-mmm-yyyy")#</td>

          </tr>

     </cfloop>

     

     </table>          

</cfoutput>



The first thing I am doing in this snippet is returning the Employee information. Then I use the ArrayLen function to output the number of transcript entries. I then put a table in place and put up the header for the columns. I then use a CFLOOP tag to go through each element in the array. Coldfusion did something really strange with the Array itself. It created a Object called ArrayOfTranscript, with a property called transcriptEntry which contained the actual array. I suppose this is more inline with the original Web Service relationship I created. If I didn’t have the web service viewer that Dreamweaver provides, I would not have been able to determine this structure.

No comments: