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

Forums / Developing with Sitefinity / Recent Blog posts

Recent Blog posts

3 posts, 0 answered
  1. Mike Mutek
    Mike Mutek avatar
    15 posts
    Registered:
    03 Dec 2009
    03 Dec 2010
    Link to this post
    Hey,
    (Forgive me, I am new to the CMS)
    I'm trying to incorporate Sam Grimmer's Recent Blog Entries control on my site but am running into an issue with the links it is generating. 

    using System;
    using System.Web;
    using System.Text;
    using System.Collections;
     
    using Telerik.Cms.Engine;
    using Telerik.Blogs;
     
    public partial class UserControls_RecentBlogs : System.Web.UI.UserControl
    {
        #region Properties
     
        /// <summary>
        /// Number of recent blog entries to display
        /// </summary>
        public int NumberOfBlogs
        {
            get
            {
                if (ViewState["NumberOfBlogs"] == null)
                    return 10;
     
                return int.Parse(ViewState["NumberOfBlogs"].ToString());
            }
            set { ViewState["NumberOfBlogs"] = value; }
        }
     
        #endregion
     
        #region Events
     
        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            BlogManager manager = new BlogManager();
            IList posts = manager.Content.GetContent();
             
            if (posts.Count > 0)
            {
                sb.Append("<ul class=\"recent\">");
     
                for (int i = 0; i <= NumberOfBlogs; i++)
                {
                    if (posts.Count < (i + 1)) break;
     
                    IContent item = (IContent)posts[i];
     
                    if (item.Status == ContentStatus.Published)
                    {
                        IBlog blog = manager.GetBlog(item.ParentID);
     
                        sb.Append("<li>");
                        sb.Append("<a href=\"" + GetUrl(blog.BlogPage.Replace(".aspx", "/") + item.UrlWithExtension) + "\">");
                        sb.Append(item.GetMetaData("Title").ToString());
                        sb.Append("</a>");
                        sb.Append("</li>");
                    }
                }
     
                sb.Append("</ul>");
                literal.Text = sb.ToString();
            }
        }
     
        #endregion
     
        #region Private Methods
     
        public string GetUrl(string value)
        {
            // if our value starts with ~/  
            // we return url 
            if (value.StartsWith("~/"))
                return this.ResolveUrl(value);
     
            if (value.StartsWith("["))
            {
                int idx = value.IndexOf("]");
                //getting the provider [Libraries] 
                string provider = value.Substring(1, idx - 1);
                string strId = value.Substring(idx + 1);
                Guid id = new Guid(strId);
     
                if (ContentManager.Providers.ContainsKey(provider))
                {
                    IContent cnt = ContentManager.Providers[provider].GetContent(id);
                    if (cnt != null)
                        return VirtualPathUtility.ToAbsolute(cnt.UrlWithExtension, this.Context.Request.ApplicationPath);
                }
            }
            return value.ToString();
        
     
        #endregion
    }


    The above code generates urls that contain "blog" rather than the actual blog name. 

    For example:

    http://<domain>/about/blog/*date*/*article_title.aspx*
    rather than
    http://<domain>/about/*my-blog-title*/*date*/*article_title.aspx*

    I'm also curious to know if it'd be easy to change this code to only look at a specific blog as well.

    Hopefully this makes sense!

    Any help would be greatly appreciated.

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    06 Dec 2010
    Link to this post
    Hi Mike,

    Here is where the url is constructed

    sb.Append("<li>");
     sb.Append("<a href=\"" + GetUrl(blog.BlogPage.Replace(".aspx", "/") + item.UrlWithExtension) + "\">");
    sb.Append(item.GetMetaData("Title").ToString());
    sb.Append("</a>");
    sb.Append("</li>");

    The code gets blog.BlogPage. You can use blog.Name  ( there is no Title) in the url format.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Mike Mutek
    Mike Mutek avatar
    15 posts
    Registered:
    03 Dec 2009
    06 Dec 2010
    Link to this post
    Thanks Ivan,

    I finally found what was going wrong.  The blog page was never set correctly when it was created.  After setting the correct blog page, the original code worked just fine.

    thanks for your assistance!
    Mike
Register for webinar
3 posts, 0 answered