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

Forums / Sitefinity SDK / Using the Fluent API to import blog posts from an RSS feed

Using the Fluent API to import blog posts from an RSS feed

2 posts, 0 answered
  1. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    27 Aug 2010
    Link to this post
    Here is what I'm trying to do:

    1.  Fetch the RSS feed from Sitefinity Watch
    2.  Loop through each RSS item
    3.  Use Sitefinity's Fluent API to add the blog post to my test 4.0 web site.

    Here is the code:

    using System;
    using System.Linq;
    using Telerik.Sitefinity;
    using System.Xml.Linq;
     
    namespace SitefinityWebApp
    {
        public partial class test : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                // Get the RSS feed from Sitefinity Watch
                var blogRSS = XDocument.Load("http://feeds.feedburner.com/SitefinityWatch");
     
                // Get the RSS items from the feed
                var rssItems = from d in blogRSS.Descendants("item")
                            select d;
     
                // Get the ID associated with my Sitefinity Watch blog
                var blogId = (from b in App.WorkWith().Blogs().Get()
                            where b.Title == "Sitefinity Watch"
                            select b).First().Id;
     
                // Print the ID, to confirm I got it (this works!)
                Response.Write(blogId.ToString() + "<br />");
     
                // Loop through each RSS item
                foreach (var item in rssItems.ToList())
                {
                    // Print the Title for each blog post (this works!)
                    Response.Write(item.Element("title").Value + "<br />");
                     
                    // Add the blog post from the RSS feed to my Sitefinity 4.0 blog
                    App.WorkWith()
                        .Blog(blogId)
                        .BlogPost()
                        .CreateNew()
                        .Do(b =>
                        {
                            b.Title = item.Element("title").Value;
                            b.Content = item.Element("description").Value;
                        })
                        .SaveChanges();
     
                    // No error is thrown, but this does't work
                }
            }
        }
    }

    Does anyone see what I might be doing wrong?

    Gabe
    ===============
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    27 Aug 2010
    Link to this post
    Hello Gabe Sumner,

    You have to modify your code as shown below

    foreach (var item in rssItems.ToList())
             {
                 // Print the Title for each blog post (this works!)
                 Response.Write(item.Element("title").Value + "<br />");
     
     
     
                 using (var fluent = App.WorkWith())
                 {
                     var blog = fluent.Blog(blogId)
                                      .Get();
     
                     blog.BlogPosts.Add(fluent.BlogPost()
                                              .CreateNew()
                                              .Do(b =>
                                              {
                                                  b.Title = item.Element("title").Value;
                                                  b.Content = item.Element("description").Value;
                                                  b.PublicationDate = DateTime.Now;
                                                  b.ExpirationDate = DateTime.Now.AddDays(10);
                                              })
                                              .Get());
                 }
             }

    The posts you have created should be in the database. The problem comes from that the parent of the post is not appended properly, but this is an issue with the fluent API.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
2 posts, 0 answered