C# Sample Code — Read (SOAP API)
This section walks through a sample C# application. Web service client access helper classes and stubs were generated using Microsoft Visual Studio 2005. For more information about generating these classes, see Getting Started with the XML API and SOAP API. This example demonstrates the following functions:
-
Sign in to SuiteProjects Pro web services with user credentials entered by the user at the console prompt.
-
Retrieve all expense reports (
Envelope
objects) in your SuiteProjects Pro account. This operation uses theread()
command andall
method. -
Retrieve the expense report envelope (
Envelope
object) with the internal ID entered by the user at the console prompt. This operation uses theread()
command andequal to
method. -
Sign out of SuiteProjects Pro web services.
using System;
using SoapApplication.OpenAir;
namespace SoapApplication
{
class Program
{
/// <summary>
/// Instance of SuiteProjects Pro web services proxy object
/// </summary>
private static OAirServiceHandlerService m_svc;
/// <summary>
/// Console prompt for user credentials
/// </summary>
private static string GetUserInput(string prompt)
{
Console.Write(prompt);
try
{
return Console.ReadLine();
}
catch (System.IO.IOException e)
{
return null;
}
}
/// <summary>
/// Sign in to SuiteProjects Pro web services using with user credentials.
/// </summary>
/// <returns>True if successful, false if not</returns>
private static bool Login()
{
// Set up login information
OpenAir.LoginParams lp = new OpenAir.LoginParams();
lp.api_key = "******************";
lp.api_namespace = "company_namespace";
lp.user = GetUserInput("Enter username: ");
lp.password = GetUserInput("Enter password: ");
lp.company = GetUserInput("Enter company: ");
// Get an instance of SuiteProjects Pro web services and login
m_svc = new OAirServiceHandlerService();
LoginResult loginResult;
try
{
loginResult = m_svc.login(lp);
Console.WriteLine("Signed in, session ID = " + loginResult.sessionId);
// Set up session header to include returned session ID to perform further operations
SessionHeader header = new SessionHeader();
header.sessionId = loginResult.sessionId;
m_svc.SessionHeaderValue = header;
}
catch (System.Web.Services.Protocols.SoapException e)
{
// Catch any login problems and return
Console.WriteLine(e.Message);
return false;
}
return true;
}
/// <summary>
/// Output the specified Envelope object.
/// </summary>
private static void PrintEnvelope(oaEnvelope env)
{
Console.WriteLine("-----------------------------");
Console.WriteLine("ID:\t" + env.id);
Console.WriteLine("Name:\t" + env.total);
Console.WriteLine("Date:\t" + env.date);
Console.WriteLine("Total:\t" + env.total);
Console.WriteLine("# receipts:\t" + env.tottickets);
// Add any other envelope properties required
}
/// <summary>
/// Read all envelopes from SuiteProjects Pro web services
/// </summary>
static void ReadAllEnvelopes()
{
// Do the read operation
Console.WriteLine("\nPerforming read of ALL envelopes\n");
ReadRequest req = new ReadRequest();
req.type = "Envelope";
req.method = "all";
try
{
ReadResult[] results = m_svc.read(new ReadRequest[] { req });
// iterate through our results and output them to console
foreach (ReadResult result in results)
{
// Output any errors
if (result.errors != null)
{
foreach (oaError err in result.errors)
{
Console.WriteLine("Error "+err.code + ": " + err.comment + "\t" + err.text);
}
}
// Output the envelope read results
if (result.objects != null)
{
Console.WriteLine("Received "+result.objects.Length+" envelope(s) from SuiteProjects Pro");
foreach (oaEnvelope env in result.objects)
{
PrintEnvelope(env);
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Error while reading envelopes:\n" + e);
}
}
/// <summary>
/// Read the envelope with the internal ID entered by the user at the console prompt
/// </summary>
private static void ReadSingleEnvelope()
{
Console.WriteLine("\n\nPerforming read using \"equal to\" method");
oaEnvelope envelope = new oaEnvelope();
envelope.id = GetUserInput("Enter an envelope id: ");
ReadRequest req = new ReadRequest();
req.objects = new oaBase[] { envelope };
req.type = "Envelope";
req.method = "equal to";
try
{
ReadResult[] results = m_svc.read(new ReadRequest[] { req });
// Iterate through read results and output them in the console
foreach (ReadResult result in results)
{
// output any errors
if (result.errors != null)
{
foreach (oaError err in result.errors)
{
Console.WriteLine("Error " + err.code + ": " + err.comment + "\t" + err.text);
}
}
// Output the envelope read results
if (result.objects != null)
{
Console.WriteLine("Received " + result.objects.Length + " envelope(s) from SuiteProjects Pro");
foreach (oaEnvelope env in result.objects)
{
PrintEnvelope(env);
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Error while reading envelopes:\n" + e);
}
}
/// <summary>
/// Application entry point
/// </summary>
static void Main(string[] args)
{
if (Login())
{
ReadAllEnvelopes();
ReadSingleEnvelope();
// end our session
m_svc.logout();
}
Console.WriteLine("\nPress enter to exit");
Console.ReadLine();
}
}
}