Create A Simple Bootstrap Navigation Menu Using Content Search WebPart

In the first step, let's create the Authoring and Publishing Site Collections. 

Authoring Site collection allows authors to manage their website content while the Publishing Site collection reviews the content from the authoring site collection.

Open the Authoring Site Collection.

Create a list named “menu” that contains title and page URL.




Let’s go and create a control and a template to display the items.

  • Control Template
    It provides the overall structure (layout) of the HTML elements along with start and end tags.

  • Item Template
    It renders the items from the list, and contains text and pictures.

Now, let's create two display templates for displaying a bootstrap carousel menu control and a menu item.

Code – Menu Control Template

Upload the script and CSS into style library.


Add the scripts and CSS into Control template. 

  1. <script>  
  2.     $includeCSS(this.url, "~sitecollection/Style%20Library/Menu/bootstrap.min.css");  
  3.     $includeScript(this.url, "~sitecollection/Style%20Library/Menu/bootstrap.min.js");  
  4.     $includeScript(this.url, "~sitecollection/Style%20Library/Menu/jquery.min.js");  
  5. </script>   

Now, let's define the HTML layout to the Control Template. 

  1. <nav class="navbar navbar-inverse">  
  2.     <div class="container-fluid">  
  3.         <div class="navbar-header"> <a class="navbar-brand" href="#">Csharpcorner </a> </div>  
  4.         <ul class="nav navbar-nav"> _#= ctx.RenderItems(ctx) =#_ </ul>  
  5.     </div>  
  6. </nav>   

The full code looks like the following. 

  1. <html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">  
  2.   
  3. <head>  
  4.     <title>MenuControl</title>  
  5.     <!--[if gte mso 9]><xml>  
  6.   
  7. <mso:CustomDocumentProperties>  
  8.   
  9. <mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>  
  10.   
  11. <mso:MasterPageDescription msdt:dt="string">Display a menu Item</mso:MasterPageDescription>  
  12.   
  13. <mso:ContentTypeId msdt:dt="string">0x0101002039C03B61C64EC4A04F5361F385106601</mso:ContentTypeId>  
  14.   
  15. <mso:TargetControlType msdt:dt="string">;#SearchResults;#Content Web Parts;#</mso:TargetControlType>  
  16.   
  17. <mso:HtmlDesignAssociated msdt:dt="string">1</mso:HtmlDesignAssociated>  
  18.   
  19. <mso:HtmlDesignConversionSucceeded msdt:dt="string">True</mso:HtmlDesignConversionSucceeded>  
  20.   
  21. <mso:CrawlerXSLFile msdt:dt="string"></mso:CrawlerXSLFile>  
  22.   
  23. <mso:HtmlDesignPreviewUrl msdt:dt="string"></mso:HtmlDesignPreviewUrl>  
  24.   
  25. <mso:HtmlDesignStatusAndPreview msdt:dt="string">http://technologygeeks/sites/publishing/_catalogs/masterpage/Display Templates/Content Web Parts/banner/bannercontrol.html, Conversion successful.</mso:HtmlDesignStatusAndPreview>  
  26.   
  27. </mso:CustomDocumentProperties>  
  28.   
  29. </xml><![endif]-->  
  30. </head>  
  31.   
  32. <body>  
  33.     <script>  
  34.         $includeCSS(this.url, "~sitecollection/Style%20Library/Menu/bootstrap.min.css");  
  35.         $includeScript(this.url, "~sitecollection/Style%20Library/Menu/bootstrap.min.js");  
  36.         $includeScript(this.url, "~sitecollection/Style%20Library/Menu/jquery.min.js");  
  37.     </script>  
  38.     <div>  
  39.         <!--#_if (!$isNull(ctx.ClientControl) && !$isNull(ctx.ClientControl.shouldRenderControl) && !ctx.ClientControl.shouldRenderControl()){return "";}  
  40.   
  41. ctx.ListDataJSONGroupsKey = "ResultTables";  
  42.   
  43. ctx["CurrentItems"] = ctx.ListData.ResultTables[0].ResultRows;  
  44.   
  45. var siteURL = SP.PageContextInfo.get_siteAbsoluteUrl();  
  46.   
  47. AddPostRenderCallback(ctx, function() {  
  48.   
  49. $.getScript(siteURL + "");  
  50.   
  51. });  
  52.   
  53. _#-->  
  54.         <nav class="navbar navbar-inverse">  
  55.             <div class="container-fluid">  
  56.                 <div class="navbar-header"> <a class="navbar-brand" href="#">WebSiteName</a> </div>  
  57.                 <ul class="nav navbar-nav"> _#= ctx.RenderItems(ctx) =#_ </ul>  
  58.             </div>  
  59.         </nav>  
  60.     </div>  
  61. </body>  
  62.   
  63. </html>   

Let’s go to Item template and declare the managed properties and necessary variables. 

  1. <!--#_  
  2.   
  3. var siteURL = SP.PageContextInfo.get_siteServerRelativeUrl();  
  4.   
  5. var itemIdx = ctx.CurrentItemIdx+1;  
  6.   
  7. var title = $getItemValue(ctx, "Title");  
  8.   
  9. var getURL = $getItemValue(ctx, "PageURL");  
  10.   
  11. var esActive ="";  
  12.   
  13. if(itemIdx == 1)  
  14.   
  15. esActive = "active";  
  16.   
  17. _#-->   

Render the HTML structure with the list item information. 

  1. <li class="_#= esActive =#_"> <a href="_#= getURL =#_">_#= title =#_</a> 
  2. </li>   

Overall Item template code looks like the following. 

  1. <html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">  
  2.   
  3. <head>  
  4.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  5.     <title>MenuItem</title>  
  6.     <!--[if gte mso 9]><xml>  
  7.   
  8. <mso:CustomDocumentProperties>  
  9.   
  10. <mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>  
  11.   
  12. <mso:MasterPageDescription msdt:dt="string">Displays an Menu Item.</mso:MasterPageDescription>  
  13.   
  14. <mso:ContentTypeId msdt:dt="string">0x0101002039C03B61C64EC4A04F5361F385106603</mso:ContentTypeId>  
  15.   
  16. <mso:TargetControlType msdt:dt="string">;#SearchResults;#Content Web Parts;#</mso:TargetControlType>  
  17.   
  18. <mso:HtmlDesignAssociated msdt:dt="string">1</mso:HtmlDesignAssociated>  
  19.   
  20. <mso:ManagedPropertyMapping msdt:dt="string">'Title':'Title','PageURL':'PageURL'</mso:ManagedPropertyMapping>  
  21.   
  22. <mso:CrawlerXSLFile msdt:dt="string"></mso:CrawlerXSLFile>  
  23.   
  24. <mso:HtmlDesignPreviewUrl msdt:dt="string"></mso:HtmlDesignPreviewUrl>  
  25.   
  26. <mso:HtmlDesignConversionSucceeded msdt:dt="string">True</mso:HtmlDesignConversionSucceeded>  
  27.   
  28. <mso:HtmlDesignStatusAndPreview msdt:dt="string">http://technologygeeks/sites/publishing/_catalogs/masterpage/Display Templates/Content Web Parts/banner/BannerItem.html, Conversion successful.</mso:HtmlDesignStatusAndPreview>  
  29.   
  30. </mso:CustomDocumentProperties>  
  31.   
  32. </xml><![endif]-->  
  33. </head>  
  34.   
  35. <body>  
  36.     <div>  
  37.         <!--#_  
  38.   
  39. var siteURL = SP.PageContextInfo.get_siteServerRelativeUrl();  
  40.   
  41. var itemIdx = ctx.CurrentItemIdx+1;  
  42.   
  43. var title = $getItemValue(ctx, "Title");  
  44.   
  45. var getURL = $getItemValue(ctx, "PageURL");  
  46.   
  47. var esActive ="";  
  48.   
  49. if(itemIdx == 1)  
  50.   
  51. esActive = "active";  
  52.   
  53. _#-->  
  54.         <!--- HTML Goes Here -->  
  55.         <li class="_#= esActive =#_"> <a href="_#= getURL =#_">_#= title =#_</a> </li>  
  56.     </div>  
  57. </body>  
  58.   
  59. </html>   

Upload the display template under Site Settings - > Master page and page layouts -> Display Templates -> Content Web Parts.


Now, let’s go to SharePoint page.

Add a Content Search Web Part into this page.



Map a search query to get the result from the authoring site collection.

Click Edit Web Part -> change query.


Provide number of items to display.


Choose the display templates.


Click OK. Under property mapping section, map the managed properties of the title, image description, and image.


Click OK to complete the setup.

The following is the final result.


List Data


Note

The result has been displayed using Search Service. You need to run the Search Service application. After adding the content into the SharePoint list, the result will be successfully displayed into the page.