Consume Salesforce Web service in C# .Net Application

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

Posted

in

,

by

Tags:


Related Posts

Comments

15 responses to “Consume Salesforce Web service in C# .Net Application”

  1. […] to publicize, if you like this article : In Previous article, we have consumed the standard enterprise wsdl of the salesforce. In this article we will create […]

  2. Shailendra Jain Avatar
    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.

    1. Jitendra Zaa Avatar

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

      Regards,
      Jitendra Zaa

  3. Shailendra Jain Avatar
    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.

    1. Jitendra Zaa Avatar

      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

    2. Jitendra Zaa Avatar

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

      Regards,
      Jitendra Zaa

  4. BC Avatar
    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

    1. JitendraZaa Avatar
      JitendraZaa

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

  5. Ishwar2443 Avatar
    Ishwar2443

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

  6. harrypop73 Avatar
    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

  7. Sandeep verma Avatar
    Sandeep verma

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

  8. Puja Avatar
    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

  9. devesh Avatar
    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

  10. gobi nath Avatar
    gobi nath

    how to use this by using classic asp

  11. Darpesh Avatar
    Darpesh

    Hi Jitendra,

    I am trying to setup Delegated Authentication for communitiies. I have followed all the steps provided on Salesforce help but I am unable to figure out which Salesforce URL to use to perform Post operation to SFDC so that my Webservice will get called.

    I have tested the Webservice that is working fine but I am not sure which URL to perform Post operation and how in which format?

    Any help, will be highly appreciated.

    Thanks,
    Darpesh

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading