Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- .NET Development
- :
- C# Console App -> Force.com REST API
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
C# Console App -> Force.com REST API
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
09-07-2012 12:15 PM
I'm new to SFDC Development and Cloud Development in general. Could someone please point me to a simple example of a vs2010 c# console app that authenticates to my SFDC Developer account, then uses the Force.com REST API to query some data from one of my custom objects. I'm not a JAVA guy. Finding this type of an example has been difficult. I've created both a User Token and a Remote Access entry in my SFDC developer account. What do I need to install on my machine to be able to access SFDC from vs2010 projects. In one of the c# examples i've found, the project had a WEB REFERENCE to apex and apex metadata. Under the .NET 4 Framework, I don't even have an option to add a WEB REFERENCE. I noticed if I change my project settings to .NET 2.0, I do have the option of adding a WEB REFERENCE. So, in VS2010, C#, how do I get what I need for developement/connectivity to salesforce. Thanks!
Re: C# Console App -> Force.com REST API
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-14-2012 12:27 AM
Hi tjn11,
I don't know if it is a big help, but here is my solution how i connect to salesforce via OAuth and then manage objects via the REST API.
(You can find a lot of information about OAuth and salesforce here)
First you have to authenticate yourself on the authorization server with your "Consumer Key" and your "Redirect URI" (both have to be absolutely identical with the values in the Remote Access definition).
I developed an excel integration, so in my implementation it looked like this:
var authURI = new StringBuilder();
authURI.Append(salesforceAuthorizationEndpoint);
authURI.Append("response_type=code");
authURI.Append("&client_id=" + consumerKey);
authURI.Append("&redirect_uri=" + redirectUri);
Globals.WorksheetFunctions.requestBrowser.Navigate (authURI.ToString());After calling this address in the browser you see the salesforce login page, where you log in with your sf credentials.
If the authentication was successfull, you will be redirected to your redirect uri with additional informationen added as key value pairs in the uri.
First you have to retrieve the authorization code from the uri.
With this and some other values you generate a post request:
(If you don't know how to peform a webrequest see this page)
To do so, I did something like this:
var sb = new StringBuilder();
keyValues.Add("code", authorizationCode);
keyValues.Add("grant_type", "authorization_code");
keyValues.Add("client_id", ConsumerKey);
keyValues.Add("client_secret", ConsumerSecret);
keyValues.Add("redirect_uri", redirectUri);
foreach (KeyValuePair<string, string> pair in keyValues)
sb.AppendFormat("{0}={1}&", pair.Key, pair.Value);In the Response Stream you get a lot of information, e.g. "access_token" and "instance_url".
With this information you can manage objects in salesforce.
As an example the next code snippet shows the way how you perform an upsert for a user object.
User/Username/test@yourwebsite.com/", instance_name);
This will be your Request Uri and don't forget to add the header attribute (key: "Authorization", value: "Bearer " + access_token).
The data in the request body has to be in JSON format (see JSON website).
Inform yourself about the required fields before you do that and watch the repsonse/exception after your request for good error handling.
It is also possible to do a SOQL request:
In this case you just have to use the same uri start, but append "query/":
var requestUri = string.Format("{0}/services/data/v26.0/query/?q={1 }, instance_name, soqlQuery)and the SOQL statement, where spaces are replaced by the '+' symbol.
I hope this will help you a bit.
Re: C# Console App -> Force.com REST API
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
11-30-2012 05:10 AM
To answer part of your question regarding how to add a web reference in VS2010, you first need to select "Add Service Reference..." from the Project menu, on the bottom left of the "Add Service Reference" dialog you will see an "Advanced" button, click it, in the advanced options, again at bottom left in the "Compatability" area you will see the button to "Add Web Reference", this effectively is how you would do it in VS2010 / .NET 4 targetted project
Re: C# Console App -> Force.com REST API
[ Edited ]- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-05-2012 11:36 PM - edited 12-05-2012 11:37 PM
Hello mnkn,
I am finding it difficult to get any proper code to get sales force data api in asp.net..it seems you might have done it...
can you please share code/info you might have in this regard.
Your help on this will be highly appreciated.

