1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Help with pseudo rating blog control

Help with pseudo rating blog control

14 posts, 0 answered
  1. Mark
    Mark avatar
    22 posts
    Registered:
    16 Sep 2012
    01 Nov 2011
    Link to this post
    Hello,

    I am looking for some assistance with creating a custom blog control that accesses values from "custom fields for blogs".

    Project Overview: The widget I'm creating will display a rating of a blog item.  The "rating" is set when the blog "post" is created and displayed graphically. The value for the rating comes from a custom field that the content editors see when they are creating the blog post within Sitefinity.  The control is just displaying data, so this isn't something the end users will need to interact with, so I'm not using the RadRating control.  I've got my ascx created along with the code-behind file. I've changed the directive in my ascx from "CodeBehind" to "CodeFile".  This is done per this post from Josh Morales. Currently, I have two public properties in my control so the content editors can modify the values.  The first public property is for the path to the rating image to use and the second property is for the rating number.  My question is how do I wire up the two custom fields from the blog posts to the public properties in the code-behind file?  What am I missing?  Am I headed in the right direction?

    Our shop is running Sitefinity 4.2.1650.0.

    Any assistance would be greatly appreciated.

    Thank you.

    Mark
  2. Svetoslav Petsov
    Svetoslav Petsov avatar
    456 posts
    Registered:
    24 Sep 2012
    04 Nov 2011
    Link to this post
    Hello Mark,

    Just to make sure we're on the same page here, can you please confirm that what you're trying to do is access the custom filed for your blog posts through code, is that correct? If so,  you can get the value of the custom fields in the codebehind, using the extension method GetValue (please add a reference to Telerik.Sitefinity.Model in your class):

    YourProperty = DataExtensions.GetValue(currentItem, "CustomFieldName"));
    where YourProperty is the name of the public property, currentItem is the current BlogPost item and "CustomFieldName" is the name of the CustomField that you added.
    If this is not the case, please send me your template, so I can get a better understanding of what you are trying to achieve. Greetings,
    Svetoslav Petsov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Mark
    Mark avatar
    22 posts
    Registered:
    16 Sep 2012
    13 Nov 2011
    Link to this post
    Svetoslav,

    Thank you for your reply.  Yes, that is what I'm trying to do.  So, how do I get "currentItem" assuming currentItem is a blog post?

    Thank you.

    Mark
  4. Svetoslav Petsov
    Svetoslav Petsov avatar
    456 posts
    Registered:
    24 Sep 2012
    14 Nov 2011
    Link to this post
    Hello Mark,

     Here's a method that is used inside Sitefinity to resolve items by URL:

    protected void Page_Load(objectsender, EventArgs e)
            {
      
                this.ResolveDetailItemFromUrl();   
            }
      
            privateBlogPost currentItem;
            publicBlogPost CurrentItem
            {
                get
                {
                    returnthis.currentItem;
                }
                set
                {
                    this.currentItem = value;
                }
            }
      
      
            /// <summary>
            /// Resolves the detail item from URL.
            /// </summary>
            protectedvirtualvoidResolveDetailItemFromUrl()
            {
                var itemUrl = this.GetUrlParameterString(true);
                if(itemUrl != null)
                {
                    stringredirectUrl;
      
                    var manager = BlogsManager.GetManager();
                    var item = (BlogPost)manager.GetItemFromUrl(typeof(BlogPost), itemUrl, true, outredirectUrl);
                    if(item != null)
                    {
                        this.CurrentItem = item;
                    }
                }
            }

    Let me know if you have any questions on the code.

    Kind regards,
    Svetoslav Petsov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Mark
    Mark avatar
    22 posts
    Registered:
    16 Sep 2012
    14 Nov 2011
    Link to this post
    Svetoslav,

    I'm now getting a Parser Error in my aspx page.  I'm trying to test out my code as a user control in an aspx page before I use it as a Sitefinity blog widget.  Here is the error:

    Parser Error

    Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 

    Parser Error Message: The base class includes the field 'BlogRating1', but its type (WebApplication2.Controls.BlogRating) is not compatible with the type of control (ASP.controls_blograting_ascx).

    Source Error: 

    Line 40: 
    Line 41: <asp:Content ID="Content11" ContentPlaceHolderID="ContentBody1" runat="server">
    Line 42:     <uc3:BlogRating1 ID="BlogRating1" runat="server" />
    Line 43:     
    Line 44: </asp:Content>

    Source File: /BlogRatingDemo.aspx    Line: 42 

    Here is my ascx code behind

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Sitefinity.Model;
    using System.Runtime.Serialization;
    using Telerik.Sitefinity.Modules.Blogs;


    namespace WebApplication2.Controls
    {
        public partial class BlogRating : System.Web.UI.UserControl, IDynamicFieldsContainer
        {
            private int _RatingStarsToShow = 0;
            private string _Stars = string.Empty;
            private string _RatingStarImageUrl = string.Empty;
            private BlogRating currentItem;


            public BlogRating CurrentItem
            {
                get { return this.currentItem; }
                set { this.currentItem = value; }
            }


            public int RatingStarsToShow
            {
                get { return _RatingStarsToShow; }
                set { _RatingStarsToShow = value; }
            }


            public string RatingStarImageUrl
            {
                get { return _RatingStarImageUrl; }
                set { _RatingStarImageUrl = value; }
            }


            protected void Page_Load(object sender, EventArgs e)
            {
                RatingStarsToShow = (int)DataExtensions.GetValue(currentItem, "NumberStarsToShow");


                if (RatingStarsToShow > 5)
                {
                    RatingStarsToShow = 5;
                }


                for (int i = 1; i <= RatingStarsToShow; i++)
                {
                    string imageId = "Star" + i.ToString();


                    Image starImage = new Image();
                    starImage.ID = imageId;
                    starImage.ImageUrl = RatingStarImageUrl;


                    Control myControl = FindControl("stars");


                    myControl.Controls.Add(starImage);


                    //stars.Controls.Add(starImage);
                }


            }


            protected virtual void ResolveDetailItemFromUrl()
            {
                var itemUrl = this.GetUrlParameterString(true);


                if (itemUrl != null)
                {
                    string redirectUrl = string.Empty;


                    var manager = BlogsManager.GetManager();
                    var item = (BlogRating)manager.GetItemFromUrl(typeof(BlogRating), itemUrl, true, out redirectUrl);


                    if (item != null)
                    {
                        this.CurrentItem = item;
                    }
                }
            }
        }
    }

    Is my code-behind correct?  Should this work in a regular aspx page?

    Please help. Thank you.

    Mark
  6. Svetoslav Petsov
    Svetoslav Petsov avatar
    456 posts
    Registered:
    24 Sep 2012
    17 Nov 2011
    Link to this post
    Hello Mark,

     No, this won't work in an .aspx page, because it gets the browser's url and resolves the item by it. This means that no control can be resolved by the .aspx page's url. 
    So, to test this code, please put it in an .ascx control and place it on a Sitefinity page with a BlogPosts widget on it. So, when you click on one of the items and it loads on the same page, the URL will be what you need and the item will get resolved by it.

    Kind regards,
    Svetoslav Petsov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Mark
    Mark avatar
    22 posts
    Registered:
    16 Sep 2012
    21 Nov 2011
    Link to this post
    Svetoslav,

    First off, thank you for your help thus far.  However, I'm still having a problem getting this to work.  I've added my custom user control into Sitefinity and copy and pasted my front end code into a custom blog widget.  I've followed the steps here to point the blog widget to my own template.  Per the instructions on this page, I've removed the Template Key and used the path to my template (my ascx file) .  When I try to preview, I get the "Error parsing the template" message. When I go back to edit the widget, the Template Key is back.  Is the Template Key property causing the parsing error since it overrides my custom template? If so, why does the Template Key keep getting added back to my widget?  Please let me know if you need more information to help troubleshoot this issue or need clarification.

    Our shop is running Sitefinity 4.2.1650.0.  

    Thank you,

    Mark
  8. Svetoslav Petsov
    Svetoslav Petsov avatar
    456 posts
    Registered:
    24 Sep 2012
    22 Nov 2011
    Link to this post
    Hi Mark,

     You have mapped the template properly, the Key gets set by default, but this now would be the new key of your mapped template. 
    The fact that you see an error shows that your template has been mapped. However, the error is inside the template, probably the control that you added cannot be rendered properly. 
    By the way, have you checked out my recent blog post on how to add a Rating control to blogs?
    http://www.sitefinity.com/blogs/svetoslavpetsov/posts/11-11-14/blog_rating_control_with_radrating_and_the_power_of_wcf_restful_services.aspx

    Greetings,
    Svetoslav Petsov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  9. Aaron Suzuki
    Aaron Suzuki avatar
    4 posts
    Registered:
    06 May 2010
    25 Jan 2012
    Link to this post
    Svetoslav,

    I found out that when I change the first line in my blog widget template


    from

    <%@ Control Language=
    "C#" %>
     
    to
     
    <%@ Control Language="C#" CodeFile="BlogRating.ascx.cs" %>
    or
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="BlogRating.ascx.cs" Inherits="WebApplication2.Controls.BlogRating" %>


    is when I get the "Error parsing the template" error message.  However, without this statement, my code file doesn't appear to even be referenced as I've got custom debug log messages being written to a log file on Page_Load in the control and those messages aren't getting written out.  If I remove the CodeFile attribute, the template loads just fine and my blogs display, but my custom code doesn't work.  EXACTLY how do I reference custom code in a widget.  The instructions here do not work.

    Thank you,
    Mark
  10. Svetoslav Petsov
    Svetoslav Petsov avatar
    456 posts
    Registered:
    24 Sep 2012
    30 Jan 2012
    Link to this post
    Hello,

     If you want to have a template with codebehind, you will have to map it to the relevant view of the widget, the way that it is shown in this blogpost:
    http://www.sitefinity.com/blogs/joshmorales/posts/11-05-10/mapping_external_templates_for_sitefinity_4_widgets.aspx 

    Another way for the mapping, that is not included in the blogpost, would be to go to Administration >> Settings >> Advanced >> Controls >> ViewMap and create new record, using Namespace.Class of your view(for the widget) and the virtual path to your external template.


    Regards,
    Svetoslav Petsov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  11. Aaron Suzuki
    Aaron Suzuki avatar
    4 posts
    Registered:
    06 May 2010
    30 Jan 2012
    Link to this post
    Svetoslav,

    That is exact same link I included in my previous post where I said those instructions DID NOT WORK.  

    Mark
  12. Svetoslav Petsov
    Svetoslav Petsov avatar
    456 posts
    Registered:
    24 Sep 2012
    31 Jan 2012
    Link to this post
    Hi Aaron,

     Have you tried the second way I suggested? It always works, as it maps the template globally. 
    Another way for the mapping, that is not included in the blogpost, would be to go to Administration >> Settings >> Advanced >> Controls >> ViewMap and create new record, using Namespace.Class of your view(for the widget) and the virtual path to your external template. 
    Can you try that and let me know if it worked for you? Thanks in advance.

    All the best,
    Svetoslav Petsov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  13. Aaron Suzuki
    Aaron Suzuki avatar
    4 posts
    Registered:
    06 May 2010
    02 Feb 2012
    Link to this post
    Svetoslav,

    I tried the ViewMap and it did not work.  Here is what I did:

    1. Navigated to Administration >> Settings >> Advanced >> Controls >> ViewMap
    2. Created a new ViewMap
      HostType = WebApplication2.Controls.BlogRating
      LayoutTemplatePath = 
    ~/App_Data/Sitefinity/WebsiteTemplates/_SharedControls/BlogRating.ascx

    3. Did an iisreset on my system
    4. I removed the CodeFile entry from line 1 in the widget, so it reads
    <%@ Control Language="C#" %>

    I viewed the page.  I can see the blogs, just not the rating stars.  When viewing the page source, the src attribute on the image is empty so the widget is not reading my custom field.

    Do I have to change anything in the widget properties?  I could not find any documentation stating if I did or not.  The documentation I found implied that after resetting the site, it would just work.

    Please advise further.

    Mark


  14. Svetoslav Petsov
    Svetoslav Petsov avatar
    456 posts
    Registered:
    24 Sep 2012
    02 Feb 2012
    Link to this post
    Hi Aaron,

     I apologize - I didn't understand that you are trying to add codebehind to a Custom control's. I thought that you were trying to map a template for the default blog widget If your control is a custom one (inherits from SimpleView) you cannot map a template for it, as it is not exposed in the definitions.
    If you are trying to map a template for the default widget, then the proper registration would be:
    HostType: Telerik.Sitefinity.Modules.Blogs.Web.UI.Public.MasterPostsView
    LayoutTemplatePath: the path to your template.

    Regards,
    Svetoslav Petsov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
14 posts, 0 answered