Site Tools‎ > ‎Tips and Tricks‎ > ‎

Script to Display Site Map with URLs

The attached code can be used to display all the pages in a Google Site along with their associated URLs.

Note: This script will replace the entire contents of a Site Page. If you edit the "target" Site Page manually any changes will be over written when the script runs

Step 1: Create a web page in your Google Site for Data
  1. Click on Create page
  2. Select Web Page
  3. Name it whatever you want
  4. Click Create Page
  5. Leave the page "empty", click Save
  6. Make a note of the page's URL (you will need this information in Step 3)
Step 2:  Add the script to your Google Site 
  • Go to Manage site => Site content: Apps Scripts
  • Click the Launch Editor button
  • If this is the first script you are creating, the editor will open to a relatively empty page that looks something like this. If this is not your first script, click File => New in the script editor.

  • Download the attached file called SiteMap_with_URLs.txt. Open the file. Select all the text and copy it into the script editor (it should replace any text that is in the editor).
  • Click File => Save (you can name your script anything you want, just so it is "unique".
Step 4:  Modify the variables in the script as needed 
  • In the script populate the variables as needed. The variable that needs to be replaced is highlighted in yellow below. The text in red shows what the value should be.
 // ************** Start of variable that need to be modified **************
  var targetpageurl = ''; //edit this line and add the url for the page where you want the sitemap to show up.
  var targetpage = SitesApp.getPageByUrl(targetpageurl);
  var toppages = SitesApp.getSite('', 'acsplace').getChildren(); //edit this line and add your domain name and the sitename for your site
  // ************** End of variable that need to be modified *************

Special Notes:

Site Name (as displayed in the Site URL - it's the first part that comes after in the variable called site

Step 5: Test/Run Your Script

  • Click Save after making all the modifications to your script. (you shouldn't get any errors - but if you do, check for typos")
  • Now test/run your script by clicking on Run =>  generateIndex from within the script
  • The first time you save the trigger you will need to authorize the script to run. Just click the Authorize button.
  • You will now see a Google Accounts pop-up that asks you to Grant access to Google Sites to your account. Just click the Grant access button.
  • Now test/run your script by clicking on Run =>  generateIndex from within the script
  • If all went well, you should see a Running Script message at the top - the message will disappear when the script is complete.
  • Check the Sites page you created to see if it was populated correctly.

Step 6: Set your script's "Trigger"

Since you don't want to manually run the script every time there is an update, you will need to set up some "triggers" to run the script automatically on a regular basis.
  • In the script editor select Triggers => Current script's triggers. You will now see this. Click on the link to add a trigger

  • Set Run to generateIndex and Events to Time-driven. The other settings can be set to whatever you want depending on how often you want to refresh the page. Click Save.  In the example below, I have set it to check every 10 minutes.

Example of output:

Unknown user,
Mar 29, 2012, 4:14 PM