Netsuite Suitetalk API – Getting started with c#

2014, Nov 28    

Not finding a great number of c# examples using the Netsuite Suitetalk  API i thought i’d post a basic example of using the web service.

This simply just authenticates, gets a list of all the users and outputs their names and email addresses to the console.

Snippet

class Program
{
  static bool _isAuthenticated;
  static NetSuiteService _service;
 
  static void Main(string[] args)
  {
    _service = new NetSuiteService();
    Login();
    GetEmployees();
    _service.logout();
  
    Console.Read();
  }
 
  private static void Login()
  {
    //_service.Url = "https://webservices.netsuite.com/services/NetSuitePort_2014_2";
    //_service.Url = "https://webservices.na1.netsuite.com/dcrf.nl?k=58baee60ac4e3c16a1765edcb0b3a51e8c6d4b154b13db1f603dd219435cc15d&dcrdt=%2Fservices";
    //_service.Url = "https://webservices.na1.netsuite.com/services";
    _service.Url = "https://webservices.na1.netsuite.com/services/NetSuitePort_2014_2";
    _service.CookieContainer = new CookieContainer();
 
    //invoke the login operation
    Passport passport = new Passport();
    passport.account = "1234567"; //Get value from here : https://system.na1.netsuite.com/app/webservices/setup.nl?whence=
    passport.email = "me@company.com";
    passport.password = "myPassword";
 
    //only need this if your default role does not have web service access
    //RecordRef role = new RecordRef();
    //role.internalId = "15";
    ////role.id = "3";
    //passport.role = role;
 
    try
    {
      SessionResponse response = _service.login(passport);
      Status status = response.status;
      _isAuthenticated = status.isSuccess;
 
      Console.WriteLine(status.statusDetail);
    }
    catch (Exception ex)
    {
      Console.WriteLine(ex.ToString());
    }
  }
 
  private static void GetEmployees()
  {
    EmployeeSearchBasic esb = new EmployeeSearchBasic();
 
    esb.isInactive = new SearchBooleanField();
    esb.isInactive.searchValue = false;
    esb.isInactive.searchValueSpecified = true;
 
    SearchResult res = _service.search(esb);
    res.pageSize = 500;
    res.pageSizeSpecified = true;
 
    if (res.status.isSuccess)
    {
      Record[] searchRecords = res.recordList;
      if (searchRecords != null && searchRecords.Length >= 1)
      {
        //Do something with the results... just to the console for the time being.
        List<Employee> employees = searchRecords.Select(e => (Employee)e).ToList();
 
        
        foreach (Employee e in employees)
        {
          Console.WriteLine(string.Format("{0} {1} : {2}", e.firstName, e.lastName, e.email));
        }
 
      }
      else
      {
        Console.WriteLine("No search results returned");
      }
    }
    else
    {
      Console.WriteLine("Search was not successful");
    }
  }
}