The Force.com Toolkit for .NET provide an easy way for .NET developers to interact with the Force.com & Chatter REST APIs using native libraries.
These toolkits are built using the Async/Await pattern for asynchronous development and .NET portable class libraries, making it easy to target multiple Microsoft platforms, including .NET 4.5, Windows Phone 8, Windows 8/8.1, and iOS/Android using Xamarin and Mono.NET.
The Common Libraries for .NET provides functionality used by the Force.com Toolkit for .NET and the Chatter Toolkit for .NET. While you can use the Common Libraries for .NET independently, it is recommended that you use it through one of the toolkits.
You can try the libraries immmediately by installing the following DeveloperForce NuGet packages.
Install-Package DeveloperForce.Force
Install-Package DeveloperForce.Chatter
The toolkit includes the following sample applications.
You can find this sample here: https://github.com/developerforce/Force.com-Toolkit-for-NET/tree/master/samples/WebServerOAuthFlow
This sample shows how you can use the Web Server OAuth Authentication Flow to authorize a user and query the Force.com API. This sample uses MVC 5 and WebAPIs to demonstrate how you can retrieve a user access token and make an AJAX call to your API to retrieve data from Force.com and bind to your page using Knockout.js.
You can find this sample here: https://github.com/developerforce/Force.com-Toolkit-for-NET/tree/master/samples/SimpleConsole
This sample shows how to write a console application that leverages the async/await paradigm of .NET 4.5 and uses the toolkit to log in to and communicate with a Salesforce organization. Useful for quick POC applications and scheduled jobs.
Currently the following operations are supported.
To access the Force.com APIs you must have a valid Access Token. Currently there are two ways to generate an Access Token: the Username-Password Authentication Flow and the Web Server Authentication Flow
The Username-Password Authentication Flow is a straightforward way to get an access token. Simply provide your consumer key, consumer secret, username, and password.
var auth = new AuthenticationClient();
await auth.UsernamePasswordAsync("YOURCONSUMERKEY", "YOURCONSUMERSECRET", "YOURUSERNAME", "YOURPASSWORD");
The Web-Server Authentication Flow requires a few additional steps but has the advantage of allowing you to authenticate your users and let them interact with the Force.com using their own access token.
First, you need to authenticate your user. You can do this by creating a URL that directs the user to the Salesforce authentication service. You'll pass along some key information, including your consumer key (which identifies your Connected App) and a callback URL to your service.
var url =
Common.FormatAuthUrl(
"https://login.salesforce.com/services/oauth2/authorize", // if using sandbox org then replace login with test
ResponseTypes.Code,
"YOURCONSUMERKEY",
HttpUtility.UrlEncode("YOURCALLBACKURL"));
After the user logs in you'll need to handle the callback and retrieve the code that is returned. Using this code, you can then request an access token.
await auth.WebServerAsync("YOURCONSUMERKEY", "YOURCONSUMERSECRET", "YOURCALLBACKURL", code);
You can see a demonstration of this in the following sample application: https://github.com/developerforce/Force.com-Toolkit-for-NET/tree/master/samples/WebServerOAuthFlow
After this completes successfully you will receive a valid Access Token and Instance URL. The Instance URL returned identifies the web service URL you'll use to call the Force.com REST APIs, passing in the Access Token. Additionally, the authentication client will return the API version number, which is used to construct a valid HTTP request.
Using this information, we can now construct our Force.com client.
var instanceUrl = auth.InstanceUrl;
var accessToken = auth.AccessToken;
var apiVersion = auth.ApiVersion;
var client = new ForceClient(instanceUrl, accessToken, apiVersion);
```
### Sample Code
Below you'll find a few examples that show how to use the toolkit.
#### Create
You can create with the following code:
```
public class Account
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
...
var account = new Account() { Name = "New Account", Description = "New Account Description" };
var id = await client.CreateAsync("Account", account);
```
You can also create with a non-strongly typed object:
```
var client = new ForceClient(_consumerKey, _consumerSecret, _username, _password);
var account = new { Name = "New Name", Description = "New Description" };
var id = await client.CreateAsync("Account", account);
```
#### Update
You can update an object:
```
var account = new Account() { Name = "New Name", Description = "New Description" };
var id = await client.CreateAsync("Account", account);
account.Name = "New Name 2";
var success = await client.UpdateAsync("Account", id, account);
```
#### Delete
You can delete an object:
```
var account = new Account() { Name = "New Name", Description = "New Description" };
var id = await client.Create("Account", account);
var success = await client.DeleteAsync("Account", id)
```
#### Query
You can query for objects:
```
public class Account
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
...
var accounts = await client.QueryAsync<Account>("SELECT id, name, description FROM Account");
foreach (var account in accounts.records)
{
Console.WriteLine(account.Name);
}
```
## Contributing to the Repository ###
If you find any issues or opportunities for improving this respository, fix them! Feel free to contribute to this project by [forking](http://help.github.com/fork-a-repo/) this repository and make changes to the content. Once you've made your changes, share them back with the community by sending a pull request. Please see [How to send pull requests](http://help.github.com/send-pull-requests/) for more information about contributing to Github projects.
## Reporting Issues ###
If you find any issues with this demo that you can't fix, feel free to report them in the [issues](https://github.com/developerforce/Force.com-Toolkit-for-NET/issues) section of this repository.