Share via


jQuery Accordion Using Content Search Web Part In SharePoint Server 2013

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 “Accordion” that contains title and description.
http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/jquery-accordion-using-content-search-web-part-in-sharepoint-server-2013/Images/image001.jpg
http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/jquery-accordion-using-content-search-web-part-in-sharepoint-server-2013/Images/image002.jpg

Let’s 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 – Accordion Control Template

Upload the script and CSS into style library.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/jquery-accordion-using-content-search-web-part-in-sharepoint-server-2013/Images/image003.jpg

Add the scripts and CSS into Control template.  

  1. <script>  
  2. $includeScript(this.url, "~sitecollection/Style%20Library/Accordion/jquery-ui.css");  
  3. $includeCSS(this.url, "~sitecollection/Style%20Library/Accordion/jquery-1.12.4.js");  
  4. $includeScript(this.url, "~sitecollection/Style%20Library/Accordion/jquery-ui.js");  
  5. </script>  
  6. <div id="accordion">  
  7. _#= ctx.RenderItems(ctx) =#_  
  8. </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>Accordion Control</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 menu 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://technologygeeks/sites/publishing/_catalogs/masterpage/Display Templates/Content Web Parts/banner/bannercontrol.html, Conversion successful.</mso:HtmlDesignStatusAndPreview>  
  16. </mso:CustomDocumentProperties>  
  17.         </xml><![endif]-->  
  18.     </head>  
  19.   
  20.     <body>          
  21.         <script>  
  22. $includeScript(this.url, "~sitecollection/Style%20Library/Accordion/jquery-ui.css");  
  23. $includeCSS(this.url, "~sitecollection/Style%20Library/Accordion/jquery-1.12.4.js");  
  24. $includeScript(this.url, "~sitecollection/Style%20Library/Accordion/jquery-ui.js");  
  25.         </script>  
  26.         <div>  
  27.             <!--#_if (!$isNull(ctx.ClientControl) && !$isNull(ctx.ClientControl.shouldRenderControl) && !ctx.ClientControl.shouldRenderControl()){return "";}  
  28.             ctx.ListDataJSONGroupsKey = "ResultTables";  
  29.             ctx["CurrentItems"] = ctx.ListData.ResultTables[0].ResultRows;  
  30.             var siteURL = SP.PageContextInfo.get_siteAbsoluteUrl();       
  31.               
  32.             AddPostRenderCallback(ctx, function() {  
  33.                 $.getScript(siteURL + "");  
  34.                   
  35.                 $( function() {  
  36.     var icons = {  
  37.       header: "ui-icon-circle-arrow-e",  
  38.       activeHeader: "ui-icon-circle-arrow-s"  
  39.     };  
  40.     $( "#accordion" ).accordion({  
  41.       icons: icons  
  42.     });  
  43.     $( "#toggle" ).button().on( "click", function() {  
  44.       if ( $( "#accordion" ).accordion( "option", "icons" ) ) {  
  45.         $( "#accordion" ).accordion( "option", "icons", null );  
  46.       } else {  
  47.         $( "#accordion" ).accordion( "option", "icons", icons );  
  48.       }  
  49.     });  
  50.   });  
  51.     });   
  52.             _#-->  
  53. <div id="accordion">  
  54.               
  55.     _#= ctx.RenderItems(ctx) =#_  
  56.                             
  57. </div>  
  58. <button id="toggle">Toggle icons</button>  
  59. </div>  
  60.     </body>  
  61. </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, "Description");  
  6.             var esActive ="";  
  7.                       
  8.             _#-->  

Render the HTML structure with the list item information.

  1. <!--- HTML Goes Here -->  
  2. <h3>_#= title =#_</h3>  
  3.          <div>  
  4.    <p>_#= desc =#_</p>  
  5.      </div>  

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>Accordion Item</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 Accordion 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','Description':'Description'</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://technologygeeks/sites/publishing/_catalogs/masterpage/Display Templates/Content Web Parts/banner/BannerItem.html, Conversion successful.</mso:HtmlDesignStatusAndPreview>  
  18. </mso:CustomDocumentProperties>  
  19.         </xml><![endif]-->  
  20.     </head>  
  21.   
  22.               
  23.     <body>  
  24.         <div>  
  25.             <!--#_  
  26.             var siteURL = SP.PageContextInfo.get_siteServerRelativeUrl();  
  27.             var itemIdx = ctx.CurrentItemIdx+1;  
  28.             var title = $getItemValue(ctx, "Title");   
  29.             var desc = $getItemValue(ctx, "Description");  
  30.                                   
  31.             _#-->  
  32.               
  33.         <!--- HTML Goes Here -->  
  34.           
  35.         <h3>_#= title =#_</h3>  
  36.          <div>  
  37.      <p>_#= desc =#_</p>  
  38.      </div>  
  39.   </div>  
  40. </body>  
  41. </html>  

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

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/jquery-accordion-using-content-search-web-part-in-sharepoint-server-2013/Images/image004.jpg

Now, insert some data into Accordion list from authoring site collection.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/jquery-accordion-using-content-search-web-part-in-sharepoint-server-2013/Images/image005.jpg

Run the Search Service application "Crawl" now.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/jquery-accordion-using-content-search-web-part-in-sharepoint-server-2013/Images/image006.jpg

Now, let’s go to SharePoint page.

Add a Content Search Web Part into this page.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/jquery-accordion-using-content-search-web-part-in-sharepoint-server-2013/Images/image007.png

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

Click Edit Web Part -> change query.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/jquery-accordion-using-content-search-web-part-in-sharepoint-server-2013/Images/image008.jpg

Provide number of items to display.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/jquery-accordion-using-content-search-web-part-in-sharepoint-server-2013/Images/image009.jpg

Choose the display templates.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/jquery-accordion-using-content-search-web-part-in-sharepoint-server-2013/Images/image010.jpg

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

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/jquery-accordion-using-content-search-web-part-in-sharepoint-server-2013/Images/image011.jpg

Click OK to complete the setup.

The following is the final result.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/jquery-accordion-using-content-search-web-part-in-sharepoint-server-2013/Images/image012.jpg

Click on the second topic.

http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/jquery-accordion-using-content-search-web-part-in-sharepoint-server-2013/Images/image013.jpg

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.

Back to Top