In this example, we are going to consume the enterprise WSDL of the Salesforce in C# application.
Log into the Salesforce and navigate to “Your Name | Setup | App Setup | Develop | API” and select the “Generate Enterprise WSDL”. Copy the URL, as we will need this in our C# Application.
Now Open Visual Studio (I am using Visual Studio 2010) and create new Windows Application. The UI of the application should look like:
Right click on the “References“ folder and select “Add Service Reference“.
Now select the “Advanced“ and then select “Add Web Reference…“
Provide the URL of the generated WSDL, which we have discussed above in this tutorial.
It will prompt you for the username and password of salesforce, so provide it. After validation, it will display you below screen. Provide ” the web reference name” which will be used in program. In this case, the web reference name is “SFDC_Enterprise_WSDL“.
Add the following code in c# application:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Web.Services.Protocols; using SFDCWebServiceExample.SFDCCustom; using SFDCWebServiceExample.SFDC_Enterprise_WSDL; namespace SFDCWebServiceExample { public partial class Form1 : Form { private SforceService binding; private LoginResult lr; private SaveExpenditureWebServiceService myBinding = new SaveExpenditureWebServiceService(); public Form1() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { if (login()) { describeGlobal(); } } private bool login() { try { binding = new SforceService(); binding.Timeout = 6000; lr = binding.login(txtUserName.Text, txtPwd.Text); return true; } catch (SoapException e) { MessageBox.Show(e.Message); } return false; } private void describeGlobal() { binding.Url = lr.serverUrl; #region[Consume Enterprise webservice] binding.SessionHeaderValue = new SFDC_Enterprise_WSDL.SessionHeader(); binding.SessionHeaderValue.sessionId = lr.sessionId; #endregion //describeGlobal returns an array of object results that //includes the object names that are available to the logged-in user DescribeGlobalResult dgr = binding.describeGlobal(); //Loop through the array echoing the object names to the console StringBuilder sb = new StringBuilder(); for (int i = 0; i < dgr.sobjects.Length; i++) { sb.Append(dgr.sobjects[i].name+" , "); } MessageBox.Show(sb.ToString()); } } }
Note :
After Successful login, set the “SessionHeaderValue” and “SessionHeaderValue.sessionId”, In this program, we are displaying the comma separated name of all the available objects available to logged in user.
You can download the source code in next tutorial.
In Next tutorial we will discuss on creating the custom web service in salesforce and consuming it in .Net application using c#.
Leave a Reply