Recently I had to create a web service in Salesforce and expose data to external application.
The first part with creating of web service on Salesforce is quite easy because SF does a good job of converting APEX classes into a web services and this information is explained very well in original SF documentaion. So, it is required to create an APEX class with "webService" keywords and download WSDL file wich can be used for proxy generation.
Here is an example: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_web_services_methods.htm
The tricky part of it is that you need to call this service from, say, C#, and this information is spread among different posts, forum topics etc.
Here is the combined and simplified C# example:
The first part with creating of web service on Salesforce is quite easy because SF does a good job of converting APEX classes into a web services and this information is explained very well in original SF documentaion. So, it is required to create an APEX class with "webService" keywords and download WSDL file wich can be used for proxy generation.
Here is an example: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_web_services_methods.htm
The tricky part of it is that you need to call this service from, say, C#, and this information is spread among different posts, forum topics etc.
Here is the combined and simplified C# example:
using System; using System.IO; using System.Net; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Text; namespace WSTest { [DataContract] internal class AuthToken { [DataMember] internal string id; [DataMember] internal string issued_at; [DataMember] internal string instance_url; [DataMember] internal string signature; [DataMember] internal string access_token; } class Program { private static AuthToken SalesforceAutenticate(string userName, string password) { // Sandbox URI, for production environment it should be "https://login.salesforce.com/services/oauth2/token" const string URI = "https://test.salesforce.com/services/oauth2/token"; // how to obtained security token: https://success.salesforce.com/answers?id=90630000000glADAAY const string securityToken = "1234567890"; // how to obtain consumer key + secret: http://feedback.uservoice.com/knowledgebase/articles/235661-get-your-key-and-secret-from-salesforce const string consumerKey = "XXXXXXXXXXX.YYYYYYYYYYYYYYYYYYYYYYY"; const string consumerSecret = "0987654321"; WebRequest req = WebRequest.Create(URI); req.Proxy = GetProxy(); req.ContentType = "application/x-www-form-urlencoded"; req.Method = "POST"; String parameters = String.Format("grant_type=password&client_id={0}&client_secret={1}&username={2}&password={3}{4}", consumerKey, consumerSecret, userName, password, securityToken); byte[] bytes = Encoding.ASCII.GetBytes(parameters); req.ContentLength = bytes.Length; Stream os = req.GetRequestStream(); os.Write(bytes, 0, bytes.Length); os.Close(); WebResponse resp = req.GetResponse(); if (resp == null) return null; // parse JSON response DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(AuthToken)); AuthToken token = (AuthToken)ser.ReadObject(resp.GetResponseStream()); return token; } static void Main(string[] args) { var authToken = SalesforceAutenticate("my@email.dev", "mypassword"); MyWebServiceService client = new MyWebServiceService(); client.Proxy = GetProxy(); client.SessionHeaderValue = new SessionHeader(); client.SessionHeaderValue.sessionId = authToken.access_token; Console.WriteLine(client.SampleMethod("123456")); } private static IWebProxy GetProxy() { IWebProxy proxy = new WebProxy("server:port"); proxy.Credentials = new NetworkCredential("ADname", "ADpassword"); return proxy; } } }