title: Treat Salesforce VisualForce Page as an External Widget
body: <p>I want to turn a Salesforce VisualForce Page into a widget for my corporate website. I want to access the widget using server side code and a service account. I'll cache, style, and output the widget html in my web page. I know I could just reproduce the widget with server side code and several API calls but I like the idea of being able to manage the widget in Salesforce and having the widget available for reuse elsewhere within Salesforce. </p>
  
  <p>In this case, the widget is going to report on the current leaders in a contest we're tracking in Salesforce. I plan on reusing this approach for other "mini-report widgets." This is a very useful pattern. I'm sure somebody out there has elegantly solved it already. My searches have come up empty so far.</p>
  
  <p>I was trying to avoid Sites because the user hitting our corporate website does not have a user account in Salesforce (now I'm thinking about a Site that allows anonymous access).</p>
  
  <p>My first attempt looked like this (inspired by SSO to Self Service Portal):</p>
  
  <pre><code>var ws = new sfapi.SforceService();
  var lr = ws.login(Secrets.salesforce_user_name, Secrets.salesforce_password);
  string url = "https://na6.salesforce.com/apex/mywidget?sessionId=" + lr.sessionId;
  </code></pre>
  
  <p>I tried ?sid and ?csssid too, but urls like these just bounce me to the Salesforce login page:</p>
  
  <pre><code>https://na6.salesforce.com/apex/mywidget?sessionId=00D3000000myorg!my-session-id-from-logging-in-with-the-api-8_i2fX_9wnYdwnwatGVuX6jGjmVmnv50j78yfGF1aKHdoDFtIx_J9
  </code></pre>
  
  <p>I could probably use standard screen scraping techniques to post to the standard Salesforce login form with a username and password and then pull the /apex/mywidget page. But that feels clumsy and unsupported.</p>
  
  <p>Now I'm thinking about making a new Site that is publicly available just to expose my widget. At least I could screen scrape that page with confidence.</p>
  
  <p>Any help is appreciated. I'm going to play with the anonymous sites approach for now.</p>