Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Visualforce Development Cookbook

You're reading from  Visualforce Development Cookbook

Product type Book
Published in Sep 2013
Publisher Packt
ISBN-13 9781782170808
Pages 334 pages
Edition 1st Edition
Languages
Author (1):
Keir Bowden Keir Bowden
Profile icon Keir Bowden

Table of Contents (16) Chapters

Visualforce Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. General Utilities 2. Custom Components 3. Capturing Data Using Forms 4. Managing Records 5. Managing Multiple Records 6. Visualforce Charts 7. JavaScript 8. Force.com Sites 9. jQuery Mobile Index

Chapter 8. Force.com Sites

In this chapter, we will cover the following recipes:

  • Creating a site

  • Record and field access

  • Retrieving content from Salesforce

  • Web to lead form

  • Creating a website template

  • Adding a header menu to a template

  • Adding a sidebar to a template

  • Conditional rendering in templates

Introduction


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.

Note

Salesforce supports an additional technology to host websites, Site.com, which does not use Visualforce to generate content. For more...

Creating a site


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.

Getting ready

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.

Note

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...

Record and field access


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.

Getting ready

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.

How to do it…

  1. First, add access to...

Retrieving content from Salesforce


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.

Getting ready

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.

  1. First, create the site content custom sObject by navigating to Your Name | Setup | Create | Objects.

  2. Click on...

Web to lead form


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.

Note

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.

Getting ready

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.

How to do it…

  1. First, create the thank you Visualforce...

Creating a website template


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.

Getting ready

This recipe requires that you have already completed the Creating a site...

Adding a header menu to a template


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.

Getting ready

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.

How to do it…

  1. First, create the template; this is simply another...

Adding a sidebar to a template


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.

Getting ready

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.

How to do it…

  1. First, create the template...

Conditional rendering in templates


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...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Visualforce Development Cookbook
Published in: Sep 2013 Publisher: Packt ISBN-13: 9781782170808
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}