Connecting to Salesforce with SOAP API in C#
Step 1: Set up a Salesforce Developer Account
Before we dive into the code, make sure you have a Salesforce Developer Account. If you don’t have one, you can sign up for free at https://developer.salesforce.com/signup. This will provide you with the necessary credentials to authenticate and access Salesforce.
Step 2: Create a Salesforce Connected App
To connect to Salesforce using the SOAP API, we need to create a Connected App in Salesforce.
Follow these steps:
Log in to your Salesforce Developer Account.
Go to “Settings” > “My Personal Information” > “Rest My Security Token”
Click “Reset Security Token”, you will receive the security token via email
Step 3: Obtain the Salesforce WSDL
The Salesforce WSDL (Web Services Description Language) file describes the SOAP API operations and objects.
Step 4: Add a Service Reference in Visual Studio
To interact with the Salesforce SOAP API in C#, we need to add a service reference in Visual Studio.
Here’s how:
Create a new C# project in Visual Studio or open an existing one. (Important: please use .net framework instead of .net core)
Right-click on “References” in the Solution Explorer and select “Add Service Reference.”
Click on “Advanced” and then on “Add Web Reference.”
Browse to the downloaded Salesforce WSDL file and add the reference to your project.
Step 5: Write Code to Connect to Salesforce
With the Salesforce service reference added, we can now write code to connect to Salesforce and perform operations.
Here’s a sample code snippet:
// Include necessary namespaces
using System;
using System.ServiceModel;
// Create a new instance of the Salesforce API client
SforceService client = new SforceService();
// Set the Salesforce API session header (obtain session ID using authentication)
LoginResult loginResult = client.login("YOUR_USERNAME", "YOUR_PASSWORD_AND_SECURITY_TOKEN");
client.SessionHeaderValue = new SessionHeader
{
sessionValue = loginResult.sessionId
};
// Perform Salesforce API operations
// Example: query the Salesforce Contact object
QueryResult queryResult = client.query("SELECT Id, FirstName, LastName FROM Contact");
Shinetech’s scalable and dedicated development teams have a shared goal to help facilitate positive change within your business by building tailored software that will respond to your unique business problems in a timely and cost-effective manner.
When you face problems that hinder your business’ growth and development, it is always a challenge to find a reliable partner that can help you build software and sustain a long-term business relationship that can scale with your business needs. Shinetech offers end-to-end solutions, from concept and strategy, UX/UI design, implementation, integration and support, helping you achieve your business goals and boost productivity within the shortest possible timeframe.