Wednesday, 27 February 2013

SharePoint 2013 - Add Ratings and Comments to Publishing Pages

Ok, I'm probably putting the horse before the cart here, as branding master and layout pages in SP2013 is a massive leap from what we were all used to in previous versions. But I just wanted to show how simple it is to get some of the new social features happening in publishing pages.

SP2013 offers a whole new way of branding. You can add page content fields and other elements to your layouts via the Snippets gallery in the new Design Manager. There are a couple of issues I have with this though:

  1. The generated code includes wads of redundant markup.
  2. The available fields are limited to what's in the default publishing page content type.

That being said, once you work out which controls are what and the prefix required, it's a breeze to just add these yourself. I'm stocking up my own gallery with trimmed snippets of my own which I may publish wholesale in another post.

Add Snippets

If you're familiar with SP2010 branding then adding these elements to 2013 is not significantly different. You just need to wrap your tags in the new comment format. First add SharePointPortalControls tag prefix.

<!--SPM:<%@ Register TagPrefix="SharePointPortalControls" Namespace="Microsoft.SharePoint.Portal.WebControls" Assembly="Microsoft.SharePoint.Portal, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%>-->

Then add our controls that use this prefix and namespace.

<!--MS:<SharePointPortalControls:AverageRatingFieldControl ID="PageRatingControl" FieldName="AverageRating" runat="server"/>-->
<!--MS:<SharePointPortalControls:SocialCommentControl ID="CommentControl" runat="server"/>-->

It's as simple as that. Drop them in a div, add a touch of css styling and voila!

20 comments:

Andreas Thumfart said...

Isn't that HTML-comment format only for master pages?

I have added the comment and rating to publishing page layouts the usual way but experience the problem, that I can't comment on pages which are published (or checked-in if versioning is off).

BB said...

Some great articles you have here.

Thomas is right you are missing a % in your first snippet on the end.

Took me some time to realize not to add the TagPrefix at the top of the (html design) page :-) The generated .aspx will have it at the top but otherwise will get an error: Unknown server tag 'SharePointPortalControls:AverageRatingFieldControl'.

Thanks again.

Anonymous said...

Object reference not set to an instance of an object.

I have encountered this error while adding the first snippet "Registering the dll".

Also how to link the rating control to specific item.

Thanks in advance!

Paul Noone said...
This comment has been removed by the author.
Anonymous said...

Awesome!

Anonymous said...

Hi

How to get this snippet working in item style of SP 2013? am trying to alter a html file of item style for Search results.

I added the said snippet, but nothing is seen in search results.

Paul Noone said...

This is ASP markup for a Layout Page. You cannot add it directly to a page via an HTML web part or field. You need to add it as a Snippet in the Design Manager.

David Arango said...

This is very helpful!
I wanted to ask you, is there a way to add comments/discussion-like commenting to items in custom lists?

Thanks!

Paul Noone said...

Hi David. Not using these placeholders. They will only work in layout pages.

You could create a separate list for comments and add its EditForm to the DispForm for your custom list.

With a little CSOM javascript, you'd then grab the item ID and add it to the comment item.

In this way you could also add a filtered list view of your comments for each DispForm item.

Hope that makes sense.

Anonymous said...

Hi Added the lines which you posted in html page layout. added it to pagelayout gallery. .aspx page generated. created a new page using this page layout. the controls are not displayed. please help.

Paul Noone said...

Are you receiving an error when you preview the layout in Design Manager?

Make sure you paste all the code as a single block where you want it to appear in your page.

You might also consider wrapping the tags in a DIV with a unique ID so you can see what's going on in the generated source code.

Gaëtan said...

Hi Paul,

Do you know how these comments can be indexed?

Thanks

Paul Noone said...

Hi Gaëtan,

Do you mean by search service?

Same as with any list. You need to make at least one of the fields indexable.

David Arango said...

Thanks Paul! it does make sense, however I'm trying to tap so sharepoint's native commenting, getting a publishing page to have comments and likes just like a blog would, and being able to see the number of comments and likes later on the page library view.
Have you been able to achieve something similar with this workaround?

By the way, this code adds Rating.


Would you happen to know the Controls or ID for the LIKE function?

Thanks again!

Anonymous said...

I have a cross-publishing site working and I'm trying to add the star control to my category item reuse (term-driven) layout page on my publishing site. Currently I see the ratings showing as 4.333333 or whatever value. I'm looking at the code snippets on the html page.

Using your code on the page layout doesn't cause any errors, but it only shows "rating not available".

Any suggestions?

Paul Noone said...

Hi Anonymous. Given that it's a term-driven page and probably utilising the same URL, I don't see this working in a hurry.

If anything it's likely to show rating value against the single page for all ratings. If you look at the Pages library, you'll only get one rating possibility against that page.

Hope that makes sense.

Paul Noone said...

Hi David,

I don't the control IDs off-hand and haven't tried this with publishing pages, although it should be possible.

I'd start by analysing the template page for Blogs and see what you can pull from that.

Satyam Dwivedi said...

Hi,
Thanks for the nice post and making life little easy for sharepoint developers.

How ever i have one question.

1. How to set alert, when ever any comment is posted.


Thanks In Advance

Anonymous said...

Paul, thank you for sharing this excellent information. It's just what I was looking for!

Paul Noone said...

Satyam, you can't set alerts. Comments is not a single list. It's tied to the social commenting feed for My Sites.