Contents

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 http://developer.force.com/

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.

/accessing-salesforce-using-the-partner-api-and-c/images/1-api-menu.png

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

/accessing-salesforce-using-the-partner-api-and-c/images/2-partner-api.png

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:

1
2
3
4
5
6
7
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:

1
2
3
4
5
6
var accountObjects = partnerApi.describeGlobal();
 
foreach (var item in accountObjects.sobjects)
{
  Console.WriteLine(string.Format("{0} ({1})", item.name, 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).

/accessing-salesforce-using-the-partner-api-and-c/images/3-account-objects.png

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var opportunityObject = partnerApi.describeSObject("Opportunity");
 
foreach (var item in opportunityObject.fields)
{
  Console.WriteLine(string.Format("{0} ({1}) - {2}", item.name, 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.

/accessing-salesforce-using-the-partner-api-and-c/images/4-opportunity-fields.png

/accessing-salesforce-using-the-partner-api-and-c/images/5-opportunity-childrelationships.png

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”.

1
2
3
4
5
6
7
8
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.

/accessing-salesforce-using-the-partner-api-and-c/images/6-query-results.png

(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.

🍪 I use Disqus for comments

Because Disqus requires cookies this site doesn't automatically load comments.

I don't mind about cookies - Show me the comments from now on (and set a cookie to remember my preference)