Consume Salesforce Web service in C# .Net Application

Author posted by Jitendra on Posted on under category Categories c#, Salesforce and tagged as Tags , with 14 Comments on Consume Salesforce Web service in C# .Net Application

This tutorial is the demonstration of how to consume the web service from salesforce.

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:

C# Application to consume salesforce webservice
C# Application to consume salesforce webservice

Right click on the “References“ folder and select “Add Service Reference“.

Add Service Reference in C#
Add Service Reference in C#

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 Web reference in C#
Add Web reference in C#

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#.

Related posts

  • Pingback: Create a custom Web service in Salesforce and consume it in C#.Net application | Shiva Blog()

  • Shailendra Jain

    I want to create a common web service which access any salesforce server wsdl on runtime and access its custom objects to perform operation.

    • Hi Shailendra,
      You can try this example. Give the username and password of any organization and try.

      Regards,
      Jitendra Zaa

  • Shailendra Jain

    Hi Jitendra
    When I pass username and password or any organization.
    it connect but i can’t access wsdl of that organization to access its custom objects because every organization have its custom objects.
    Basically i want to add web reference of wsdl on run time.

    • Hi Shailendra,
      I have not tried this and i think it would not be straight forward.
      You can easily bind webservices – WSDL dynamically in c#, However to get the web reference of salesforce enterprise wsdl, we have to supply the credentials of the organizations.
      I will try to find out the solution, in mean while if you get the answer please post here so that others can get benefited.

      Regards,
      Jitendra Zaa

    • Hi Shailendra,
      Instead of Enterprise WSDL, use the Partner WSDL.
      It would work in this scenario.

      Regards,
      Jitendra Zaa

  • BC

    Hi All,

    I have to use BULK API functionality of Salesforce in C#. Just want to know first which WSDL should I use Enterprise one or the Partner one? I have downloaded Salesforce.com Partner Web Services API Version 24.0 but it seems it doesn’t have those required parameters what I am looking for.

    Please guide.

    -Regards,
    BC

    • JitendraZaa

      Hi,
      Partner WSDL is very generic, give a try to Enterprise WSDL however it is tightly coupled and have its own disadvantages.

  • Ishwar2443

    Hi I am not able to access WSDL
    Getting error like  related to SSL /TSL

  • harrypop73

    Hi,

    This is talking about inserting data into salesforce. Do you have an example to push data from salesforce (to Biztalk)? I need to push data out for each Case insert/update.

    Thanks

  • Sandeep verma

    Hello guys I have to consumes partner wsdl in Asp.net application. Can you help me …!!

  • Puja

    Hi Jitendra,
    I am new in webservice.Please guide me on same.I want to create webservice in apex.And I want to the depth od the webservices.
    Thanks

  • devesh

    i create a webservice in salesforce and consuming in .net same as above mentioned but i am getting invalid session id issue while consuming..Authorization purpose i am using enterprise.wsdl

  • gobi nath

    how to use this by using classic asp