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

Forums / Developing with Sitefinity / Publishing Content Programmatically

Publishing Content Programmatically

8 posts, 0 answered
  1. Alex
    Alex avatar
    39 posts
    Registered:
    19 Jul 2010
    05 Jan 2012
    Link to this post
    I am trying to create and publish content items programmatically and have based most of the code on your blog post at http://www.sitefinity.com/blogs/slavoingilizov/posts/11-09-28/how_to_publish_content_items_in_the_past.aspx.  However, when it runs the code it does not "Publish" the item.  It shows as a "draft" instead of "published in the admin for News items.  Any ideas?

    Here is a snippet of my code:  (Publication date will always have a date in this scenario and I have verified that a date is being passed in)

    NewsManager nManager = NewsManager.GetManager();
    var newsItem = nManager.CreateNewsItem();
     
    newsItem.Title = this.txtTitle.Text;
    newsItem.Content = this.RadEditorStoryHTML.Content;
    newsItem.Summary = this.txtSummary.Text;
    newsItem.AllowComments = false;
    newsItem.Visible = true;
         
    string cloudFileLocation = "ims_media/";
     
    if (txtPublishDate.SelectedDate.HasValue)
    {
        newsItem.PublicationDate = txtPublishDate.SelectedDate.Value;
    }
    if (txtUnPublishDate.SelectedDate.HasValue)
    {
        newsItem.ExpirationDate = txtUnPublishDate.SelectedDate.Value;
    }
     
    //Set Categories
    var catlist = newsItem.GetValue<TrackedList<Guid>>("Category");
    string selectedCategory = Request.Form["C001$radioCategories"];
    if (!selectedCategory.IsNullOrEmpty())
    {
        ListItem item = radioCategories.Items.FindByValue(selectedCategory);
     
        catlist.Add(Guid.Parse(selectedCategory));
        cloudFileLocation += item.Text.ToLower().Replace(" ", "_") + "/";
     
        newsItem.SetValue("Category", catlist);
    }
     
    nManager.RecompileItemUrls(newsItem);
    nManager.Lifecycle.PublishWithSpecificDate(newsItem, newsItem.PublicationDate);
    nManager.SaveChanges();

  2. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    02 Dec 2016
    10 Jan 2012
    Link to this post
    Hello Alex,

    We have replied to you in the support thread you had open for the same issue, you can check our response there. For your convenience, please find below a copy of our reply:

    "Actually if you want to Schedule the item for publishing/unpublishing you might need to alter the code a little bit to involve WorkflowManager and use the MessageWorkflow() method with parameter "Schedule". Please find below a slightly modified sample demonstrating how you can achieve this (I have omitted several parts of the code for the sake of testing, but they should not interfere with the functionality so you can add them back). The important changes have been commented for your convenience:
    public const string UrlNameCharsToReplace = @"[^\w\-\!\$\'\(\)\=\@\d_]+";
           public const string UrlNameReplaceString = "-";
      
           protected void Page_Load(object sender, EventArgs e)
           {
                  
               NewsManager nManager = NewsManager.GetManager();
               var newsItem = nManager.CreateNewsItem();
      
               newsItem.Title = "News Article 4";
               newsItem.Content = "Some test content, created though code";
               newsItem.Summary = "Summary";
               newsItem.AllowComments = false;
               newsItem.Visible = true;
               //You need to set the UrlName and replace any special characters,  the manager will take care of recompiling the item URL
               newsItem.UrlName = Regex.Replace(newsItem.Title.ToLower(), UrlNameCharsToReplace, UrlNameReplaceString);
               newsItem.PublicationDate = DateTime.UtcNow;
               newsItem.ExpirationDate = DateTime.UtcNow.AddDays(+2);
               //The item needs to be checked out so we can pass it to WorkflowManager for scheduling
               nManager.CheckOut(newsItem);
               nManager.SaveChanges();
      
               var bag = new Dictionary<string, string>();
               bag.Add("ContentType", typeof(NewsItem).FullName);
               WorkflowManager.MessageWorkflow(newsItem.Id, typeof(NewsItem), null, "Schedule", true, bag);
      
           }
    "

    Please do not hesitate to let us know if you need some additional information, we'll be glad to help


    Kind regards,
    Boyan Barnev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Ahmed Salman
    Ahmed Salman avatar
    31 posts
    Registered:
    13 Apr 2010
    27 Jun 2012
    Link to this post
    Hi ,

    i have error when i try to set the News item Catefories.

    Error :
    Telerik.sitefinity.News.Model.NewsItem does not contaion a definition for 'GetValue' and no extension method ....etc please check the attached file/ 

     

     

    //Set Categories

     

     

     

    var catlist = newsItem.GetValue<TrackedList<Guid>>("Category");

     

  4. Nicolas
    Nicolas avatar
    156 posts
    Registered:
    19 Jan 2011
    27 Jun 2012
    Link to this post
    Hi,

    It's a using problem.
    Add this one : using Telerik.Sitefinity.Model;

    Regards,
    Nicolas
  5. Ahmed Salman
    Ahmed Salman avatar
    31 posts
    Registered:
    13 Apr 2010
    28 Jun 2012
    Link to this post
    thank you nicolas but it does not work  :(
  6. Nicolas
    Nicolas avatar
    156 posts
    Registered:
    19 Jan 2011
    28 Jun 2012
    Link to this post
    Hi,
    With this using you probably doesn't have the same error. On previous one, GetValue() wasn't recognized, it needs a specific using.
    It doesn't build or doesn't work when running ?

    Regards,
    Nicolas
  7. Ahmed Salman
    Ahmed Salman avatar
    31 posts
    Registered:
    13 Apr 2010
    28 Jun 2012
    Link to this post
    You are correct it is working fine now thank you :)
  8. Dhivya
    Dhivya avatar
    1 posts
    Registered:
    15 Nov 2012
    12 Mar 2014 in reply to Boyan Barnev
    Link to this post
    How to unpublish the blog post item directly?
8 posts, 0 answered