Using Repeater Control In SharePoint (News Rollup WebPart)

Open SharePoint Site.

Open SharePoint Site

Create a simple list named “News”.

Create a simple list

Open Visual studio.

Create a new SharePoint 2013 empty project.

Add a visual webpart into the SharePoint solution named “News rollup”.

visual studio

news

Now add a repeater control into the News Rollup.ascx.

Code:

  1. <asp:Repeater ID="rptsmartspeak" runat="server">  
  2.    <ItemTemplate>  
  3.           
  4.         <%#DataBinder.Eval(Container.DataItem,"Date")%>   
  5.         <br />  
  6.         <h2 style="padding-top: 5px; font-weight: 700; font-size: 12px;"><%#DataBinder.Eval(Container.DataItem,"Title")%></h2>  
  7.         <br />  
  8.         <br />  
  9.         <p  style="margin-top: -39px;">  
  10.             <%#FormatDescription(Container.DataItem,"Description")%>  
  11.                                       
  12.         </p>  
  13.        <hr />  
  14.     </ItemTemplate>  
  15. </asp:Repeater>  
Code

To bind the data into the repeater from SharePoint lists.

Download CAML Query builder.

Click here to download U2U CAML Query builder.

After installation, I am going to generate a query to retrieve the Isactive values from the news list.

retrieve the Isactive values

Code
  1. using Microsoft.SharePoint;  
  2. using System;  
  3. using System.ComponentModel;  
  4. using System.Web.UI.WebControls.WebParts;  
  5.   
  6. namespace News.News_Rollup  
  7. {  
  8.     [ToolboxItemAttribute(false)]  
  9.     public partial class News_Rollup : WebPart  
  10.     {  
  11.         // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution  
  12.         // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready  
  13.         // for production. Because the SecurityPermission attribute bypasses the security check for callers of  
  14.         // your constructor, it's not recommended for production purposes.  
  15.         // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]  
  16.         public News_Rollup()  
  17.         {  
  18.         }  
  19.   
  20.         protected override void OnInit(EventArgs e)  
  21.         {  
  22.             base.OnInit(e);  
  23.             InitializeControl();  
  24.         }  
  25.   
  26.         protected void Page_Load(object sender, EventArgs e)  
  27.         {  
  28.             if(!Page.IsPostBack)  
  29.             {  
  30.                 Bindspeaker();  
  31.             }  
  32.         }  
  33.         private void Bindspeaker()  
  34.         {  
  35.             using (SPSite site = new SPSite(SPContext.Current.Site.Url))  
  36.             {  
  37.                 using (SPWeb web = site.OpenWeb())  
  38.                 {  
  39.                     SPList list = web.Lists["News"];  
  40.                     if (list != null)  
  41.                     {  
  42.                         SPQuery query = new SPQuery();  
  43.                         query.Query = "<Where><Eq><FieldRef Name='Isactive' /><Value Type='Boolean'>1</Value></Eq></Where>";  
  44.                         SPListItemCollection collitem = list.GetItems(query);  
  45.   
  46.                         rptsmartspeak.DataSource = collitem.GetDataTable();  
  47.                         rptsmartspeak.DataBind();  
  48.   
  49.                     }  
  50.                 }  
  51.             }  
  52.         }  
  53.     }  
  54. }  
Now I am going to do some customization of my repeater control to view the news stylish.

Full code

News Rollup.ascx
  1. <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>  
  2. <%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>   
  3. <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>   
  4. <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>  
  5. <%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>  
  6. <%@ Import Namespace="Microsoft.SharePoint" %>   
  7. <%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>  
  8. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="News Rollup.ascx.cs" Inherits="News.News_Rollup.News_Rollup" %>  
  9. <div style="height:150px; width:200px" >  
  10. <table  style="width:100%" >  
  11.     <tr>  
  12.         <td  style="width:10%" ></td>  
  13.         <td  style="width:80%" >  
  14.              <marquee  behavior="scroll" direction="up" scrollamount="3" onmouseover="stop();"  onmouseout="start()";>  
  15.     <div class="smartspeak" style="padding-right: 20px; height:300px; width:280px;">  
  16.  <asp:Repeater ID="rptsmartspeak" runat="server">  
  17.                    <ItemTemplate>  
  18.                           
  19.                         <%#DataBinder.Eval(Container.DataItem,"Date")%>   
  20.                         <br />  
  21.                         <h2 style="padding-top: 5px; font-weight: 700; font-size: 12px;"><%#DataBinder.Eval(Container.DataItem,"Title")%></h2>  
  22.                         <br />  
  23.                         <br />  
  24.                         <p  style="margin-top: -39px;">  
  25.                             <%#FormatDescription(Container.DataItem,"Description")%>  
  26.                                                       
  27.                         </p>  
  28.                        <hr />  
  29.                     </ItemTemplate>  
  30.                 </asp:Repeater>  
  31.          </div>         
  32.         </marquee>  
  33.           </td>  
  34.         <td  style="width:10%" ></td>  
  35.   
  36.     </tr>  
  37. </table>  
News Rollup.ascx.cs
  1. using Microsoft.SharePoint;  
  2. using System;  
  3. using System.ComponentModel;  
  4. using System.Web.UI;  
  5. using System.Web.UI.WebControls.WebParts;  
  6.   
  7. namespace News.News_Rollup  
  8. {  
  9.     [ToolboxItemAttribute(false)]  
  10.     public partial class News_Rollup : WebPart  
  11.     {  
  12.         // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution  
  13.         // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready  
  14.         // for production. Because the SecurityPermission attribute bypasses the security check for callers of  
  15.         // your constructor, it's not recommended for production purposes.  
  16.         // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]  
  17.         public News_Rollup()  
  18.         {  
  19.         }  
  20.   
  21.         protected override void OnInit(EventArgs e)  
  22.         {  
  23.             base.OnInit(e);  
  24.             InitializeControl();  
  25.         }  
  26.   
  27.         protected void Page_Load(object sender, EventArgs e)  
  28.         {  
  29.             if (!Page.IsPostBack)  
  30.             {  
  31.                 Bindspeaker();  
  32.             }  
  33.         }  
  34.         private void Bindspeaker()  
  35.         {  
  36.             using (SPSite site = new SPSite(SPContext.Current.Site.Url))  
  37.             {  
  38.                 using (SPWeb web = site.OpenWeb())  
  39.                 {  
  40.                     SPList list = web.Lists["News"];  
  41.                     if (list != null)  
  42.                     {  
  43.                         SPQuery query = new SPQuery();  
  44.                         query.Query = "<Where><Eq><FieldRef Name='Isactive' /><Value Type='Boolean'>1</Value></Eq></Where>";  
  45.                         SPListItemCollection collitem = list.GetItems(query);  
  46.   
  47.                         rptsmartspeak.DataSource = collitem.GetDataTable();  
  48.                         rptsmartspeak.DataBind();  
  49.   
  50.                     }  
  51.                 }  
  52.             }  
  53.         }  
  54.         protected string FormatDescription(object item, string fieldName)  
  55.         {  
  56.             if (string.IsNullOrEmpty(DataBinder.Eval(item, fieldName).ToString()))  
  57.             {  
  58.                 return string.Empty;  
  59.             }  
  60.             else  
  61.             {  
  62.                 string text = DataBinder.Eval(item, fieldName).ToString();  
  63.                 if (text.Length < 250)  
  64.                     return string.Format("{0}", text);  
  65.                 return string.Format("{0}", text.Substring(0, 250));  
  66.             }  
  67.         }  
  68.   
  69.     }  
  70. }  
Now deploy the solution.

After deploying, the webpart adds some news contents into the SharePoint list.

deploy

SharePoint list

The final result is.

result

Now enjoy Stylish news webpart at free of cost with dynamic features.