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

Forums / Developing with Sitefinity / Get latest Blog Posts from all blogs

Get latest Blog Posts from all blogs

6 posts, 0 answered
  1. Roland
    Roland avatar
    12 posts
    Registered:
    07 Apr 2009
    19 Mar 2010
    Link to this post
    Hi,

    I am looking for a way to get the latest published blog posts from all blogs and bind them to a repeater.

    In particular, I was trying to get latest posts in one line of code: (is it possible?)
    IList latestfourposts = blogManager.Content.GetContent(0, 4, "Created_Date DESC", ContentStatus.Published, ???Guid[] ParentIDs???);
      
      
    // populate the repeater
    rptblogPosts.DataSource = latestfourposts;
    rptblogPosts.DataBind();

    I am not sure what I can pass in as the last parameter to get from all blogs.

    Also, what do I have to do to get the properties of the posts to the repeater?

    Your help is much appreciated.

    Thanks,
    Roland
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    19 Mar 2010
    Link to this post
    Hello Roland,

    Below is a sample code that illustrates how to get the recent five posts and bind it to a repeater control

    <asp:Repeater runat="server" ID="blogPostsRepeater">
        <ItemTemplate>
           <asp:Label runat="server"  ID="PostTitle"></asp:Label><br />
           <asp:Label runat="server"  ID="Content"></asp:Label>
           <asp:Label runat="server" ID="PostAuthor"></asp:Label>
        </ItemTemplate>
    </asp:Repeater>

    protected void Page_Load(object sender, EventArgs e)
       {
     
           ContentManager manager = new ContentManager("Blogs");
           Guid blogID = new Guid("9ece05da-0a79-49b5-8666-860e05a7a524");
           blogPostsRepeater.DataSource = manager.GetContent(0, 5, "Publication_Date DESC", blogID);
           blogPostsRepeater.ItemDataBound += new RepeaterItemEventHandler(blogPostsRepeater_ItemDataBound);
           blogPostsRepeater.DataBind();
          
       }
     
       void blogPostsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
       {
           if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
           {
               IContent obj = e.Item.DataItem as IContent;
               if (obj != null)
               {
                  Label PostTitle = (Label)e.Item.FindControl("PostTitle");
                  if (PostTitle != null)
                  {
                      PostTitle.Text = (string)obj.GetMetaData("Title");
                  }
                  Label Content = (Label)e.Item.FindControl("Content");
                  if (Content != null)
                  {
                      Content.Text =(string)obj.Content;
                  }
                  Label PostAuthor = (Label)e.Item.FindControl("PostAuthor");
                  if (PostAuthor != null)
                  {
                      PostAuthor.Text = (string)obj.GetMetaData("Author");
                  }
               }
     
           }
       }

    Another option is using Eval directly in the Text property of your repeater items

    <asp:Repeater runat="server" ID="blogPostsRepeater">
        <ItemTemplate>
           <asp:Label runat="server"  ID="PostTitle" Text='<%# Eval("Title") %>'></asp:Label><br />
           <asp:Label runat="server"  ID="Content"  Text='<%# Eval("Content") %>'></asp:Label>
           
        </ItemTemplate>
    </asp:Repeater>

    Then the server code looks as shown below

    ContentManager manager = new ContentManager("Blogs");
    Guid blogID = new Guid("9ece05da-0a79-49b5-8666-860e05a7a524");
    blogPostsRepeater.DataSource = manager.GetContent(0, 5, "Publication_Date DESC", blogID);
    blogPostsRepeater.DataBind();


    Kind 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. Roland
    Roland avatar
    12 posts
    Registered:
    07 Apr 2009
    19 Mar 2010
    Link to this post
    Thanks Ivan.

    Is the Guid you identified just a dummy Guid?

    manager.GetContent(0, 4, "Publication_Date DESC", blogID)
    Result: Count = 0

    manager.GetContent(0, 4)
    Result: Count = 4

    I have 7 blogs and need to get the latest (approved) posts from all.

    Also, I was hoping you could point me to where I can find out what fields are returned for posts.
    I will need to have a button that links back to the Post and I am not sure if that is one of the fields returned.

    Thanks again!

    Roland
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    19 Mar 2010
    Link to this post
    Hello Roland,

    Posts are IContent objects. You can take a look at IContent Interface Members. You can also take a look at Finding Content Items to see all overloads of GetContent methods and gather more information about its parameter.

    Greetings,
    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.
  5. Roland
    Roland avatar
    12 posts
    Registered:
    07 Apr 2009
    24 Mar 2010
    Link to this post
    Thanks Ivan.

    Well I thought I had it all working but I am only getting the blog posts from the current culture.

    And in most cases this would make perfect sense.
    However, I have 7 seperate blogs that each have their own language.
    (They are not replicated into the other languages as versions.)

    I am looking to get the latest 4 published blog posts from all languages.
    So I tried to add a filter so that I get all languages:
    (Get all posts where the language ID is 0 or greater)


    filters[0] = new MetaSearchInfo(MetaValueTypes.Integer, "LangID", 0, SearchCondition.GreaterOrEqual);


    LangID is the field in the DB - is there another field that I should be looking for?


    private void GetAllPosts()
    {
        // create new instance of BlogManager
        Telerik.Blogs.BlogManager blogManager = new Telerik.Blogs.BlogManager();
        // get all blogs
        IList listOfAllBlogs = blogManager.GetBlogs();
        // create an array of blog IDs and populate it
        Guid[] parentIDArray = new Guid[7];
        parentIDArray[0] = ((Telerik.Blogs.IBlog)listOfAllBlogs[0]).ID;
        parentIDArray[1] = ((Telerik.Blogs.IBlog)listOfAllBlogs[1]).ID;
        parentIDArray[2] = ((Telerik.Blogs.IBlog)listOfAllBlogs[2]).ID;
        parentIDArray[3] = ((Telerik.Blogs.IBlog)listOfAllBlogs[3]).ID;
        parentIDArray[4] = ((Telerik.Blogs.IBlog)listOfAllBlogs[4]).ID;
        parentIDArray[5] = ((Telerik.Blogs.IBlog)listOfAllBlogs[5]).ID;
        parentIDArray[6] = ((Telerik.Blogs.IBlog)listOfAllBlogs[6]).ID;
        IMetaSearchInfo[] filters = new IMetaSearchInfo[1];
        filters[0] = new MetaSearchInfo(MetaValueTypes.Integer, "LangID", 0, SearchCondition.GreaterOrEqual);
        // get content items in ascending order for Title meta key,
        // get all items ('0,0'),
        // take only Published ones (WORKFLOW must be ON)
        // where the parent IDs are passed in an array
        IList listOfContentItems = blogManager.Content.GetContent(0, 0, "Publication_Date DESC", Telerik.Cms.Engine.ContentStatus.Published, parentIDArray, filters);
        if (listOfContentItems.Count > 0)
        {
            foreach (Telerik.Cms.Engine.IContent contentItem in listOfContentItems)
                Response.Write("Content is " + contentItem.Content + "<br />");
        
    }


    I have reviewed the documentation and i cannot find a reference to all the fields that I can use in my filters.

    Can you please answer these 2 questions:

    1. Is it possible to get latest blog posts from all blogs regadless of the current culture? If yes, how?

    2. Where is the best place to get to understand what fields can be used in the GetContent methods? Or other areas as well.

    Thanks,
    Roland
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    24 Mar 2010
    Link to this post
    Hi Roland,

    1. Is it possible to get latest blog posts from all blogs regadless of the current culture? If yes, how?


    • MetaSearchInfo class can be used only with metakeys. You could create an additional metakey that will be used to store the LangID for each content item you have created. Afterwords you will be able to filter by this metakey.
    • Another option: In your code you could set the culture explicitly and get all blogs/posts that have been created under a particular culture. Then populate all items in a list and use this list as datasource for the Repeater control.

    // set a culture, so that the manage will pick up all content items from this language
    System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en");
     

    2. Where is the best place to get to understand what fields can be used in the GetContent methods? Or other areas as well.


    The article that I sent you about Finding Content Items explains in details each overload of GetContent method. Each overload is represented by code sample.

    Kind 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.
Register for webinar
6 posts, 0 answered