Accessing SalesForce using the Partner API and C#

SalesForce exposes a number of WebService APIs which can be used to access objects in an organization (see here for details). In this post I’m going to show how from a C# application you can use the Partner API.

The Partner API is designed for those who want to develop applications which are agnostic to the objects in a given SalesForce account. This means that it will work for multiple organizations, which is in contrast to the Enterprise API which only works for a single organization and gives a strongly typed interface into that single organization.

I used the Partner to build an application which dynamically generates SQL Server Integration Services packages based on SalesForce meta data. You can read more about it here.

This post is for other people who are interested in accessing SalesForce via C# to retrieve metadata about objects and perform queries.

Getting Setup

If you don’t have access to SalesForce you can sign up for a developer account at

Once you have an account, sign in to SalesForce and click on Setup in the top left.

Then expand the Develop menu and click API.


This will then show a list of API WSDLs that can be downloaded. The one that we are interested in is the Partner API.


Right click on the Generate Partner WSDL link and save it to your computer as PartnerAPI.xml

Open Visual Studio and create a new Console application, I’ve called mine SalesForcePartnerDemo.

Right click on your project and select Add Service Reference, then click Advanced, then click Add Web Reference.

This will bring up the Add Web Reference window, in the URL text box enter the file path for the Partner API WSDL file that you downloaded, then give it the name SFPartnerAPI then click Add Reference.

Visual Studio will then generate code which can be used to access the API as well as provide some classes which we can use.

Logging Into SalesForce

To login to SalesForce you will need your email address, password and the security token. (Resetting your Security Token)

The follow code snippet will perform the login action:

SFPartnerAPI.SforceService partnerApi = new SFPartnerAPI.SforceService();

SFPartnerAPI.LoginResult loginResult = partnerApi.login("emailaddress", "passwordsecuritytoken");

partnerApi.SessionHeaderValue = new SFPartnerAPI.SessionHeader();
partnerApi.SessionHeaderValue.sessionId = loginResult.sessionId;
partnerApi.Url = loginResult.serverUrl;

When users are using this application they will need to enter their username and then their password combined with the security token.

Listing SalesForce Objects

The next thing to do is to list all of the objects that are in the logged in users SalesForce account:

var accountObjects = partnerApi.describeGlobal();

foreach (var item in accountObjects.sobjects)
  Console.WriteLine(string.Format("{0} ({1})",, item.label));

This snippet will retrieve all of the objects in SalesForce and write to the console the underlying name of the object and the label (or display name).


Retrieving meta data about a specific object is not much more complicated:

var opportunityObject = partnerApi.describeSObject("Opportunity");

foreach (var item in opportunityObject.fields)
  Console.WriteLine(string.Format("{0} ({1}) - {2}",, item.label, item.type));

foreach (var item in opportunityObject.childRelationships)
  Console.WriteLine(string.Format("{0}", item.relationshipName));

This snippet will list details on each of the fields within the object and any child relationships that have been defined.



Querying Objects

The final thing to do is to write a query and see the result. For my example I’m going to query the Opportunity object and return all of the objects that are “Won”.

string query = "SELECT Id, Name, StageName, Amount FROM Opportunity WHERE IsWon = True";

var queryResult = partnerApi.query(query);

foreach (var item in queryResult.records)
  Console.WriteLine(string.Format("Id: {0}, Name: {1}, StageName: {2}, Amount: {3}", item.Any[0].InnerText, item.Any[1].InnerText, item.Any[2].InnerText, item.Any[3].InnerText));

Issuing queries is very straightforward once you know what column names to use, that is where retrieving the meta data of an object comes in handy.


(NB: This is data from my developer account – not real!)

Wrap Up

As you can see using the Partner API is very straightforward and exposes everything that you need to get started building data driven applications which access SalesForce.

7 thoughts on “Accessing SalesForce using the Partner API and C#”

  1. Cannot implicitly convert type ‘SalesForcePartnerDemo.SFPartnerAPI.ListViewRecordColumn’ to ‘SalesForcePartnerDemo.SFPartnerAPI.ListViewRecordColumn[]’

  2. Really nice, good explained, thanks a lot.
    I got two errors.
    1.{“Unable to generate a temporary class (result=1).\r\nerror CS0029: Cannot implicitly convert type ‘SalesforceSample.SFPartnerAPI.ListViewRecordColumn’ to ‘SalesforceSample.SFPartnerAPI.ListViewRecordColumn[]’\r\nerror CS0029: Cannot implicitly convert type ‘SalesforceSample.SFPartnerAPI.ListViewRecordColumn’ to ‘SalesforceSample.SFPartnerAPI.ListViewRecordColumn[]’\r\n”}
    2.{“The request was aborted: Could not create SSL/TLS secure channel.”}

    I fixing both errors.
    1.For some reason SalesforceSample.SFPartnerAPI.ListViewRecordColumn[][] was changing it to SalesforceSample.SFPartnerAPI.ListViewRecordColumn[] solved the issue.
    2. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; add this line before LoginResult loginResult = partnerApi.login(username, password);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s