Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (5) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Creating a Sitemap for a Salesforce Knowledge Public Knowledge Base

Creating a Sitemap for a Salesforce Knowledge Public Knowledge Base

Post by paul-lmi  (2011-01-27)

Status: Unverified
Level: intermediate


One important factor to note when rolling out a public knowledge base with a search feature is that many users are going to try Google before they come to your site to find the answers they are looking for. By ensuring search engines like Google have easier access to your content, you improve your user experience and make it easier for them to get the answers they need.


In this recipe, I'll step you through a simple VIsualforce page and Apex controller that will generate a Google-friendly sitemap XML that you can point to via Google Webmaster Tools. For a more generic look at creating a sitemap file, see Creating a Sitemap File.

Page Controller

In this controller, we'll be aggregating two Article Types: FAQ and Documentation. For the FAQ, we'll simply aggregate the top 1000 entries by last published date. For Documentation, we'll do the same, but also require two Data Categories to be added to the URL: Products and Languages.

public class SelfServiceSitemapCon {
  // A property to return the proper Sites URL, used to build our full URL later
  public string siteUrl{
 	String surl = site.getcustomwebaddress();
	if (surl != '' && surl != null) {
		return site.getcustomwebaddress();
	} else {
		return site.getcurrentsiteurl();

 // A method to retrieve the most recent 1000 FAQ__kav articles
  public FAQ__kav[] getFAQList(){	
	FAQ__kav[] f = [select id, knowledgearticleid, lastpublisheddate, localized_title__c, title FROM FAQ__kav where PublishStatus = 'Online' AND IsVisibleInPkb = true ORDER BY LastPublishedDate DESC LIMIT 1000];
	return f;	

  // A slightly more complex query based on the product and lang URL params, to return the most recent 1000 articles
  public Documentation__kav[] getDocumentationList(){
	String productcode = ApexPages.currentPage().getParameters().get('product') + '__c';
	String langcode = ApexPages.currentpage().getParameters().get('lang') + '__c';
	String qry = 'select id, knowledgearticleid, lastpublisheddate, localized_title__c, title from Documentation__kav where (PublishStatus = \'Online\' AND IsVisibleInPkb = true) WITH DATA CATEGORY Products__c AT ' + productcode + ' AND Languages__c AT ' + langcode +' ORDER BY LastPublishedDate DESC LIMIT 1000';
	Documentation__kav[] d = database.query(qry);	
	return d;	

This controller is all you need to build sitemap Visualforce pages for the FAQ and the Documentation. Let's look at the FAQ in more detail.

FAQ Sitemap

In this sitemap, we build a properly formatted sitemap XML file for Google to spider.

<apex:page controller="SelfServiceSitemapCon" showHeader="false" contentType="text/xml" cache="false">
<urlset xmlns:xsi="" xsi:schemaLocation="" xmlns=""> 
<apex:repeat id="faqrepeat" var="d" value="{!FaqList}">
		<lastmod>{!(TEXT(YEAR(d.lastpublisheddate)) & "-" & IF(LEN(TEXT(MONTH(d.lastpublisheddate)))==2,TEXT(MONTH(d.lastpublisheddate)),"0" & TEXT(MONTH(d.lastpublisheddate))) & "-" & IF(LEN(TEXT(DAY(d.lastpublisheddate)))==2,TEXT(DAY(d.lastpublisheddate)),"0" & TEXT(DAY(d.lastpublisheddate))))}</lastmod>

This page defines a <url> element for each FAQ in your public knowledge base (where the "isVisibleInPKB" flag on the article is set to true by your knowledge editor).

Note the important contentType attribute, which ensures this page is rendered as XML.

Documentation Sitemap

The sitemap for Documentation is pretty much identical to FAQ. In our implementation, we use a custom Visualforce page to render the article content (selfserviceknowledgerenderer), so we needed to pass the "type" URL param to that in order to clue that page's controller on what type of article it needs to retrieve and render. In your implementation, you will need to modify the content between the URLENCODE function of this sample to fit your needs.

Additional Steps

In order to expose your site map to Google, you'll need to:

  1. Add the Visualforce pages to your Site
  2. Ensure that the fields referenced in the page controller are allowed in Public Access Settings for the Site
  3. Add the site map to Google Webmaster Tools


I've built some sample URLs that you can view to see the end result. View page source to see the XML structure.



Recipe Activity - Please Log in to write a comment

YEAR() is not accepting a datetime. The documentation says it will, but visualforce won't compile the page and there is a thread about it on visualforce.

by a093000000FPtxD  (2011-09-07)


Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.