Establishing a Salesforce Session

The SalesforceSession object provides access to the Partner API for the connected Salesforce organization.

IMPORTANT: Salesforce sessions expire according to the security settings in each instance. The constructors based on stored credentials can automatically re-establish an active session if required.

IMPORTANT: FuseIT recommended session instances be accessed using the Singleton pattern - see below

Basic username and password


SalesforceSession salesforceSession = new SalesforceSession(new LoginDetails("username@example.com", "password"), autoReestablishSession: true);


Including the Security Token


SalesforceSession salesforceSession = new SalesforceSession(new LoginDetails("username@example.com", "password", "abcdefghijklmnopqrstuvwxyz"), autoReestablishSession: true);


Specifying the Salesforce environment (BindingEnvironment)

You can specify a BindingEnvironment in the LoginDetails to connect to a Salesforce Sandbox or Pre release environment. By default the session will connect to production and developer edition orgs.


SalesforceSession salesforceSession = new SalesforceSession(new LoginDetails("username@example.com","password", "abcdefghijklmnopqrstuvwxyz", LoginDetails.BindingEnvironment.Sandbox), autoReestablishSession: true);


Using a connection string


SalesforceSession salesforceSession = new SalesforceSession("connectionStringName");


The connection string should have the "user id" and "password" specified. The security token can be appended to the password or specified as the "token" if required. The binding environment can be optionally specified using the "environment" prefix.


  <connectionStrings>
    <add name="connectionStringName" connectionString="S4S:user id=username@example.com;password=password;token=3VSampleYeZmgcwbozdKkSm;environment=Production"/>
  </connectionStrings>
Note: If you encrypt the connection strings with ASPNET_REGIIS you do not need to change the calling code.

Using the Singleton Pattern to improve Performance

Establishing a new Salesforce session takes several API calls to establish user and org details plus to cache metadata about the objects and fields that are accessible in the org.  This overhead is not an issue but creating new session instances with each page can degrade the overall performance. When getting the SalesforceSession instance it is usually advantageous to use a singleton implementation.


/// 
/// Singleton Pattern returns a SalesforceSession instance created with connectionStringName connection string
/// 
public class SitecoreSalesforceSessionSingleton : SalesforceSessionSingleton
{
	private SitecoreSalesforceSessionSingleton()
		: base(null, "connectionStringName")
	{
	}
 
	/// 
	/// Get the singleton instance of the Salesforce Session.
	/// 
	public static SalesforceSession SessionInstance
	{
		get
		{
			SitecoreSalesforceSessionSingleton singleton = new SitecoreSalesforceSessionSingleton();
			return singleton.Instance;
		}
		set
		{
			SitecoreSalesforceSessionSingleton singleton = new SitecoreSalesforceSessionSingleton();
			singleton.SetNewInstance(value);
		}
	}
}


When you need a SalesforceSession instance use the following call:
SalesforceSession salesforceSession = SitecoreSalesforceSessionSingleton.SessionInstance;


Even then, because the default application pool session time-out is 20 minutes, sessions will expire meaning the next visitor will take a performance hit. To mitigate this you could increase the timeout in web.config and set the application pool’s worker process default idle timeout as described here - http://asp-net.vexedlogic.com/2012/05/23/aspasp-net-session-timeout-how-do-i-change-it/. This will, at least let you extend the refresh duration to what you want. You may also need to change the Session Storage Mode described in the same article.  

 Another approach is the KeepAlive page (http://sitecorebasics.wordpress.com/2011/06/21/basics-of-keepalive/ from Sitecore). With the Sitecore UrlAgent you can keep the SalesForceSession alive in the keepalive.aspx. The KeepAlive agent is set to 1 hour by default.  A cleaner solution would be to create a new ASPX page just for maintaining the SalesForceSession instead of modifying the Sitecore KeepAlive.aspx page.  Then add a new URLAgent that calls that new web page on some frequency.