Getting started with Salesforce and C#

I’ve been working on integrating an existing web application with Salesforce, and it was a little more work than I expected.  Some of this was down to unfamiliarity with the platform, which I’ve never used before today.  Here are some of the key steps involved and things to note:

1. The cheapest plan that offers API access is the enterprise plan.  At $125 /month /user, this seems a bit on the expensive side for small businesses, but I digress… In any case, if you’re using a different (cheaper) plan, then the API is a paid extra and not included as standard.

2. Creating a Salesforce developer account is free, and is the easiest way to start using the API.

3. Add your IP address to your account or your API access won’t work.  You can do this by logging in, and then doing the following: Click on Setup in the top right corner next to your name, expand the Security Controls section on the left hand side (in the “Administer” area), click on Network Access, then New. Enter your public IP address in the Start IP Address and End IP Address boxes. Click Save.

4. Next you need to get the URL to the WSDL file, which we’ll add to our project in Visual Studio to generate the web service proxy.  To get this, expand the Develop section on the left hand side (under the “Build” area). Then click on API. Right click on the Generate Enterprise WSDL link and copy it (it’ll be something like*).

5. Once that’s done, create a new project in Visual Studio.  For this example, let’s use a C# Console Application.  I’m assuming use of VS2010+ here (the process of adding a web reference is slightly different but easier in prior versions).

6. First we’ll need to add a reference to our WSDL from Salesforce to generate the webservice proxy. Right click on References in the Solution Explorer and select Add Service Reference. Click Advanced (bottom left on dialog box). Then Add Web Reference.   Pase the URL from step 4 above into the URL box and click the green arrow.  You’ll probably be prompted to login to Salesforce. Once that’s done, you should see the WSDL or service method summaries in the window.  On the right hand side, under “Web Reference Name”, type salesforce and click Add Reference.

7. Your project now has a reference to Salesforce, and in the solution explorer, you should see a new node titled Web References with salesforce under it, which we just created.

8. Paste the code below into your main method.  Note, you will need to change the username and password placeholders below to your details (it appears Salesforce doesn’t offer an API key option).

private static void Main(string[] args)
    Console.WriteLine("First Name?");
    var firstName = Console.ReadLine();

    Console.WriteLine("Last Name?");
    var lastName = Console.ReadLine();

    var contact = new Contact();
    contact.FirstName = firstName;
    contact.LastName = lastName;
    contact.Email = string.Concat(firstName, ".", lastName, "");

    using (SforceService sfs = new SforceService())
        var login = sfs.login("", "your-password");

        if (login.passwordExpired)
            Console.WriteLine("Salesforce password expired!");
            sfs.Url = login.serverUrl;
            sfs.SessionHeaderValue = new SessionHeader();
            sfs.SessionHeaderValue.sessionId = login.sessionId;
            var userinfo = login.userInfo;

            Console.WriteLine("Logged in as {0}", userinfo.userFullName);

            var sr = sfs.create(new sObject[] { contact })[0];

            var result = sr.success;


            if (!result)
                Console.WriteLine(string.Join(", ", sr.errors.Select(x => x.message)));

            Console.WriteLine(result ? "Created" : "Not Created");


9. This code sample will prompt you to enter a first name and last name, and will create a new contact with these details.

10. To view the newly created contact, click on the “Contacts” tab in the Salesforce web interface. Choose “All Contacts” from the View dropdown and click Go. Your new contact created from the command line application should be in the list.

This is far from a definitive guide, but should hopefully help anyone new to Salesforce overcome some of the initial hurdles in getting started with the API using .NET.

2 thoughts on “Getting started with Salesforce and C#

  1. thanks Mun. This is now working for me. I had a pretty good app with (using VS 2013) joining in Salesforce/outlook/Excell so that I can distribute reports, but I had not yet built similar functionality with C#. .

    The only change I had to make was that I had to reference the service in the salesforce calls, ie instead of just saying SObject or Contact, I had to write it service.SObject and service.Contact

Leave a Reply

Your email address will not be published. Required fields are marked *