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

Forums / Developing with Sitefinity / Need help asap: How to retrieve the ID of the selected blogpost in a blog

Need help asap: How to retrieve the ID of the selected blogpost in a blog

10 posts, 0 answered
  1. Roland
    Roland avatar
    45 posts
    Registered:
    23 Feb 2009
    02 Mar 2009
    Link to this post
    How will I be able to get the primary key or ID of the blogpost that I selected in the blogpostlist..

    Here is the scenario..

    I dragged a blogpost control on my page and then when I previewed it it will show all the post that I created. When I clicked one of the links of a specific post
     say "Full Story", it will be redirected to a page displaying the details of the post.

    Where can I get the ID of that post in the redirected page or how I will get it.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    02 Mar 2009
    Link to this post
    Hi Roland,

    Thank you for using our services.

    You can get the id programmatically as below:

          BlogManager blogManager = new BlogManager("Blogs"); 
            IList ListOfAllBlogs = blogManager.GetBlogs(); 
            foreach (IBlog blog in ListOfAllBlogs) 
            { 
                if (blog.Name == "MyBlog"
                { 
                    Guid parentID = blog.ID; 
                    IList content = blogManager.GetPosts(string.Empty, blog.ID); 
                     
                    foreach (IContent post in content) 
                    { 
                        string title = (string)post.GetMetaData("Title"); 
                        if (title == "MyPost"
                        { 
                            Response.Write(post.ID.ToString() + "<br />" + post.GetMetaData("Title") + "<br />"); 
                        } 
                    } 
     
                } 
            } 

    You can get the ID from the database

    select ContentID from sf_GCMetaData where ShortText = 'MyPOST' AND Application = '/Blogs' 

    The easiest way is changing the urlRewriteFormat property for Blogs provider in the web.config as the example below demonstrates:

    urlRewriteFormat="[Publication_Date]/{GUID}.aspx" 

    I hope this helps.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Roland
    Roland avatar
    45 posts
    Registered:
    23 Feb 2009
    03 Mar 2009
    Link to this post
    Hi Ivan,
    Thanks its great it did the trick. How about if I want to get the ID of the blogpost in the blogpost list? Is there a metadata specific only for the ID of the blogpost so that I won't have to traverse each blogpost that I have?
  4. Roland
    Roland avatar
    45 posts
    Registered:
    23 Feb 2009
    03 Mar 2009
    Link to this post
    To follow up what I really wanted to happen is that I want to add another field in the BlogPostList on the page showing the rating of that specific post that is why I have to get the ID of the each post displayed.. I know I can do this by traversing in every items in BlogManager.GetBlogs and then GetPosts but is there an easier way for me say there is already a metadata for that specific post so I wouldn't have to traverse on all BlogPosts?
  5. Ivan
    Ivan avatar
    478 posts
    Registered:
    16 Jun 2015
    03 Mar 2009
    Link to this post
    Hello Roland,

    unfortunately there is no simple way to do this just through the template modification. However, you can do something along these lines:

    1. Create a new control and derive from BlogPosts control
    2. In this control override following method
    protected override void SetItemMetadata(Control itemContainer, IContent contentItem)  
    {  
        base.SetItemMetadata(itemContainer, contentItem);  
        ITextControl ratingControl = (ITextControl)itemContainer.FindControl("ratingControl");  
        ratingControl.Text = contentItem.Id.ToString();  
    3. Use this control instead of BlogPosts control on the pages

    RatingControl is obviously your control which is located in the template and has id="ratingControl". There is no need for it to implement ITextControl interface - you can make it anything you wish.

    I hope you'll find this information helpful. Let us know if there is anything else we can do for you.

    Kind regards,
    Ivan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Roland
    Roland avatar
    45 posts
    Registered:
    23 Feb 2009
    04 Mar 2009
    Link to this post
    Can you guide me on how I would be able to derive the BlogPost Control?
  7. Roland
    Roland avatar
    45 posts
    Registered:
    23 Feb 2009
    04 Mar 2009
    Link to this post
    Thanks for the idea Ivan...

    I tried to create an asp.net server control and derive it from BlogPost control and tried to override the setitemmetadata method in your example..However I was unable to load the post. Can you guide me on how I will accomplish your example from your previous reply.
  8. Ivan
    Ivan avatar
    478 posts
    Registered:
    16 Jun 2015
    04 Mar 2009
    Link to this post
    Hi Roland,

    please follow these steps:

    1. Make sure your project has App_Code folder. If not, right-click on the Website in your project (in Visual Studio) and then go to "Add ASP.NET folder" and then click on "App_Code"
    2. Add new class to the App_Code folder and name it "CustomBlogPosts"
    3. A If you are NOT working with Sitefinity 3.6 paste following in the class (delete anything that was there when you created a class):
    using Telerik.Blogs.WebControls;  
    using System.Web.UI;  
    using Telerik.Cms.Engine;  
     
    /// <summary>  
    /// Custom implementation of BlogPosts  
    /// </summary>  
    public class CustomBlogPosts : BlogPosts  
    {  
        /// <summary>  
        /// Sets metadata controls for the post that is currently binding.  
        /// This method is invoked when content item is data bound.  
        /// </summary>  
        /// <param name="itemContainer">The control that contains the item layout template.</param>  
        /// <param name="contentItem">IContent object representing the content data.</param>  
        protected override void SetItemMetadata(Control itemContainer, IContent contentItem)  
        {  
            base.SetItemMetadata(itemContainer, contentItem);  
            ITextControl ratingControl = (ITextControl)itemContainer.FindControl("ratingControl");  
            ratingControl.Text = contentItem.ParentID.ToString();  
        }   
         
    3. B If you ARE WORKING with Sitefinity 3.6 paste following in the class (delete anything that was there when you created the class)
    using Telerik.Blogs.WebControls;  
    using System.Web.UI;  
    using Telerik.Cms.Engine;  
     
    /// <summary>  
    /// Custom implementation of BlogPosts  
    /// </summary>  
    public class CustomBlogPosts : BlogPosts  
    {  
        /// <summary>  
        /// Gets the type from the assembly containing the embedded localization resource.  
        /// Override if embedded templates are using ASP.NET localization.  
        /// </summary>  
        /// <value></value>  
        public override System.Type LocalizationAssemblyInfo  
        {  
            get 
            {  
                return typeof(BlogPosts);  
            }  
            set 
            {  
                base.LocalizationAssemblyInfo = value;  
            }  
        }  
     
        /// <summary>  
        /// Gets the type from the assembly containing the embedded resources.  
        /// Cannot be null reference.  
        /// </summary>  
        /// <value></value>  
        public override System.Type AssemblyInfo  
        {  
            get 
            {  
                return typeof(BlogPosts);  
            }  
            set 
            {  
                base.AssemblyInfo = value;  
            }  
        }  
     
        /// <summary>  
        /// Sets metadata controls for the post that is currently binding.  
        /// This method is invoked when content item is data bound.  
        /// </summary>  
        /// <param name="itemContainer">The control that contains the item layout template.</param>  
        /// <param name="contentItem">IContent object representing the content data.</param>  
        protected override void SetItemMetadata(Control itemContainer, IContent contentItem)  
        {  
            base.SetItemMetadata(itemContainer, contentItem);  
            ITextControl ratingControl = (ITextControl)itemContainer.FindControl("ratingControl");  
            ratingControl.Text = contentItem.ParentID.ToString();  
        }   
         
    }  
     

    4. Open web.config file and locate <toolboxControls> collection
    5. Add following line inside of the <toolboxControls> collection (after the <clear /> declaration):
    <add name="Custom Blog Posts"   
         section="Blogs"   
         description="Custom implementation of the BlogPosts control" 
         type="CustomBlogPosts, App_Code" /> 

    6. Make sure you have in the template control with ID="ratingControl" and which implements ITextControl interface (Label, Textbox and so on).

    If you are using version prior to Sitefinity 3.6 you can find blog templates in ~/Sitefinity/ControlTemplates/Blogs/ folder. If you are working with Sitefinity 3.6 please consult following articles for mapping templates:

    I hope you will find this information useful. Let us know if there is anything else we can do for you.

    Sincerely yours,
    Ivan
    the Telerik team


    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  9. Roland
    Roland avatar
    45 posts
    Registered:
    23 Feb 2009
    05 Mar 2009
    Link to this post
    Hi Ivan,
    Thanks for the information and it did work well however it shows the ID of the Blog but not the ID of that specific BlogPost. What is the missing one that we have? Thanks..
  10. Roland
    Roland avatar
    45 posts
    Registered:
    23 Feb 2009
    05 Mar 2009
    Link to this post
    Nevermind what I said.. I change the content.parentid.tostring() to content.id.tostring() and it did the trick.. Thank you very much Ivan for helping me out there 5 stars for you.. :)
Register for webinar
10 posts, 0 answered