Force.com sites allow public websites to be created in and hosted by Salesforce, removing the requirement to configure, secure, and manage a web server. Visualforce pages that have direct access to Salesforce data via the page controller generate the site content.
In this chapter, we will create a Force.com site initially containing static content. We will then create a set of template pages to remove repetition of common markup. Finally, we will provide access to Salesforce data from a public website, allowing visitors to access records without logging in to Salesforce.
Unlike earlier chapters in this book, these recipes are best performed in order, as many recipes build on knowledge gained in earlier recipes and the first recipe, Creating a site, configures the Force.com site that is used to serve the content for all of the remaining recipes.
In this recipe we will configure a Force.com site that displays a single page. The contents of the page are static, and the page will be publicly available to unauthenticated visitors.
This recipe uses the Bootstrap framework (http://twitter.github.io/bootstrap/) to style the page. The JavaScript and CSS files are included from the BootStrapCDN (http://www.bootstrapcdn.com/index.html) content delivery network rather than being uploaded as Salesforce static resources, as this makes it straightforward to move to new versions simply by changing the URL of the included file. Bootstrap in turn relies on the jQuery (http://jquery.com/) JavaScript framework. This is included from the Google Hosted Libraries content delivery.
This does introduce a dependency on the BootstrapCDN and Google Hosted Libraries sites and in the event that either site was unavailable or access blocked, the Bootstrap styling and functionality would be lost.
Before the site can be configured...
A common source of confusion for Visualforce developer is configuring a Force.com site to allow unauthenticated access to Salesforce records and specific fields. This is usually configured via the Profiles menu located at Your Name | Setup | Administration Setup | Profiles. However, access to records and fields for a Force.com site is configured via the setup page for the site in question.
In this recipe we will configure the Force.com site created in the first recipe to allow public access to contact records. We will then create a Visualforce page that allows a visitor to enter an e-mail address into a form on the Force.com site and extract the contact record matching the e-mail address, displaying the First Name, Last Name, and Email fields from the contact record.
This recipe requires that you have already completed the Creating a Site recipe, as it relies on the custom domain and Force.com site created in that recipe.
Force.com sites allow unauthenticated visitors access to custom (and some standard) Salesforce sObjects. While providing public access to data stored in Salesforce might seem like a security risk, it is a perfect fit for dynamically generated website content; content authors don't need to know how to edit Visualforce pages in order to be able to update the content of the site.
In this recipe we will create a Visualforce page that renders content from the three most recently edited records of a custom sObject. We will then make this page publicly available via an unauthenticated Force.com site.
This recipe requires that you have already completed the Creating a site recipe, as it relies on the custom domain and Force.com site created in that recipe.
This recipe requires a custom sObject that encapsulates the items to display on the site.
First, create the site content custom sObject by navigating to Your Name | Setup | Create | Objects.
Click on...
The standard Salesforce web to lead functionality allows a form to be embedded into a company's website to capture information that is then turned into a lead in the company's Salesforce instance. The form is submitted to a servlet that is common to all Salesforce instances and thus, may not be customized besides sending the user to a thank you page that is disconnected from the lead.
For more information on web to lead, visit http://login.salesforce.com/help/doc/en/customize_leadcapture.htm.
In this recipe we will create a Visualforce page that captures a lead and redirects the user to a personalized thank you page that displays the ID of the lead for future reference. We will then make this page publicly available via an unauthenticated Force.com site.
This recipe requires that you have already completed the Creating a site recipe, as it relies on the custom domain and Force.com site created in that recipe.
In the previous recipes in this chapter, each page contained the entire Visualforce markup needed to display its content. This leads to repetition of common markup, to display headers and footers for example. In the event that the header or footer content needs to be changed, every page on a site needs to be updated with the new markup.
Visualforce provides a solution to this issue—templates. These allow the common elements of a site to be added to a template that is used as the starting point for rendering any page. The page then injects its specific content into the template at appropriate points.
In this recipe we will create a template version of the SiteItem Visualforce page from the Retrieving content from Salesforce recipe, where the template provides the header and footer markup. We will then make this page available publicly available via an unauthenticated Force.com site.
A common requirement for a website is to display a navigation menu as part of the header. In the scenario where each page defines its own header and footer, it is straightforward to highlight a menu option to indicate the page that is currently being displayed. When a template provides the header and footer information, a mechanism is required to allow the page to identify itself to the template, which can then highlight the appropriate menu option.
In this recipe, we will create a Visualforce template that provides header and footer content to four other Visualforce pages: a Home page, an About page, a Contact page, and a Links page. We will then make these pages available publicly available via an unauthenticated Force.com site.
This recipe requires that you have already completed the Creating a site recipe, as it relies on the custom domain and Force.com site created in that recipe.
Website content is not always suited to being broken up across a number of pages. A table of contents, while being potentially quite long, may be more usable when displayed on a single page. A sidebar can improve the user experience by providing links to allow the user to rapidly navigate around the lengthy content.
In this recipe, we will create a Visualforce template that provides header and footer content to two other Visualforce pages: a TableOfContents page (containing information about all chapters) and a Chapter1 page (containing detailed information about the first chapter). Each page has a sidebar to assist with navigation through the page content. We will then make these pages available publicly available via an unauthenticated Force.com site.
This recipe requires that you have already completed the Creating a site recipe, as it relies on the custom domain and Force.com site created in that recipe.
Templating a website is an effective way to avoid repeated content and the associated maintenance overhead. There are occasions when this common content needs to be replaced for one or two exceptional pages; for example, a homepage may require slightly different header information than other pages in a site. This problem can be solved by the homepage not utilizing a template, but this then means that any common content that the homepage does require is repeated in the homepage and the template.
In this recipe we will create a Visualforce template that provides header and footer content. A page may override the header text provided by the template. We will then create two Visualforce pages that utilize this template: a StandardHeader page (that displays the standard header text) and a CustomHeader page (that provides its own custom text for use in the header). We will then make these pages available publicly available via an unauthenticated Force.com site...