1/31/2015 9:37:20 PM

The following code connects to a Salesforce account using the Salesforce WSDL Web Service. This example assumes you have added the Salesforce Service reference and names that reference MySalesforce.

    //get response values
    string sessionId = "";
    string serverUrl = "";

    //use default binding and address from app.config
    using (Web_MVC_5.MySalesforce.SoapClient loginClient = new Web_MVC_5.MySalesforce.SoapClient("Soap"))
        //set account password and account token variables
        string username = "myuser@example.com";
        string password = "MyExamplePassword";
        string securityToken = "MToken";

        //call Login operation from Enterprise WSDL
        Web_MVC_5.MySalesforce.LoginResult result = loginClient.login(null, username, password + securityToken);

        //get response values
        sessionId = result.sessionId;
        serverUrl = result.serverUrl;

        //print response values
        System.Diagnostics.Debug.WriteLine(string.Format("The session ID is {0} and server URL is {1}", sessionId, serverUrl));       

    //set query endpoint to value returned by login request
    System.ServiceModel.EndpointAddress apiAddr = new System.ServiceModel.EndpointAddress(serverUrl);

    //instantiate session header object and set session id
    Web_MVC_5.MySalesforce.SessionHeader header = new Web_MVC_5.MySalesforce.SessionHeader();
    header.sessionId = sessionId;

    //create service client to call API endpoint
    using (Web_MVC_5.MySalesforce.SoapClient queryClient = new Web_MVC_5.MySalesforce.SoapClient("Soap", apiAddr))
        //create SOQL query statement
        string query = "";

		//query by email
        query = "SELECT Id, Name FROM Contact where email = 'test@example.com'";
		//query by contactId
        query = "SELECT Id, Name FROM Contact where Id = 'A_SALESFORCE_CONTACTID'";

        Web_MVC_5.MySalesforce.QueryResult queryResult = null;

            header, //sessionheader
            null, //queryoptions
            null, //mruheader
            null, //packageversionheader
            query, //SOQL query
            out queryResult
        if (queryResult.records != null)
            foreach (var record in queryResult.records)
                var contact = (Web_MVC_5.MySalesforce.Contact)record;
                System.Diagnostics.Debug.WriteLine(string.Format("Contact Name: {0}", contact.Name));