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.
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.
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:
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:
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:
This snippet will list details on each of the fields within the object and any child relationships that have been defined.
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”.
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!)
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.