Programing QA

Programing/Developing Questions & Answers. Focus on business application material

Connect to Salesforce partners WebServices (WSDL) Using Visual Studio 2010/2012/2013 & C#. This is easy DOT.NET 2.0 style example

Step 1:

Logon to Salesforce and download the Partners WSDL and save it to your HD. You can get it from “Setup—>Develop—>API”. Make sure to click “save link as”. open the link and then save it will result corrupted WSDL file.

image_thumb[6]

Step 2:

Add reference to the WSDL – DOT.NET 2.0 Style: Right click on the project and choose “Add service reference”. Then click on “Advance”, The click on “Add Web Reference”. Put the location od the WSDL File you downloaded, write the namespace and click “Add Reference”.

image

Step 3:

Write this code to communicate with salesforce “the easy way”.

Code Snippet
  1. // Create instance to the SforceService which will use to communicate with salesforce.
  2. SFWR.SforceService service = new SFWR.SforceService();
  3.  
  4. // Use this URL for production. You can change API version.
  5. service.Url = "https://login.salesforce.com/services/Soap/u/29.0";
  6. // User this URL for sandbox.
  7. //service.Url = "https://test.salesforce.com/services/Soap/u/29.0";
  8.  
  9. // Login using you username and password combine with token at the end.
  10. SFWR.LoginResult lr = service.login("moshe@83demo.com", "pass@word3dv7q9CixQZiQzA7kuHb4DLBb");
  11.  
  12. // Initilize the paramters you got in the LoginResult in the service itself.
  13. service.Url = lr.serverUrl; // Setup the right URL.
  14. service.SessionHeaderValue = new SFWR.SessionHeader();
  15. service.SessionHeaderValue.sessionId = lr.sessionId; // Setup the session id.
  16.  
  17. // Now we can do what ever we want - query salesforce and execute any message we want against the salesforce.
  18. SFWR.QueryResult result = service.query("select id from account");

Connect to Salesforce partners WebServices (WSDL) Using Visual Studio 2010/2012/2013 & C#. This is complex WCF example. look for the “DOT.NET 2.0 Style” for easier way.

Step 1:

Logon to Salesforce and download the Partners WSDL and save it to your HD. You can get it from “Setup—>Develop—>API”. Make sure to click “save link as”. open the link and then save it will result corrupted WSDL file.

image

Step 2:

Add reference to the new WSDL inside your project in Visual Studio using “Add new service reference”.

image

Step 3:

Write the actual code to query the salesforce using C# and WCF.

Code Snippet
  1. // This client instance will b e use to login and get valid password.
  2. SF.SoapClient loginClient = new SF.SoapClient();
  3.  
  4. SF.LoginScopeHeader header = new SF.LoginScopeHeader(); // not needed in this demo.
  5. SF.CallOptions options = new SF.CallOptions(); // not needed in this demo.
  6.  
  7. // User this URL for production. you can change the API version id needed.
  8. loginClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("https://login.salesforce.com/services/Soap/u/29.0"); // Production
  9. // Use this URL for sandbox.
  10. //client.Endpoint.Address = new System.ServiceModel.EndpointAddress("https://test.salesforce.com/services/Soap/u/29.0"); // Sandbox
  11.  
  12. // Login details - add the token string at the end of the password
  13. SF.LoginResult response = loginClient.login(header, options, "myemail", "mypasswordmytoken");
  14.  
  15. // The object will hold the session id and required for all future communication with salesforce.
  16. SF.SessionHeader sessionHeader = new SF.SessionHeader();
  17. sessionHeader.sessionId = response.sessionId;
  18.  
  19. // Now, after we loged in and have session id - we can start new client and use it to query the salesforce
  20. SF.SoapClient executionClient = new SF.SoapClient();
  21.  
  22. // Here we give our execution client the urk we got from the login call.
  23. executionClient.Endpoint.Address = new System.ServiceModel.EndpointAddress(response.serverUrl);
  24.  
  25. // object to hold our query result
  26. SF.QueryResult result = new SF.QueryResult();
  27.  
  28. // Finaly we can query the salesforce. the result will be in the "QueryResult" object.
  29. SF.LimitInfo[] info = executionClient.queryAll(sessionHeader, options, new SF.QueryOptions(), "select id from account", out result);