Sharepoint Online – Custom Table of Contents

Building a knowledge repository is always a daunting and especially hard task to start. There are a lot of tools out there to help you get started like Confluence or more traditional tools like Sharepoint. Most of the time is worth making the investment in a state of the art tool like Confluence but most businesses are able to accomplish an good result by using their current Office 365 subscription to build an acceptable knowledge repository with Wiki Libraries. I was one of the latter so I embarked on the Wiki journey using Sharepoint online

It wasn’t long before I faced my first big roadblock: How do I insert a table of contents?

Tables of contents are essential parts of every Wiki as they let you quickly browse the headers of a page and jump to the place you need to go:

Sharepoint Online was confusing to say the least about how to go about adding a Table of contents to my newly created page. I spent some time browsing around the existing Web parts and found an existing Table of Contents Web part. However, you will notice that this is actually a list of all the pages in your site and has nothing to do with the contents of the specific page you are working on. After investigating for quite some time, I decided that this problem would have to be solved with a custom solution.

Build your custom Table of Contents

If you follow these steps, this is how your Wiki page will look like once you add some headers:

Note: The complete code will make your page look a lot like Wikipedia, but this can be removed as explained on step 2.

  • A Wiki Library created on Sharepoint Online
  • Editor access to your site: This is so that you can add a Content Editor Web part.
  • JQuery 2.2 or above: Makes it easier to traverse the DOM
  • Download HTML file from the Github repo

If you are not familiar with Javascript I would recommend following these steps skipping the code explanations. If you are code savvy check the code explanations and you will have a better idea on how to customize this to your needs.

  1. Download the following HTML file from the Github repository:

    Download File

    A code snippet is shown below, note that the fragment below is not the complete code and will not work on its own.

    function GenerateTOC() {
    var headerTOC = $('
    '<span > [<a id="hideLink" class="togglelink">Hide</a>] </span>'
    .on('click', 'a', toggleTOC);
    var levels = [
    var prevLvl = null;
    var currNode = $("#toc");
    ".ms-wikicontent h1, .ms-wikicontent h2, .ms-wikicontent h3, .ms-wikicontent h4"
    ).each(function(i) {
    var currentNode = $(this);
    currentNode.attr("id", "title" + i);
    var lvl = currentNode.prop("tagName")[currentNode.prop(
    "tagName").length - 1];
    if (lvl < prevLvl) { //clear levels levels =, ci, arr) { if (ci > lvl)
    return null;
    return cv;
    if (lvl && !levels[lvl]) {
    //level doesn't exist, create it and append to higher level
    levels[lvl] = currNode.append('
    currNode = $("
    	<li><a id='link'" + i + "' href='#title" + i + "' title='" + currentNode.html() + "'>" + currentNode.html() +
    //append item to level
    prevLvl = lvl;
    <div style='padding-bottom:20px;'/>")
    var toggleTOC = function toggleTOC() {
    var linkText = $("#hideLink").text() == 'Hide' ? 'Show' : 'Hide';
    $(document).ready(function() {

    The code above includes three main parts: A Wikipedia like CSS skin, this will make your Wiki page look like Wikipedia both on the header and body style. Remove the CSS classes if the skin is not desired.

    Two Javascript functions, the GenerateTOC function is a JQuery based function to iterate through all the headers in the page and generates a nested list architecture of as the table of contents. Items will be auto-numbered using standard HTML ordered lists. The second function supports the Hide/Show button on the table of contents.

    Note that JQuery is used as a supporting library to generate the table, if you would rather host JQuery on your Site Assets you can do so by modifying the first script tag on the file to point to the new location of the library.

  2. Upload the HTML file to your Site Assets. You can do so by clicking on the gear wheel in your Wiki page, then Site Contents. In the new Window select Site Assets and upload. Once uploaded, click the triple dot menu to copy a link to the asset, this will be used on step 6.
  3. Navigate to your Wiki page and click Edit
  4. On the menu bar select Insert> Web Part
  5. On the Web Part menu select Media & Content and insert the Content Editor Web Part:
  6. Edit the Web Part properties to set the following:
    Content Link: Should point to the HTML file uploaded on step 2.
    Chrome Type: Under Appearance > Chrome Type select None.
  7. Click OK in your Web Part to save changes.
  8. Back in your Wiki Page make sure you have content marked with Header 1, 2,3 or 4 to automatically include them in the table of contents.

You are done, you will note that the code in step 2 also sets a nice Wikipedia like formatting to your Wiki page:
Add the table of contents to all your Wiki pages or your master template so that you don’t have to repeat the steps.

I will post more tricks as I continue this journey in the Sharepoint Online Wiki.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s