Create A Bootstrap Image Carousel Using Content Search Web Part CSWP In SharePoint 2013

Content Search Web Part

In  the first step, let's create the authoring and publishing site collection. 

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 “banner” that contains title, description, and images.


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 a two display templates for display a bootstrap carousel banner control and banner item

Code - Banner Control Template

Upload the script and CSS into style library.


Add the scripts and css in to control template.
  1. <script>  
  2.         $includeCSS(this.url, "~sitecollection/Style%20Library/banner/bootstrap.min.css");  
  3.         $includeCSS (this.url, "~sitecollection/Style%20Library/banner/custom.css");  
  4.         $includeScript(this.url, "~sitecollection/Style%20Library/banner/bootstrap.min.js");  
  5.         $includeScript(this.url, "~sitecollection/Style%20Library/banner/jquery.min.js");  
  6.       
  7.     </script>  
Now, I am going to define the HTML layout to the Control Template.
  1. <div class="container">  
  2.             <br>    
  3.             <div id="myCarousel" class="carousel slide" data-ride="carousel">  
  4.             <ol class="carousel-indicators">  
  5.             <li data-target="#myCarousel" data-slide-to="0" class="active"></li>  
  6.             <li data-target="#myCarousel" data-slide-to="1"></li>  
  7.             <li data-target="#myCarousel" data-slide-to="2"></li>  
  8.             <li data-target="#myCarousel" data-slide-to="3"></li>  
  9.            </ol>  
  10.       <!-- Wrapper for slides -->  
  11.               <div class="carousel-inner" role="listbox">                       
  12.           _#= ctx.RenderItems(ctx) =#_  
  13.                 </div>  
  14.             </div>  
  15.             </div>                               
  16.             </div> 
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.     <head>  
  3.         <title>BannerControl</title>  
  4.   
  5.         <!--[if gte mso 9]><xml>  
  6.         <mso:CustomDocumentProperties>  
  7.         <mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>  
  8.         <mso:MasterPageDescription msdt:dt="string">Display a banner Item</mso:MasterPageDescription>  
  9.         <mso:ContentTypeId msdt:dt="string">0x0101002039C03B61C64EC4A04F5361F385106601</mso:ContentTypeId>  
  10.         <mso:TargetControlType msdt:dt="string">;#SearchResults;#Content Web Parts;#</mso:TargetControlType>  
  11.         <mso:HtmlDesignAssociated msdt:dt="string">1</mso:HtmlDesignAssociated>  
  12.         <mso:HtmlDesignConversionSucceeded msdt:dt="string">True</mso:HtmlDesignConversionSucceeded>  
  13.         <mso:CrawlerXSLFile msdt:dt="string"></mso:CrawlerXSLFile>  
  14.         <mso:HtmlDesignPreviewUrl msdt:dt="string"></mso:HtmlDesignPreviewUrl>  
  15.         <mso:HtmlDesignStatusAndPreview msdt:dt="string">http://10.100.247.50:801/_catalogs/masterpage/Display%20Templates/Content%20Web%20Parts/News/bannercontrol.html, Conversion successful.</mso:HtmlDesignStatusAndPreview>  
  16. </mso:CustomDocumentProperties>  
  17.         </xml><![endif]-->  
  18.     </head>  
  19.   
  20.     <body>          
  21.         <script>  
  22.             $includeCSS(this.url, "~sitecollection/Style%20Library/banner/bootstrap.min.css");  
  23.             $includeCSS(this.url, "~sitecollection/Style%20Library/banner/custom.css");  
  24.             $includeScript(this.url, "~sitecollection/Style%20Library/banner/bootstrap.min.js");  
  25.             $includeScript(this.url, "~sitecollection/Style%20Library/banner/jquery.min.js");  
  26.           
  27.         </script>  
  28.         <div>  
  29.             <!--#_if (!$isNull(ctx.ClientControl) && !$isNull(ctx.ClientControl.shouldRenderControl) && !ctx.ClientControl.shouldRenderControl()){return "";}  
  30.             ctx.ListDataJSONGroupsKey = "ResultTables";  
  31.             ctx["CurrentItems"] = ctx.ListData.ResultTables[0].ResultRows;  
  32.             var siteURL = SP.PageContextInfo.get_siteAbsoluteUrl();       
  33.               
  34.             AddPostRenderCallback(ctx, function() {  
  35.                 $.getScript(siteURL + "");  
  36.                   
  37.                     });   
  38.             _#-->  
  39.              <div class="container">  
  40.             <br>    
  41.             <div id="myCarousel" class="carousel slide" data-ride="carousel">  
  42.             <ol class="carousel-indicators">  
  43.             <li data-target="#myCarousel" data-slide-to="0" class="active"></li>  
  44.             <li data-target="#myCarousel" data-slide-to="1"></li>  
  45.             <li data-target="#myCarousel" data-slide-to="2"></li>  
  46.             <li data-target="#myCarousel" data-slide-to="3"></li>  
  47.            </ol>  
  48.       <!-- Wrapper for slides -->  
  49.               <div class="carousel-inner" role="listbox">  
  50.                         _#= ctx.RenderItems(ctx) =#_  
  51.                                     </div>  
  52.             </div>  
  53.             </div>                               
  54.             </div>  
  55.     </body>  
  56. </html>  
Let’s go to Item template and declare the managed properties and necessary variables.
  1. <!--#_  
  2.             var siteURL = SP.PageContextInfo.get_siteServerRelativeUrl();  
  3.             var itemIdx = ctx.CurrentItemIdx+1;  
  4.             var title = $getItemValue(ctx, "Title");   
  5.             var desc = $getItemValue(ctx, "ImageDescription");  
  6.             var Image = $getItemValue(ctx, "PublishingImage");  
  7.         var esActive ="";  
  8.              if(itemIdx <= 1)  
  9.                 esActive = "active";  
  10.                   
  11.                 _#-->  
Render the HTML structure with the list item information.
  1. <!--- HTML Goes Here -->  
  2.         <div class="item _#= esActive =#_">  
  3.         <img src="_#= Image =#_" alt="Chania" width="460" height="345">  
  4.         <div class="carousel-caption">  
  5.         <h3>_#= title =#_</h3>  
  6.         <p>_#= desc =#_</p>  
  7.         </div>  
  8.         </div>  
  9.   
  10. <!—End of HTML-->  
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.     <head>  
  3.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  4.         <title>BannerItem</title>  
  5.         <!--[if gte mso 9]><xml>  
  6.         <mso:CustomDocumentProperties>  
  7.         <mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>  
  8.         <mso:MasterPageDescription msdt:dt="string">Displays an Banner Item.</mso:MasterPageDescription>  
  9.         <mso:ContentTypeId msdt:dt="string">0x0101002039C03B61C64EC4A04F5361F385106603</mso:ContentTypeId>  
  10.         <mso:TargetControlType msdt:dt="string">;#SearchResults;#Content Web Parts;#</mso:TargetControlType>  
  11.         <mso:HtmlDesignAssociated msdt:dt="string">1</mso:HtmlDesignAssociated>  
  12.         <mso:ManagedPropertyMapping msdt:dt="string">'Title':'Title','ImageDescription':'ImageDescription','PublishingImage':'PublishingImage'</mso:ManagedPropertyMapping>  
  13.         <mso:CrawlerXSLFile msdt:dt="string"></mso:CrawlerXSLFile>  
  14.         <mso:HtmlDesignPreviewUrl msdt:dt="string"></mso:HtmlDesignPreviewUrl>  
  15.         <mso:HtmlDesignConversionSucceeded msdt:dt="string">True</mso:HtmlDesignConversionSucceeded>  
  16.           
  17.         <mso:HtmlDesignStatusAndPreview msdt:dt="string">http://webtest.adjd.gov.ae:801/_catalogs/masterpage/Display Templates/Content Web Parts/Arabic Display Templates/ArabicEventsItem1.html, Conversion successful.</mso:HtmlDesignStatusAndPreview>  
  18. </mso:CustomDocumentProperties>  
  19.         </xml><![endif]-->  
  20.     </head>  
  21.               
  22.     <body>  
  23.         <div>  
  24.             <!--#_  
  25.             var siteURL = SP.PageContextInfo.get_siteServerRelativeUrl();  
  26.             var itemIdx = ctx.CurrentItemIdx+1;  
  27.             var title = $getItemValue(ctx, "Title");   
  28.             var desc = $getItemValue(ctx, "ImageDescription");  
  29.             var Image = $getItemValue(ctx, "PublishingImage");  
  30.               
  31.               
  32.               
  33.             var esActive ="";  
  34.               
  35.               
  36.              if(itemIdx <= 1)  
  37.                 esActive = "active";  
  38.                   
  39.                 _#-->  
  40.               
  41.         <!--- HTML Goes Here -->  
  42.         <div class="item _#= esActive =#_">  
  43.         <img src="_#= Image =#_" alt="Chania" width="460" height="345">  
  44.         <div class="carousel-caption">  
  45.         <h3>_#= title =#_</h3>  
  46.         <p>_#= desc =#_</p>  
  47.         </div>  
  48.         </div>  
  49.   </div>  
  50.                   
  51.               
  52.           
  53.     </body>  
  54. </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 Webpart 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.

There 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 results will be successfully displayed into the page.

Up Next
    Ebook Download
    View all
    Learn
    View all