Learn how to generate a site map in SharePoint Online using Highlighted Content or Content Search web part without the added complexity of publishing features.
Ever been asked to display a list of sites or documents within a web part? “How do we display subsites in a table of contents?” or “We used to show users the document libraries they have access to.”
These requests are common when you have the publishing feature turned on for an on-premises version of SharePoint. The table of contents web part – though limited in my opinion – was the simple solution back in the day.
Alternatively, there are a couple of great options right out of the box in SharePoint Online that don’t require the publishing feature. Let’s look at the Content Search web part as well as the Highlighted Content web part, including how to configure a query in both to display a security trimmed list of sites.
Our Business Case using SharePoint Online
We have a site collection that we migrated from SharePoint on-premises that contains several subsites (I know what you’re thinking, how dare he use that terrible “subsite” word! Let’s save that discussion for another day).
In our legacy on-premises environment, the home page of the site collection used to display all the subsites within a web part. We would like to replicate that behavior using out-of-the-box web parts in a new SharePoint Online site but utilize a separate site collection to host the landing page. The list of sites should only display sites a user has existing permissions to.
Our two sites:
- Site 1 – https://ourtenant.sharepoint.com/sites/ClassicSiteExample
- We migrated this site from SharePoint on-premises to SharePoint Online. It contains subsites that use unique permissions based on departments.
- Site 2 – https://ourtenant.sharepoint.com/sites/Landing
- This site will be the landing page. It will display on its home page a list of sites from Site 1 within a web part.
Below we explore two options – the classic approach using Content Search and the modern approach using Highlighted Content.
Classic Approach – Content Search
The Content Search Web Part has been around for quite a while now and can be used to create some powerful query-based displays on a classic page, like a web part page.
This web part is available via the site collection feature, “SharePoint Server Enterprise Site Collection features.” Once we enable that feature for the site collection, the web part will be available in the standard web part menus within the “Content Rollup” section.
Having added the web part to our classic web part page, we can now configure the various settings as desired, beginning with the query.
The settings page displayed can be daunting the first time you see it, but don’t fret – we will only need a limited number of settings to display what our business case requires.
Beginning with the Basics settings, change the Select Query to Items matching a content type and the Restrict by app setting to Don’t restrict results by app. This will allow us to display results from another site collection that match the content type we prefer, namely sites.
Next, we need to define the site collection we would like to gather our results from. In our scenario, that would be any subsites contained within Site 1. To scope the results to Site 1, add the following query to the Add additional filters box (make sure to adjust the URL to your desired destination):
contentclass:STS_Web Path:https://ourtenant.sharepoint.com/sites/ClassicSiteExample
To ensure we are getting the desired results, click off the Add additional filters box, and the preview pane to the right will refresh based on our query.
In the example above, we see the results are displaying all subsites from Site 1.
Click OK to save the query and close the query builder. Finally, save the web part settings using the apply button and publish the page. Note: You can also set your display templates – I used a list with paging for Control and Two lines for Item.
Displayed below is the finished result.
Modern Approach – Highlighted Content
Classic page too classic for you? Not a problem! We can do the same within a modern page using the Highlighted Content web part and some simple KQL. The syntax is fairly similar to using filters in a Content Search web part with a few minor alterations. If you aren’t familiar with KQL, you can learn more about it here.
On a modern page, add the Highlighted Content web part and edit its properties.
In the web part properties pane, select Custom query, and change the source to All sites.
In the Query text (KQL) box, enter the following:
ParentLink:https://ourtenant.sharepoint.com/sites/ClassicSiteExample NOT contentclass:STS_List
The query above defines the site we want our results to come from as well as excludes lists from the results which are included by default.
Click the Apply button, and the results in the web part to the left will refresh based on the query.
As you can see, I’ve set my Layout to List, and the results display similar to the content search web part results. After altering the title text to Sites and publishing the page, our landing page displays the following:
Validating the Results and Security Trimming
For the final step, let’s ensure our end users are only allowed to see the sites they have permission to, as our business case requires.
To test this, I’ll add permissions for my test user account, aptly named Test User1, to Sub1 and Sub2 but not ClassicSubSite.
Success! Here is the Content Search web part display:
Here is the Highlighted Content web part display:
Based on our test results above, we can create a landing page that displays the subsites in a site collection that is security trimmed, so an end user can only see what they have permission to.
Now that the foundation is complete, we can go crazy with formatting, ranking results, dressing up the design of the landing page, and more.
Final Thoughts, Considerations and Gotchas
As we demonstrated above, both options can be a viable solution to the business need, but as with everything in SharePoint Online, it’s important to understand the full solution to ensure we select the correct option. There are some things to consider when using either approach:
- The Content Search web part is limited in how many results it can display out of the box. For instance, if we have a large number of results, we would need to use paging to display all of them. This can be a bit clunky for an end user to get used to.
- To use enterprise SharePoint features like search-driven content, which the Content Search web part requires, an end user must be licensed to an appropriate level. For example, a user with an E1 license will see no results as they do not have a license that supports the feature.
- The Highlighted Content web part is only available on a modern page. This can be quite a change for an end user coming from legacy SharePoint platforms, so ensure you take the time to train them properly on the differences between the two.
- Both options I’ve defined require a basic understanding of SharePoint content classes to ensure the results are properly defined and filtered.