Create a Dynamic Sitemap in ServiceNow

by Kevin Custer on July 29, 2020


For organizations having public-facing service portals, there is often a need to have public content indexed, or crawled, by search engines in order for users to quickly and easily find relevant articles. Building a sitemap is one way of improving SEO by essentially announcing a given list of links that search crawlers should visit. I will walk you through setting this up in ServiceNow by using a custom Processor. We will be using the protocol, so feel free to visit the link and get familiar with the XML tags to use.

Note: You will need the Custom Search Integration plugin activated in your instance, which must be done through a HI request, in order to update Robots.txt - this is necessary for this guide.

Create a Processor

Disclaimer: Per ServiceNow's documentation, processors are in fact a deprecated feature. However, they can still be created and used, at least for now. In the future I will amend this article to include instructions for using the recommended replacement, which is Scripted REST APIs.

Processors allow you to create a customizable URL endpoint that can execute server-side code to produce an output in HTML, JSON, Text, or even XML format. In this example we will be generating an XML sitemap document.

  1. Navigate to System Definition -> Processors, and click New
  2. Modify the Form Layout to include the Roles Field.
  3. Enter the following field values for the Processor:
Field Value
Name Dynamic Sitemap
Type script
CSRF protect false (unchecked)
Roles public
Description Generate a Dynamic Sitemap from Knowledge Base Articles for SEO
Path sitemap
  1. Modify the script below to suit your needs, and enter it in the Script field. The comments in the script describe what is being done, so be sure to read them! :) :
(function process(g_request, g_response, g_processor) {

    // Get the instance name for dynamic generation of URLs
    var instanceName = gs.getProperty("instance_name");
    // Enter static/manual URLs here, and the sitemap header
    //   In this example, we're indexing the default service 
    //   portal home and service catalog
    var xmlString = '<?xml version="1.0" encoding="UTF-8"?>';
    xmlString = xmlString + '<urlset xmlns:xsi="" ' +
        'xsi:schemaLocation="" ' +
        'xmlns="">' +
        '<url>' +
        '  <loc>https://' + instanceName + '</loc>\n' +
        '</url>' +
        '<url>' +
        '  <loc>https://' + instanceName + '</loc>' +

    // ------ Dynamic URLs -----------------------------------

    // Knowledge Articles - Public KB Articles
    // Encoded query gets all Published articles in the IT Knowlege Base,
    //   and where the Can Read user criteria is Empty (Public).  Tweak the
    //   query to suit your needs.
    var grKK = new GlideRecord('kb_knowledge');


    while ( {
        xmlString = xmlString +
            '<url>' +
            '  <loc>https://' + instanceName + ';sys_id=' + grKK.getValue('sys_id') + '</loc>' +
            '  <lastmod>' + grKK.getValue('sys_updated_on').split(' ')[0] + '</lastmod>' +
    // End Knowledge Articles

    // -- End Dynamic URLs -----------------------------------

    // Close the XML document root tag
    xmlString = xmlString + '</urlset>';

    // Convert the XML string into an XMLDocument
    var xmldoc = new XMLDocument(xmlString);

    // Write the XML document to output in text/xml format
    g_processor.writeOutput("text/xml", xmldoc);

})(g_request, g_response, g_processor);

Create a Public Page

The next thing to do is to make the new sitemap endpoint available to the public so that web crawlers can find it and index your content. To do this, we need to create a new public page.

  1. In the navigator type sys_public.list and press enter.
  2. Click new, and enter the following field values:
Field Value
Page sitemap
Active true (checked)

This allows us to navigate to without being logged in. Go ahead and try it in a private browser window! You should see something like this if everything works as expected:

ServiceNow Sitemap Results

Update Robots.txt

The last thing that needs to be done is to update Robots.txt - this is the file that tells search engines where they are allowed and disallowed from crawling, and also lets you specify the sitemap URL, which is useful when using a non-default URL (the default is /sitemap.xml, and ServiceNow uses /, for example).

This requires the Custom Search Integration plugin!

  1. Navigate to Custom Search Integration -> Robots.txt Definitions
  2. In the Text field, enter something like:
User-agent: *
Allow: /sp
Allow: /
Disallow: /

The above Robots.txt definition allows crawling of any page under the /sp URL, and it allows crawling of the sitemap, but it disallows all other crawling from the instance. Further, we have specified where crawlers can find the sitemap by providing the full URL.

It is recommended to either deactivate or use a data preserver on the Robots.txt file in sub-prod instances so that they do not get indexed by search engines.

With this, your instance's public content is now ready to be crawled by search engines!


Credit goes to zirnhelt and Charlotte Pakes of the ServiceNow community for inspiring this post.