function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
K JK J 

.NET and Soap API

I am new to SF. I Just create a C# project and trying connect to Salesforce using Soap API. I down load wsdl to my local drive and add web service.  After successfully adding web service, I can't find login method as show in sample document.  I also try to add web service, which reference partner wsdl by URL, and I can't find login method, either.  Please help. Thanks, K
Fabrice76Fabrice76
Hi,
Here is part of my code to login and logout. I use a property SrvSalesforce as parameter with all my method...so i'm always connected to do my job, even if the TimeOut is reached (I reconnect).
I translate my code in english and i removed some portion to simplify so there can be little mistakes ;-) 
 
using SF; // Web reference for API

		private static SF.SforceService fSrvSalesForce = null;

		public static string UserSFDC = "";
		public static string PasswordSFDC = ""; 
		public static string SecurityTokenSFDC = "";
		public static int TimeOutSFDC = 0;

		public static SF.SforceService SrvSalesForce
		{
			get 
			{ 
				if (fSrvSalesForce == null)
				{
					Login(UserSFDC, PasswordSFDC, SecurityTokenSFDC);
				}
				return fSrvSalesForce;
			}
			set 
			{
				fSrvSalesForce = value;
			}
		}

        public static bool Login(string aLogin, string aPassword, string aSecurityToken, int aTimeout)
        {           
            fSrvSalesForce = new SF.SforceService();

            // I also Keep parameters in public properties for other use
            UserSFDC = aLoginUtilisateur;
			PasswordSFDC = aMotDePasse;
			SecurityTokenSFDC = aJetonDeSecurite;
			TimeOutSFDC = aTimeout;

            string PWDWithSecurityToken = string.Format("{0}{1}",aPassword,aSecurityToken);

            SF.LoginResult ResultatConnexion;
            try
            {
                ResultatConnexion = fSrvSalesForce.login(aLoginUtilisateur, PWDWithSecurityToken);
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {              
                // Add your personal catch (trace,log,...)
                // ex :System.Windows.Forms.MessageBox.Show(ex.Message);

                fSrvSalesForce = null;
                return false;
            }

            if (fSrvSalesForce != null)
            {
                if (aTimeout > 0)
                {
                    fSrvSalesForce.Timeout = aTimeout;
                }
                fSrvSalesForce.Url = ResultatConnexion.serverUrl;
                fSrvSalesForce.SessionHeaderValue = new SF.SessionHeader();
                fSrvSalesForce.SessionHeaderValue.sessionId = ResultatConnexion.sessionId;
            }
            return (fSrvSalesForce != null);
        }

        public static void Logout()
        {
            try
            {                
                if (fSrvSalesForce != null)
                    fSrvSalesForce.logout();                
            }
            catch (System.Web.Services.Protocols.SoapException e)
            {
                // Do your personal catch
            }
            finally
            { 
                fSrvSalesForce = null;
            }
        }

You can also use your session id to open an url or a document (ex : PDF) over the web (as you are already connected...you don't need further identification)
 
private const string StrOpenSalesforceWebWithSessionId = "https://YOURPLATFORM.salesforce.com/secur/frontdoor.jsp?sid={0}&retURL={1}";
		
		private void DoOpenDocumentURL(string aId)
		{
			if ((aId != null) && (SrvSalesForce != null))
			{                
				string StrUrl = string.Format(StrOpenSalesforceWebWithSessionId, SrvSalesForce.SessionHeaderValue.sessionId, "/servlet/servlet.FileDownload?file=" + aId);               
				DoOpenURL(StrUrl);
			}
		}        

		private void DoOpenURLWithSessionId(string aId)
		{
			if ( (aId != null) && (SrvSalesForce != null))
			{
				string StrUrl = string.Format(StrOpenSalesforceWebWithSessionId, SrvSalesForce.SessionHeaderValue.sessionId, "/" + aId);
				DoOpenURL(StrUrl);
			}
		}

		public static void DoOpenURL(string aStrUrl)
		{
			try
			{
				aStrUrl = Uri.EscapeUriString(aStrUrl);
				System.Diagnostics.Process.Start(aStrUrl);
			}
			catch (Exception ex)
			{
				if (ex.GetType().ToString() != "System.ComponentModel.Win32Exception")
				{
					try
					{
						System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("IExplore.exe", aStrUrl);
						System.Diagnostics.Process.Start(startInfo);
						startInfo = null;
					}
					catch (Exception ex2)
					{
						System.Windows.Forms.MessageBox.Show("Error : " + ex2.Message);
					}
				}
			}
		}

Finally, a function to get a contact list (in partameters the fields you want; the order, the filter and the record limit if needed...)
 
public static List<SF.Contact> GetLstContacts(SF.SforceService aSfdcBinding, string aLstField, string aFilter, string aOrder, string aLimit)
        {
            List<SF.Contact> LstSFDC = new List<SF.Contact>();
            string SOQL;
            SF.QueryResult queryResult = null;
            SF.Contact CurrentRecord;
            SOQL = "select " + aLstField + " from contact "+aFilter+" "+aOrder+" "+aLimit;
            try
            {
                queryResult = aSfdcBinding.query(SOQL.Trim());
                bool IsFinished = false;                
                if (queryResult != null)
                {
                    if (queryResult.size > 0)
                    {
                        while (!IsFinished)
                        {
                            SF.sObject[] TabRecords = queryResult.records;
                            for (int i = 0; i < TabRecords.Length; i++)
                            {                                
								CurrentRecord = (SF.Contact)TabRecords[i];                                
                                LstSFDC.Add(CurrentRecord);
                            }
                            // If we haven't read all the records (cf government limits), we need to read another record pack...
                            if (queryResult.done)
                            {
                                IsFinished = true;
                            }
                            else
                            {
                                queryResult = aSfdcBinding.queryMore(queryResult.queryLocator);
                            }
                        }
                    }
                }
            }
            catch (System.Web.Services.Protocols.SoapException e)
            {
               // Make your personal catch
            }
            return LstSFDC;
        }
Hope this can help you ^^
K JK J
Fabrice7, thanks you so much for your response and help.  I've found my problem. It's related to WSDL file that I've downloaded it.