Connecting to Salesforce with SOAP API in C#

Salesforce is a popular Customer Relationship Management (CRM) platform that provides a robust set of APIs for integrating and extending its functionality. One of the APIs available is the SOAP API, which allows developers to interact with Salesforce using the SOAP protocol. In this blog post, we will explore how to connect to Salesforce using the SOAP API in C# and perform basic operations.

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.

Here’s how to obtain it:
 
Log in to your Salesforce Developer Account.
Go to “Setup” > “Inegrations” > “API”
Click on “Generate Enterprise WSDL” and save the WSDL file to your local machine.
 
Tips: there are an issue in the WSDL file, please modify the below line as below:
				
					<complexType name="ListViewRecord">
  <sequence>
    <element name="columns" type="tns:ListViewRecordColumn" minOccurs="1" maxOccurs="unbounded"/>
  </sequence>
  <xsd:attribute name="tmp" type="xsd:string" />
</complexType>
				
			

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.

Contact us

Please fill require field.
Please fill a valid Email.
Please fill require field.
Please fill require field.
Please fill require field.