More in this section
Categories
Bloggers
Blogs RSS feed

How to Publish Content Items in the Past

by Slavo Ingilizov

As usual, we’ve packed a lot of fixes and improvements in the just released 4.2 SP1 of Sitefinity. If you still haven’t checked it out, go download it from your account. With this blog post, I want to highlight a feature we’ve implemented that was requested a lot. From now on, you can publish any content in Sitefinity with a date in the past.

Lots of people asked us to implement this, and we listened. A common scenario we’ve heard about is importing content from an existing news site into Sitefinity, and keeping the dates of news articles. This is now possible.

When creating an item, instead of clicking the Publish button directly, you can choose another option from the “More Actions” menu:

moreActions

This would bring up a dialog, where you can select any date for publishing the news item, including ones in the past.

publishDialog

Publishing items in the past using the API

Although this is handy, I admit there will be a lot of scenarios, where this work will not be done through the user interface. Many people have written custom code to import content using the Sitefinity API. We’ve also made it very easy to specify a past date for items created through code.

var pubDate = new DateTime(1978, 4, 15);
var manager = NewsManager.GetManager();
var item = manager.CreateNewsItem();
item.Title = "News item in the past";
item.UrlName = "past-news";
item.Content = "This item is created through the native API";
manager.RecompileItemUrls(item);
manager.Lifecycle.PublishWithSpecificDate(item, pubDate);
manager.SaveChanges();

And also, here’s how you can do the same using the fluent API:

App.WorkWith().AnyContentItem<NewsItem>().CreateNew().Do(
    n =>
    {
        n.Title = "Sample item in past";
        n.UrlName = "sample-item-in-past";
    })
    .Publish(new DateTime(1978, 4, 15))
    .SaveChanges();

Go ahead and try this new feature. We are open to other suggestions to include in the next releases. The more you vote on items in PITS, the faster you get them.

26 comments

Leave a comment
  1. Jack May 14, 2013
    This doesn't work.  If I set the publication date in the past, the item  never gets published.
  2. Jack May 14, 2013
    This doesn't work.  If I set the publication date in the past, the item  never gets published.
  3. Syed Ahsan May 26, 2014
    Also not working for me, I am using Sitefinity 5.2 :)
  4. Syed Ahsan May 26, 2014
    In my case it is working for default language(en) but if any news exist in Arabic but not in English then this problem occurs.
  5. Taha Jul 15, 2014
    This does not work and remains "scheduled for publication" and does not gets published.
  6. Neil Nov 17, 2014
    The Publish method does not have an overload that includes a DatTime param...
  7. Uttam Kumar May 08, 2015

    Hi,

    After doing lot of research for hours, following code did work for me and I'm on Sitefinity version 8.0.

    private void CreateBlogPost(Guid blogPostId, Guid parentBlogId, Lstring blogPostTitle, Lstring blogPostContent, string postDate, string authorName, Listcategories)        {            var masterBlogPost = BlogsManager.GetBlogPosts().Where(item => item.Id == blogPostId).FirstOrDefault();            if (masterBlogPost == null)            {                //The Blogs item is created as a master. The masterBlogPostId is assigned to the master.                masterBlogPost = BlogsManager.CreateBlogPost(blogPostId);                //The item needs to be checked out so we can pass it to WorkflowManager for scheduling                var tempBlogPost = BlogsManager.CheckOut(masterBlogPost) as BlogPost;                //Set the parent blog.                Blog blog = BlogsManager.GetBlogs().Where(b => b.Id == parentBlogId).SingleOrDefault();                tempBlogPost.Parent = blog;                //Set the properties of the blog post.                                tempBlogPost.Title = blogPostTitle;                tempBlogPost.Content = blogPostContent;                //custom field                tempBlogPost.SetValue("AuthorName", authorName);                tempBlogPost.DateCreated = DateTime.Parse(postDate);                tempBlogPost.PublicationDate = DateTime.Parse(postDate);                tempBlogPost.ExpirationDate = null;                //tempBlogPost.LastModified = DateTime.Parse(postDate);                tempBlogPost.UrlName = Regex.Replace(blogPostTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");                //assign blog post to taxons                if (categories != null && categories.Any())                {                    foreach (var category in categories)                    {                        if (!string.IsNullOrEmpty(category))                        {                            var categoryId = GetCategoryId(category);                            tempBlogPost.Organizer.AddTaxa("Category", categoryId);                        }                    }                }                //Recompiles and validates the url of the blog.                BlogsManager.RecompileAndValidateUrls(tempBlogPost);                //check-in to update master copy                masterBlogPost = BlogsManager.Lifecycle.CheckIn(tempBlogPost) as BlogPost;                DateTime postPublishDate = DateTime.MinValue;                if (DateTime.TryParse(postDate, out postPublishDate))                {                    BlogsManager.Lifecycle.PublishWithSpecificDate(masterBlogPost, postPublishDate);                }                else                {                    //Publish the Blogs item. The published version acquires new ID.                    var bag = new Dictionary();                    bag.Add("ContentType", typeof(BlogPost).FullName);                    WorkflowManager.MessageWorkflow(blogPostId, typeof(BlogPost), null, "Publish", false, bag);                }                //Save the changes.                BlogsManager.SaveChanges();            }        }

  8. spizz Nov 27, 2015
    This doesn't work! Version 8.2 and publishing content items in the past using this method does not work.  
  9. Richard Dec 06, 2015

    I really need this to work? Anyone have ideas please?

    Uttam Kumar - if you are receiving notifications for this, please cna you get in touch? Many thanks!

  10. Andrew Apr 02, 2016
    This is absolutely ridiculous that it is this difficult to accomplish. Do you people not take into account migration scenarios?
  11. Gold Storage Dubai Apr 02, 2018
    This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post.Gold Storage Dubai
  12. Military flight travel Apr 02, 2018
    I recently found many useful information in your website especially this blog page. Among the lots of comments on your articles. Thanks for sharing.Military flight travel
  13. HYDRAULIC POWER UNITS Apr 02, 2018
    Excellent information on your blog, thank you for taking the time to share with us. Amazing insight you have on this, it's nice to find a website that details so much information about different artists.HYDRAULIC POWER UNITS
  14. multiple sclerosis Apr 02, 2018
    Thank you for the update, very nice site..multiple sclerosis
  15. Elliatt Apr 02, 2018
    thanks for the tips and information..i really appreciate it..Elliatt
  16. painting cabinets Apr 02, 2018
    Thanks for the nice blog. It was very useful for me. I'm happy I found this blog. Thank you for sharing with us,I too always learn something new from your post.painting cabinets
  17. sbobet Apr 02, 2018
    You’ve got some interesting points in this article. I would have never considered any of these if I didn’t come across this. Thanks!.sbobet
  18. generator maintenance Apr 02, 2018
    Great knowledge, do anyone mind merely reference back to itgenerator maintenance
  19. detectives en el barrio de salamanca Apr 02, 2018
    I found that site very usefull and this survey is very cirious, I ' ve never seen a blog that demand a survey for this actions, very curious...detectives en el barrio de salamanca
  20. from sacramento to sfo Apr 09, 2018
    I have recently started a blog, the info you provide on this site has helped me greatly. Thanks for all of your time & work.from sacramento to sfo
  21. DETECTIVES EN MADRID Apr 11, 2018
    I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit.DETECTIVES EN MADRID
  22. DETECTIVES MADRID Apr 16, 2018
    I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people.DETECTIVES MADRID
  23. ΑΠΟΦΡΑΞΕΙΣ ΤΙΜΕΣ Apr 18, 2018
    Τα τελευταία χρόνια επεκτείναμε σταδιακά τις δραστηριότητες μας εκτός τις Αποφράξεις αποχετευτικών αγωγών και εκκενώσεις βόθρων, τώρα αναλαμβάνουμε υπεύθυνα απολυμάνσεις κτηρίων και έκδοση κοινοχρήστων, θέλοντας να προσφέρουμε ολοκληρωμένες υπηρεσίες στις κτηριακές ανάγκες.αποφραξεις
  24. Spuitkurk Apr 21, 2018
    Please continue this great work and I look forward to more of your awesome blog posts.Spuitkurk
  25. avengers Apr 23, 2018
    Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article.avengers
  26. Zon Apr 23, 2018
    I was really enthousiastic when this blog was presented. Of great interest to me to read. Zonnepanelen | Warmtepomp kopen | Airco
    | Airconditioning

    Leave a comment