The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.
Let me first start by wrapping up what has to be done. Since we are going to rate content items we would need to associate the Rating Control with the content item being rated. For these purposes we will need to create two controls - one IScriptControl which will wrap the functionality of the RadRating control and tie it up to a content item and a custom ContentView control, which will pass the content item id and provider name to the first control. To rate content items without making post backs we would also need to create Web Service to handle this. Finally we should keep track of who has rated which item in order to prevent double rating.
For the purposes of this sample I will use the Generic Content module, however this can be easily integrated with any Generic Content based module. Lets start by adding the necessary meta fields to the meta fields collection of Generic Content module:
The next step is to build our custom IScript control, create a control template for it and expose content item information to our client control. Code code bellow is for the server part of our IScript control:
This is the markup of our control:
And lastly the client code:
After we have created our SitefinityRating control, it is time to customize the ContentView control to pass the content item ID and the provider name to it. We have to create a control which inherits from ContentView override the LayoutTemplatePaths and point to custom templates to which we have added our SitefinityRating control, finally we should override the SetItemMetadata and OnPreRender methods. The first one is to pass id and provider name. The second one is to add a reference to the web service which is going to update the ratings:
Now we have to create the WebService and implement the RateContent WebMethod which is will update content ratings :
In the above code you have probably noticed that I am using LINQ to retrieve and store data about which user has rated which item. For this I have created a table in my DB to store each user's unique identifier and the content item ID. Both of those values are of type Guid. I have set those two values that I retain to be a composite primary key for this table. This table is added to my Sitefinity DB and is called sf_Ratig. Bellow is a snapshot of how my table looks in MS SQL.
Lastly you have to add hidden text boxes to the control template for editing content items and map this template, in order to prevent loosing the rating values once content item is being edited. The control template is ~/Sitefinity/Admin/ControlTemplates/Generic_Content/ContentEditView.ascx. I have added this to my template:
All you have to do now is to add the control to your controls collection:
You can download all sample code and other related files from the following link: SitefinityRating.
Please, note that after you have added new meta fields to the Generic Content module items which you previously had will not contain the default values for those meta fields. You will have to go through all content items and "dummy" edit and save them.
View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.
Subscribe to get all the news, info and tutorials you need to build better business apps and sites
Copyright © 2019 Progress Software Corporation and/or its subsidiaries or affiliates.
All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks for appropriate markings.