Tuesday, July 29, 2014

How to connect Salesforce from Asp.net? Salesforce Integration in Asp.net

Today I will show you how to use new salesforce toolkit for .NET Application. Follows the steps:

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

React-select is very slow on larger list - Found solution - using react-window

 I had more than 4000 items in searchable dropdownlist. I have used react-select but it was very slow. finally I found complete solution to ...