Suite RK
02/16/2022, 12:11 PMbattk
02/16/2022, 12:14 PMbattk
02/16/2022, 12:14 PMSuite RK
02/16/2022, 12:15 PMusing NetSuite.com.netsuite.webservices;
using System;
using <http://System.Net|System.Net>;
using System.Xml;
namespace NetSuite {
/// <summary>
/// This is the main class for the SuiteTalk course. All of the web service operations are to be added in this class.
/// The only operations that exist initially is a simple get request.
///
/// </summary>
class SuiteTalkCourse {
// Passport info
const string netsuiteApplicationId = "";
const string netsuiteEmail = "";
const string netsuitePassword = "";
const string netsuiteAccountNumber = "";
const string netsuiteRoleId = "";
// Web Serices URL info
const string netsuiteAccountVersion = "2020_1";
// Token Based Authentication info
const string netsuiteConsumerKey = "";
const string netsuiteConsumerSecret = "";
const string netsuiteTokenId = "";
const string netsuiteTokenSecret = "";
// All web service operations executed against the NetSuiteService
public NetSuiteService _service;
// Object containing preferences that get generated in the Soap header of a request
private Preferences _prefs;
// Object containing search specific preferences that get generated in the Soap header of a request
private SearchPreferences _searchPreferences;
public SuiteTalkCourse()
{
/*
* Sets the application to accept all certificates coming from a secure server.
* NOTE: This line is only required if the network or application needs to communicate over HTTPS (SSL).
*/
ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };
const SecurityProtocolType Tls12 = (SecurityProtocolType)0x00000c00;
ServicePointManager.SecurityProtocol = Tls12;
// All web service operations execute against the _service field
_service = new NetSuiteService();
// Added to make sure the application works regardless of the data center
setDataCenterUrl(netsuiteAccountNumber);
setPreferences(); // Set all preferences, search & regular
setPassport(); // Set the Passport information for authentication
setTokenPassport(); // Comment out the setPassport() call and uncomment this to use tokens for logging in.
}
[STAThread] // .Net attribute that indicates single threading. KEEP!
static void Main(string[] args)
{
/*
* Creates an instance of the class. The Constructor is executed which creates the _service field,
* sets preferences, etc.
*/
SuiteTalkCourse ns = new SuiteTalkCourse();
/*
* A try-catch block is set up with appropriate exception handling for various
* .Net specific system and web service level exceptions.
* As the course progresses you will find that many NetSuite web service errors are captured in
* a status object, and do not generate exceptions.
*/
try
{
/*
* Here is an example web service operation to get a vendor. Create new methods as needed as
* you go through the course. In general you will create a new method for each exercise, though
* some exercises build upon the code of a previous exercise. Execute your method here, as is the
* case with the getVendor example. Feel free to comment out existing methods so that you're
* executing only what supports a specific exercise.
*/
ns.getVendor();
// Spacer line added for formatting.
Console.WriteLine("");
}
catch (System.Web.Services.Protocols.SoapException ex) {
// Get the fault type. It's the only child element of the detail element.
string fault = ex.Detail.FirstChild.Name;
string code = null;
string message = null;
// Get the list of child elements of the fault type element.
// It should include the code and message elements
System.Collections.IEnumerator ienum = ex.Detail.FirstChild.ChildNodes.GetEnumerator();
while (ienum.MoveNext()) {
XmlNode node = (XmlNode)ienum.Current;
if (node.Name == "code") {
code = node.InnerText;
} else if (node.Name == "message") {
message = node.InnerText;
}
}
Console.WriteLine("\n*** SOAP FAULT: fault type={0} with code={1}. {2}", fault, code, message);
}
catch (System.Net.WebException ex) {
Console.WriteLine("[SOAP Fault Web Exception]: {0}", ex.Message);
}
catch (System.InvalidOperationException ex) {
Console.WriteLine("[SOAP Fault Invalid Operation Exception]: {0}", ex.Message);
}
catch (System.Exception ex) {
Console.WriteLine("[Error]: {0}", ex.Message);
}
}
/// <summary>
/// Set the Data Center Url based on where the account is stored.
/// </summary>
/// <param name="accountId"></param>
private void setDataCenterUrl(String accountId)
{
DataCenterUrls dataCenterUrls = _service.getDataCenterUrls(accountId).dataCenterUrls;
String webServiceUrl = dataCenterUrls.webservicesDomain;
_service.Url = webServiceUrl + "/services/NetSuitePort_" + netsuiteAccountVersion + "?c=" + accountId;
}
/// <summary>
/// Sets the authentication information needed to connect to NetSuite
/// </summary>
private void setPassport() {
// Replace text in the next lines as noted
_service.applicationInfo = new ApplicationInfo {
applicationId = netsuiteApplicationId
};
_service.passport = new Passport {
email = netsuiteEmail,
password = netsuitePassword,
account = netsuiteAccountNumber,
role = new RecordRef { internalId = netsuiteRoleId }
};
// Display the login information in the console
Console.WriteLine("Login info...");
Console.WriteLine("\tEmail : {0}", _service.passport.email);
Console.WriteLine("\tRole Internal ID: {0}", _service.passport.role.internalId);
Console.WriteLine("\tAccount Number : {0}", _service.passport.account);
Console.WriteLine("\tApplication ID : {0}\n", _service.applicationInfo.applicationId);
}
/// <summary>
/// This provides an alternative way of authentication by using tokens instead of
/// username and password combinations
/// </summary>
private void setTokenPassport() {
/* Replace text with the information generated by the integration and token records */
string accountId = netsuiteAccountNumber;
string consumerKey = netsuiteConsumerKey;
string consumerSecret = netsuiteConsumerSecret;
string tokenId = netsuiteTokenId;
string tokenSecret = netsuiteTokenSecret;
_service.tokenPassport = WsHelper.generateTokenPassport(accountId, consumerKey, consumerSecret, tokenId, tokenSecret);
}
/// <summary>
/// This method builds the Pereferences and SearchPreferences in the SOAP header.
/// Any preferences entered here at the request level override preferences at the
/// company-level. Company level preferences can be set at
/// Setup > Integration > Web Services Preferences
/// </summary>
private void setPreferences() {
/* Set objects that contain request-level preferences */
_service.preferences = new Preferences {
// Add your Web Service Preference Settings here.
};
_service.searchPreferences = new SearchPreferences {
// Add your Search Preferences here.
};
}
/// <summary>
/// Sample Get operation call
/// </summary>
private void getVendor() {
RecordRef vendorRef = new RecordRef {
internalId = "211",
type = RecordType.vendor,
typeSpecified = true
};
_service.get(vendorRef);
}
}
}
Suite RK
02/16/2022, 12:16 PMSuite RK
02/16/2022, 12:16 PMbattk
02/16/2022, 12:19 PMSuite RK
02/16/2022, 12:19 PMbattk
02/16/2022, 12:19 PMbattk
02/16/2022, 12:20 PMSuite RK
02/16/2022, 12:21 PMbattk
02/16/2022, 12:22 PMbattk
02/16/2022, 12:23 PMSuite RK
02/16/2022, 12:23 PMSuite RK
02/16/2022, 12:23 PMbattk
02/16/2022, 12:30 PMSuite RK
02/16/2022, 12:35 PMSuite RK
02/16/2022, 12:35 PMbattk
02/16/2022, 12:47 PMbattk
02/16/2022, 12:48 PMSuite RK
02/16/2022, 1:04 PMbattk
02/16/2022, 7:15 PMbattk
02/16/2022, 7:15 PMShawn Talbert
02/17/2022, 4:14 AM