Pages

Showing posts with label salesforce Integration. Show all posts
Showing posts with label salesforce Integration. Show all posts

Sunday, 30 October 2011

How to Integrate Salesforce with .Net using C# ?

Hello friends ,


The most interesting and promising feature of Salesforce is the ease of Integration with any platform. You can find many integration Docs over the web. However , many times in discussion boards and in various blogs I have gone through the requirement where in users want to start up with Integration using c# code. In Salesforce official docs you can find sample code for Integration with .Net using VB.Net.

I am providing c# code, in case anyone needs it .

Code for .cs file :

   using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using sforce;
using getLeadInfo;

public partial class _Default : System.Web.UI.Page
{
    private string _userid = "niketsoral1@gmail.com";
    private string _password = "Google@1235et6wJW9TzsyCa3JqnVUoW7AU";
    private string _sessionId;
    private string _serverUrl;
    private string _leadEmail;
    private DateTime _nextLoginTime;


    protected void Page_Load(object sender, EventArgs e)
    {


    }

    protected void btn_Click(object sender, EventArgs e)
    {
        //       ' Confirm Lead Email Exists
        _leadEmail = txtEmail.Text;
        if(Application["_sessionId"] == null)
            Application["_sessionId"] = "";
        if (Application["_serverUrl"] == null)
            Application["_serverUrl"] = "";
        if (Application["_nextLoginTime"] == null)
            Application["_nextLoginTime"] = " #1/1/2000#";
        if (!isConnected())
            getSessionInfo();
        //       ' Call getLeadInfo Web Service

        getLeadInfoService getLeadInfo = new getLeadInfoService();
        getLeadInfo.SessionHeaderValue = new getLeadInfo.SessionHeader();
        getLeadInfo.SessionHeaderValue.sessionId=_sessionId;

        getLeadInfo.Lead getLeadInfoResponse;
        getLeadInfoResponse = getLeadInfo.getLeadAddressByEmail(_leadEmail);
        txtAddress.Text = getLeadInfoResponse.City.ToString();
        txtState.Text = getLeadInfoResponse.State.ToString();
        txtZip.Text = getLeadInfoResponse.PostalCode.ToString();

    }

    public Boolean isConnected()
    {
        if (_sessionId != "" & _sessionId != null)
        {
            if (DateTime.Now > _nextLoginTime)
                return false;
            else
                return true;
        }
        else
            return false;
    }

    public void getSessionInfo()
    {
        sforce.LoginResult lr;

        sforce.SforceService ss = new sforce.SforceService();
        lr = ss.login(_userid,_password);
        _sessionId = lr.sessionId;
        Application["_sessionId"] = lr.sessionId;
        _serverUrl = lr.serverUrl;
        Application["_serverUrl"] = lr.serverUrl;
        Application["_nextLoginTime"] = DateTime.Now;


    }
}



Code for .aspx Page :

<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>
                        Email:
                    </td>
                    <td>
                        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        Address:
                    </td>
                    <td>
                        <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        City:
                    </td>
                    <td>
                        <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        State:
                    </td>
                    <td>
                        <asp:TextBox ID="txtState" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        ZIP:
                    </td>
                    <td>
                        <asp:TextBox ID="txtZip" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <asp:Button ID="btn" runat="server" Text="Click" OnClick="btn_Click" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>


Note :- In order to use this code, you need to do some homework :
  1. Create web service method in your salesforce Org as mentioned in Salesforce official docs.
  2. Generate WSDL for you Salesforce Org an import it in your .Net application.
  3. Use the reference in the .cs file.
Hope this helps the new comers who are starting Integration using c#.

Cheers,
Niket
Certified Developer | Certified Administrator