Using SalesForce.com in your C# application
We are working on some cool app, that is handling tasks and meetings a little bit different and more contextual. You can check that on mativy.com and try it out.
Anyway, basic idea is also, that all tasks in organization (or team) is handled on one place, so we needed importing from different services. Today I’ll show you how to get all tasks from SalesForce.com account.
If you are not aware, SalesForce.com is one of the leading online CRM solutions used by many big companies (Starbucks, Qualcomm, Siemens, etc.), so if you are developing software like ours, you need to take into account those “big guys”. Anyway, SalesForce.com is offering huge development platform, called Force.com (flashback: Luke, use The Force) which offers API access to all of the features of SalesForce.com.
First thing, you need to create a developer account on developer.force.com, which brings you to fully functional interface, but meant only for testing purposes. You need to obtain security token via: Setup > Personal Information > Security token and save it somewhere, you’ll need it later.
Second thing, go to App Setup > Develop > API and download Enterprise WSDL (right click on Generate Enterprise WSDL).
Third thing, generate some test data (in this example, we need some tasks).
Now open Visual Studio, create one project (I did a simple Console App, just for demo) and put downloaded WSDL file into solution folder.
After opening solution, add a web reference (right click on References > Add Service Reference > Advanced > Add Web Reference) to this WSDL file (enter physical path to file). You’ll get some parsing error, saying that this is not valid WSDL file, but just ignore that and click Add Reference.
That’s it with clicking part, now let’s digg into code:
SforceService binding = new SforceService(); binding.Timeout = 60000; string un = "your@email.com", pw = "YourPassword", token = "security_token"; LoginResult loginResult = binding.login(un, pw + token); binding.Url = loginResult.serverUrl; binding.SessionHeaderValue = new SessionHeader(); binding.SessionHeaderValue.sessionId = loginResult.sessionId; GetUserInfoResult userInfo = loginResult.userInfo;
This is used for logging into SalesForce API with your developer account and getting some basic info about your account. When we’re logged in, we can start getting all the data we need. In this example, we’ll just get all the tasks and select only few properties (as you can see in example below, we took AccountId, Description and Subject )of Task object. All available properties and functions of Task object can be found in API documentation.
For querying data you need to use SOQL (SalesForce Object Query Language), which is actually stripped down version of normal SQL.
QueryResult qr = null; binding.QueryOptionsValue = new QueryOptions(); binding.QueryOptionsValue.batchSize = 3; binding.QueryOptionsValue.batchSizeSpecified = true; qr = binding.query("select AccountId, Description, Subject from Task"); for (int i = 0; i < qr.size; i++) { Console.WriteLine((qr.records[i] as Task).Subject); }
That’s it! Very simple and straight forward solution, so if you need to access any data from salesforce.com, that’s the way you can do it in C#.
You can download the whole demo project here on my skydrive.
| Tweet |
author: Aleš Rosina | Comments: 2 | Tags: salesforce, c-sharp, demo, visual-studio
October
2010
It's quite useful post.
thnx..!!
Numerous many thanks for taking this prospect to speak about this, I experience strongly about this and I get satisfaction in obtaining out about this make a difference.


