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.

1-api-menu

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

2-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.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).

3-account-objects

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

4-opportunity-fields

5-opportunity-childrelationships

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.

6-query-results

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

Automate your SalesForce SSIS Staging Packages with C#

This week I’ve been making some changes in one of my data warehouse projects, with the aim of making it a long lived and easy to maintain solution.

One of the big changes that I made was the way I was staging the data from my different sources, and this meant re-working a whole bunch of SSIS packages. Specifically there were a bunch that pull data from SalesForce which needed to be cleaned up.

So I got thinking, “all of these staging tables are loaded the same way, surely I should be able to automate this”.

In this post I’m going to show you a tool that I wrote in C# which connects to SalesForce, downloads metadata on all your objects, then generates staging tables and finally SSIS packages to load these tables.

The Manual Process

As I was re-working my packages, I was also recreating a lot of the staging tables. This was a very tedious process, as I had to go back and forth looking at the mapping of SalesForce types to their counterpart in SQL Server.

After creating the staging tables, the process of loading them from SalesForce is always the same:

  1. Truncate staging table
  2. Populate staging table with records from SalesForce (for a subset of column)

Down the line I have plans to extend this, so I’m only loading changed objects from SalesForce, which would result in the following process:

  1. Truncate staging table
  2. Lookup (from a meta data store) the last date loaded
  3. Populate staging table with records from SalesForce with CreatedDate or LastModified after the last date loaded
  4. Update the last data loaded value

The Solution

I designed a simple application in C#, this is what it looks like:

1-the-appThe example shown here is looking at the Opportunity object with a number of columns selected.

Clicking Generate Table DDL will pop up the following:

2-table-ddlAnd clicking Yes will then create the object in your database.

Clicking Generate Package will create the SSIS package which follows the above design pattern:

3-package-control-flow 4-package-data-flow

(I’m using the SalesForce Source from PragmaticWorks)

When it comes to extending the design pattern to only load new and/or changed objects all I will need to do is update my package generator and re-create the packages. Saving me the hassle of going through each package and making the change.

Future Plans

The application needs a bit of a tidy up before I can make it available for download, it also uses the PragmaticWorks assemblies for SSIS so I need to find out if I can bundle them for download.

It was an interesting exercise building this application so I’m planning on writing a few posts on how to programmatically build SSIS packages with C#.