jQuery News Ticker Using Content Search Web Part In SharePoint Server 2013

0
108
views

In the first step, let’s create the Authoring and Publishing site collections.

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

Open Authoring site collection.

Create a list named Breaking News, which contains title and description.

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

  • Control Template
    It provides the overall structure (layout) of 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 to display a jQuery News Ticker control and a jQuery News Ticker item.

Code – News Control Template

Upload the script and CSS into style library.

Add the scripts and CSS into Control template.

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

Now, let’s define HTML layout to Control Template.

  1. <div class=“ticker-container”>
  2.   <div class=“ticker-caption”>
  3.     <p>Breaking News</p>
  4.   </div>
  5. <ul>
  6.     _#= ctx.RenderItems(ctx) =#_
  7.      </ul>
  8. </div>

The full code looks, as shown below.

  1. <html xmlns:mso=“urn:schemas-microsoft-com:office:office” xmlns:msdt=“uuid:C2F41010-65B3-11d1-A29F-00AA00C14882”>
  2.     <head>
  3.         <title>News Control</title>
  4.         <!–[if gte mso 9]><xml>
  5.         <mso:CustomDocumentProperties>
  6.         <mso:TemplateHidden msdt:dt=“string”>0</mso:TemplateHidden>
  7.         <mso:MasterPageDescription msdt:dt=“string”>Display a News Control</mso:MasterPageDescription>
  8.         <mso:ContentTypeId msdt:dt=“string”>0x0101002039C03B61C64EC4A04F5361F385106601</mso:ContentTypeId>
  9.         <mso:TargetControlType msdt:dt=“string”>;#SearchResults;#Content Web Parts;#</mso:TargetControlType>
  10.         <mso:HtmlDesignAssociated msdt:dt=“string”>1</mso:HtmlDesignAssociated>
  11.         <mso:HtmlDesignConversionSucceeded msdt:dt=“string”>True</mso:HtmlDesignConversionSucceeded>
  12.         <mso:CrawlerXSLFile msdt:dt=“string”></mso:CrawlerXSLFile>
  13.         <mso:HtmlDesignPreviewUrl msdt:dt=“string”></mso:HtmlDesignPreviewUrl>
  14.         <mso:HtmlDesignStatusAndPreview msdt:dt=“string”>http://technologygeeks/sites/publishing/_catalogs/masterpage/Display Templates/Content Web Parts/banner/bannercontrol.html, Conversion successful.</mso:HtmlDesignStatusAndPreview>
  15. </mso:CustomDocumentProperties>
  16.         </xml><![endif]–>
  17.     </head>
  18.     <body>
  19.         <script>
  20.         $includeScript(this.url, “~sitecollection/Style%20Library/news%20ticker/jquery.min.js”);
  21.             $includeScript(this.url, “~sitecollection/Style%20Library/news%20ticker/ticker.js”);
  22.              $includeCSS(this.url, “~sitecollection/Style%20Library/news%20ticker/ticker.css”);
  23.         </script>
  24.         <div>
  25.             <!–#_if (!$isNull(ctx.ClientControl) && !$isNull(ctx.ClientControl.shouldRenderControl) && !ctx.ClientControl.shouldRenderControl()){return “”;}
  26.             ctx.ListDataJSONGroupsKey = “ResultTables”;
  27.             ctx[“CurrentItems”] = ctx.ListData.ResultTables[0].ResultRows;
  28.             var siteURL = SP.PageContextInfo.get_siteAbsoluteUrl();
  29.             AddPostRenderCallback(ctx, function() {
  30.                 $.getScript(siteURL + “”);
  31.     });
  32.             _#–>
  33. <div class=“ticker-container”>
  34.   <div class=“ticker-caption”>
  35.     <p>Breaking News</p>
  36.   </div>
  37.   <ul>
  38.     _#= ctx.RenderItems(ctx) =#_
  39.      </ul>
  40. </div>
  41. </div>
  42.     </body>
  43. </html>

Let’s go to Item template, declare the managed properties and the 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.             _#–>

Render HTML structure with the list item information.

  1. <!— HTML Goes Here –>
  2.         <div>
  3.       <li><span>_#= title =#_ <a href=“#”>_#= desc =#_</a></span></li>
  4.     </div>

Overall Item template code looks, as shown below.

  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>News 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 News 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.         <mso:HtmlDesignStatusAndPreview msdt:dt=“string”>http://technologygeeks/sites/publishing/_catalogs/masterpage/Display Templates/Content Web Parts/banner/BannerItem.html, Conversion successful.</mso:HtmlDesignStatusAndPreview>
  17. </mso:CustomDocumentProperties>
  18.         </xml><![endif]–>
  19.     </head>
  20. <script>
  21. </script>
  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, “Description”);
  29.             _#–>
  30.         <!— HTML Goes Here –>
  31.     <div>
  32.       <li><span>_#= title =#_ <a href=“#”>_#= desc =#_</a></span></li>
  33.     </div>
  34. </div>
  35. </body>
  36. </html>

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

Now, we are going to insert some news into Breaking News list from Authoring site collection.

Run the search Service Application Crawl now.

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 Authoring site collection.

Click Edit Web Part -> change query.

Provide the number of items to display.

Choose the display templates.

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

Click Apply and OK to complete the setup.

The final result is given below.


Note

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

Please follow and like us:
0

LEAVE A REPLY

Please enter your comment!
Please enter your name here