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

Posted by Community Admin on 05-Aug-2018 00:14

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

All Replies

Posted by Community Admin on 27-Aug-2010 00:00

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
===============

Posted by Community Admin on 27-Aug-2010 00:00

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.

This thread is closed