Displaying SharePoint rating field in a DataGrid

Posted: 29th November 2011 in Cutstom Controls, Sharepoint 2010

One of my latest challenges was to display one of the new SharePoint 2010 social fields in a datagrid. At first this was just to display the field, but after the client seen the mouse-over functionality on the list item, they wanted to be able to assign a rating straight from the grid without having to go to the item itself. So, here’s how I went about it.

Firstly I created a custom control call as seen in the code sample below:

Rating Control
  1. [DefaultProperty("Rating")]
  2.     [ToolboxData("<{0}:RatingControl runat=server></{0}:RatingControl>")]
  3.     public class RatingControl : WebControl
  4.     {
  5.         protected override void OnInit(EventArgs e)
  6.         {
  7.             base.OnInit(e);
  8.             Page.RegisterRequiresControlState(this);
  9.         }
  10.  
  11.  
  12.         [Bindable(true)]
  13.         [Category("Binding")]
  14.         [DefaultValue("")]
  15.         [Localizable(true)]
  16.         public SPField RatingField
  17.         {
  18.             get
  19.             {
  20.                 SPField field = (SPField)ViewState["RatingField"];
  21.                 return field;
  22.             }
  23.  
  24.             set
  25.             {
  26.                 ViewState["RatingField"] = value;
  27.             }
  28.         }
  29.  
  30.  
  31.         [Bindable(true)]
  32.         [Category("Binding")]
  33.         [DefaultValue("")]
  34.         [Localizable(true)]
  35.         public int ListItemId
  36.         {
  37.             get
  38.             {
  39.                 return Convert.ToInt32(ViewState["ListItemId"]);
  40.             }
  41.  
  42.             set
  43.             {
  44.                 ViewState["ListItemId"] = value;
  45.             }
  46.         }
  47.  
  48.  
  49.         [Bindable(true)]
  50.         [Category("Binding")]
  51.         [DefaultValue("")]
  52.         [Localizable(true)]
  53.         public Guid ListId
  54.         {
  55.             get
  56.             {
  57.                 Guid id = (Guid)ViewState["ListId"];
  58.                 return id;
  59.             }
  60.  
  61.             set
  62.             {
  63.                 ViewState["ListId"] = value;
  64.             }
  65.         }
  66.  
  67.         protected override void CreateChildControls()
  68.         {
  69.  
  70.             if (this.ChildControlsCreated)
  71.                 return;
  72.  
  73.             
  74.             BaseFieldControl renderingcontrol = this.RatingField.FieldRenderingControl;
  75.             renderingcontrol.ControlMode = SPControlMode.Display;
  76.             renderingcontrol.ListId = this.ListId;
  77.             renderingcontrol.ItemId = this.ListItemId;
  78.             Controls.Add(renderingcontrol);
  79.             this.ChildControlsCreated = true;
  80.            
  81.            
  82.             
  83.        }

Next, after adding a new template column to the datagrid containing a place holder (DataGrid source), then binding the data source to the ListItemCollection returned. 

DataGrid Source
  1. <asp:TemplateColumn>
  2.          <HeaderTemplate>
  3.                 Rating
  4.             </HeaderTemplate>
  5.             <ItemTemplate>
  6.                 <asp:PlaceHolder runat="server" ID="RatingPlaceHolder"></asp:PlaceHolder>
  7.             </ItemTemplate>
  8.         </asp:TemplateColumn>

 

I create a new instance  of the rating control on the girdview OnItemDataBound event receiver and add it to the place holder.

OnItemDataBound
  1. protected void SearchResultsGridView_OnItemDataBound(object sender, DataGridItemEventArgs e)
  2.      {
  3.          if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  4.          {
  5.              Label lastModifiedLabel = (Label)e.Item.FindControl("LastModifiedLabel");
  6.              lastModifiedLabel.Text = Convert.ToDateTime(DataBinder.Eval(e.Item.DataItem, "ModifiedDate")).ToString("dd-MMM-yyyy");
  7.              PlaceHolder placeHolder = (PlaceHolder)e.Item.FindControl("RatingPlaceHolder");
  8.              ratingControl = new RatingControl();
  9.              ratingControl.RatingField = (SPField)DataBinder.Eval(e.Item.DataItem, "RatingField");
  10.              ratingControl.ListId = (Guid)DataBinder.Eval(e.Item.DataItem, "ListId");
  11.              ratingControl.ListItemId = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "Id"));
  12.              placeHolder.Controls.Add(ratingControl);
  13.          }
  14.     }

Job Done!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s