How to create an index for SharePoint Spaces using the content query web part

SharePoint Spaces are great and allow you to display data like never before however they are just regular pages and there is no place where you can get a central view of all the spaces you have created.

As part of the Library series of articles I’ve been doing (you can read more about it here and here) in this one I’ll show you how to build an index to display SharePoint Spaces using the content query web part.

The content query web part is an open source project included in the PnP initiative and it just got updated to make use of the latest version of the SharePoint Framework.

sharepoint spaces index

The Content Query web part is a modern version of the good old Content by Query web part that was introduced in SharePoint 2007. While the original web part was based on an XSLT template engine, the new web part uses Handlebars template engine, which empowers users to create simple, yet powerful HTML templates for rendering the queried content.

To build the SharePoint Spaces index you can either clone the GitHub repository and compile the web part yourself or download a compiled version for Office 365 from here.

Once installed and added to your site collection do the following:

  1. Add the content query web part to the page you want to use as an index
  2. Once added to the page open the settings pane
  3. Choose the source site for your index, if you are creating the index in the same site collection where you have the SharePoint Spaces it will be selected by default
  4. Choose the web where you have your SharePoint Spaces, by default it will select the root site of the site collection
  5. On the list select Site Pages, this is the library where all the pages are created including the SharePoint Spaces
  6. Chose the library field to sort the items, Title is a good candidate for an index
  7. On the filters you need to select Page Layout Type {{PageLayoutType}}, this is the field that will be used to select just the SharePoint Spaces
  8. On the operator select Equals
  9. On the value use d39ad2cb-84bd-48a0-9daa-4aea9f644cd4, this is the unique identifier of the page layout used by the SharePoint Spaces
  10. On the Layout section select the fields you want to display in your index, to build this example you will need to select the following:
    • Banner Image Url {{BannerImageUrl}} – this field has a preview image of your SharePoint Space that will help you to easily identify it
    • Title {{Title}} – this the name of your SharePoint Space
    • URL Path {{FileRef}} – this the link to your SharePoint Space
  11. Click on the Edit template content and replace all default content by the following Handlebars template

    <style type="text/css">
    	.dynamic-template h2 {
    		font-size: 24px;
    		font-weight: 300;
    		color: "[theme:neutralPrimary, default:#323130]";
    	}
    
    	.space-card {
    		display: block;
    		-webkit-box-shadow: 0 1.6px 3.6px 0 rgba(0,0,0,0.132), 0 0.3px 0.9px 0 rgba(0,0,0,0.108);
    		box-shadow: 0 1.6px 3.6px 0 rgba(0,0,0,0.132), 0 0.3px 0.9px 0 rgba(0,0,0,0.108);
    		border-radius: 2px;
    		background-clip: padding-box;
    		-webkit-transition: all 300ms cubic-bezier(0.8, 0, 0.2, 1);
    		transition: all 300ms cubic-bezier(0.8, 0, 0.2, 1);		
    		margin-bottom: 16px;	
    		background-color: "[theme:whiteTranslucent40, default: rgba(255,255,255,.4)]";	
    	}
    	
    	.space-link, .space-link:visited{
    		color: "[theme:themePrimary, default: #0078d4]";
    		text-decoration: none;
    	}
    	
    	.space-link:hover{
    		color: "[theme:themeLighter, default: #c7e0f4]";;
    		text-decoration: none;
    	}
    	
    	.space-card:hover{
    		background-color: "[theme:themeSecondary, default: #2b88d8]";
    	}
    	
    	.card-image{
    		height:150px;
    		border-top-left-radius: 2px;
    		border-top-right-radius: 2px;
    	}
    	
    	.card-title {
    		padding: 20px;
    		text-align: center;
    		font-size: 20px;
    		font-weight: 100;
    	}
    </style>
    
    <div class="dynamic-template">
      <h2>Space Libraries</h2>
      <div class="ms-Grid-row ">
    	  {{#each items}}          
    		  <a href="{{FileRef.textValue}}" class="ms-Grid-col ms-sm12 ms-md3 block space-link">
    			<div class="space-card">
    				<div class="card-image" style="background-image: url('{{BannerImageUrl.rawValue.Url}}'); background-position: center center; background-size:cover;"></div>	
    				<div class="dynamic-item-fields card-title">
    					<span class='ms-DetailsRow-cell'>{{Title.textValue}}</span>					
    				</div>
    			</div>
    		  </a>
    	  {{/each}}
      </div>
    </div>
    
  12. Click Save an Publish your page

Your SharePoint Spaces index will look similar to the following animation, modify it according to your needs or reuse the content query web part to easily build content inside SharePoint.

Download Content Query Web Part
Explore Solution on GitHub

Woman vector created by stories – www.freepik.com


No comments yet

Leave a Reply


Web developer focused on SharePoint branding, blogger, tech enthusiast. Travelling and sports are my addictions, knowledge and success are my daily motivations.