Step 1. Create a project with C# ASP.NET 4.5 empty web application.
Step 2. Right click on this project and click on “Manage NuGet Packages”. Now search for “salesforce” and install “DeveloperForce.Force” NuGet package like this: see screen.
Step 3. After installing this package, add new webform in the project. Name it “Default.aspx”
Step 4. Before I show you complete code, you need five things from salesforce: “ConsumerKey, ConsumerSecret, username, password and security token”.
Note:A security token is an automatically generated key that you must add to the end of your password in order to log into Salesforce from an untrusted network. For example, if your password is mypassword, and your security token is XXXXXXXXXX, then you must enter mypasswordXXXXXXXXXX to log in.
Step 5. Paste following code in your “Default.aspx.cs” page.
public partial class Default : System.Web.UI.Page { List<contact> Contacts; protected void Page_Load(object sender, EventArgs e) { Do().Wait(); foreach (var item in Contacts) { Response.Write(item.LastName + ","); } } async Task Do() { //get credential values string consumerkey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; string consumersecret = "XXXXXXXXXXXXXXXXX"; string username = "XXXXXXXXXXX"; string password = "mypasswordXXXXXXXXXX"; //create auth client to retrieve token var auth = new AuthenticationClient(); //get back URL and token await auth.UsernamePasswordAsync(consumerkey, consumersecret, username, password); var instanceUrl = auth.InstanceUrl; var accessToken = auth.AccessToken; var apiVersion = auth.ApiVersion; var client = new ForceClient(instanceUrl, accessToken, apiVersion); var contacts = await client.QueryAsync<contact>("SELECT Id, LastName From Contact"); Contacts = contacts.records; } public class Contact { public string Id { get; set; } public string LastName { get; set; } } }
Now, run your application and it works perfect :)
I hope this article will help you in connecting Salesforce from Asp.Net
No comments:
Post a Comment