function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tjn11tjn11 

C# Console App -> Force.com REST API

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!

mnknmnkn

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.

curtiskcurtisk

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

hk2012hk2012

 

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.

Jane JaniszewskiJane Janiszewski
@mnkn
I know this is an old post but how did you retrieve the access code from the uri? I understand how to do everything except that.