+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

Schedule content items publishing with workflow enabled

by Ivan Dimitrov

 

 

One more blogger is on board ready to post more and more.

 

In this post I am going to show you how to publish and schedule content items with workflow enabled.

First we need to create a user control. I am going to work with News items. Let's say my control is named ScheduleNews.ascx.

 

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ScheduleNews.ascx.cs" Inherits="CustomControls_ScheduleNews" %> 
 
 
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> 
 

 

Then, register this control in our web.config file.

 

<toolboxControls> 
 <clear /> 
 <add name="ScheduleNews" 
 section="Custom" 
 url="~/CustomControls/ScheduleNews.ascx" /> 
...
</toolboxControls>

 

Now we are going to add some logic in our control code behind. The idea is that, we have to read Publication_Date and Expiration_Date metafields, check the dates, and set new time to these metafields programmatically. Then, we need to iterate through all workflow states till we reach the last one - "Publish" state.

 

ScheduleNews.ascx.cs

 

using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Telerik.Cms.Engine; 
using System.Collections; 
using Telerik.Workflow; 
 
public partial class CustomControls_ScheduleNews : System.Web.UI.UserControl 
{ 
 
 protected void Page_Load(object sender, EventArgs e) 
    { 
    } 
 protected void Button1_Click(object sender, EventArgs e) 
    { 
        ContentManager manager = new ContentManager("News"); 
        IList Items = manager.GetContent(); 
 foreach (IContent item in Items) 
        { 
 //READ METAFIELDS  
 // OPTIOANLLY WORKS WITH CATEGORIES  
 //string Category = item.GetMetaData("Category").ToString();  
            DateTime pubDate = (DateTime)item.GetMetaData("Publication_Date"); 
            DateTime expDate = (DateTime)item.GetMetaData("Expiration_Date"); 
 //OPTIONALLY CHECK IF OUR ITEMS ARE IN "MyCategory"  
 //if (Category.ToString().Equals("MyCategory"))  
 //{  
 //get the content for editing  
            StagedContent itemToChange = manager.GetStagedContent(item.ID) as StagedContent; 
 //OPTIONALLY CHECK EVEN OR ODD DATES(2,4,6...1,3,5)  
 bool changed = false; 
 if (pubDate.Day % 2 == 0) 
            { 
 //DAY IS EVEN HERE  
                DateTime newTime = DateTime.Today; 
 //SET NEW PUBLICATION DATE  
                itemToChange.SetMetaData("Publication_Date", newTime.AddDays(5)); 
 //SET NEW EXPIRATION DATE  
 //THE ITEM EXPIRE IN 30 DAYS FROM NOW ON  
                itemToChange.SetMetaData("Expiration_Date", newTime.AddDays(30)); 
                changed = true; 
            } 
 else 
            { 
 //DAY IS ODD  
                changed = true; 
            } 
 //SAVE CONTENT  
 if (changed) 
 //SAVE IN DRAFT  
                itemToChange.Status = ContentStatus.Draft; 
            manager.SaveContent(itemToChange); 
 //ITERATE THROUGH ALL WORKFLOW STATES TILL WE REACH PUBLICATION DATE  
            StagedContent staged = manager.GetCurrentState(itemToChange.ID); 
            ExecuteActivity(staged.WorkflowInstanceId, "SendForApproval"); 
            Nolics.ORMapper.Base.DataConnection.InitWebRequest(); 
 
 //PASS THROUH APPROVE STATUS  
            ExecuteActivity(staged.WorkflowInstanceId, "Approve"); 
            Nolics.ORMapper.Base.DataConnection.InitWebRequest(); 
 
 //PASS THROUGH PUBLUISH STATUS  
            ExecuteActivity(staged.WorkflowInstanceId, "Publish"); 
 
        } 
    }
 
 private EventActivity FindActivity(string name, IList<Activity> activities) 
    { 
 foreach (Activity act in activities) 
        { 
 if ((act is EventActivity) && (((EventActivity)act).CommandName == name)) 
 return (EventActivity)act; 
            EventActivity ea = FindActivity(name, act.Activities); 
 if (ea != null) 
 return ea; 
        } 
 return null; 
    } 
 
 private void ExecuteActivity(Guid id, string activityName) 
    { 
        WorkflowInstance instance = WorkflowRuntime.Instance.GetWorkflow(id); 
 if (instance != null) 
        { 
            ContentWorkflow workflow = (ContentWorkflow)instance.Activity; 
            EventActivity activity = FindActivity(activityName, workflow.Activities); 
 if (activity != null) 
            { 
                activity.Execute(WorkflowRuntime.Instance); 
            } 
        } 
    } 
}
 
 
 
 

 

Conclusion: When you run the code news items Publicaton_Date and Expiration_Date will be changed and the content will be published even with workflow enabled only with one click on the button.

6 comments

Leave a comment
  1. Romi Dec 20, 2008
    Hi Ivan,

    Could imagine more telerik fan using RadScheduler and grouping features to move the contents along one Time line during the month. The object is time management for contents.

                t1    t2    t3    t4    t5    t6    t7
    item1           XXXX
    item2        XXX                XXXX
    itemN                XXXX

    Thanks for this article.
  2. real estate careers long island Feb 04, 2013
    I actually have no idea about this thing here yet but I guess I have the right to do it on the first place. It's really awesome and one of the best.
  3. Halenaa Apr 24, 2013
    I actually love this idea here on the first place.. Thanks a lot for all the help... I love to at least make it clear then.  <a rel="dofollow" href="http://ogdenrestaurant.com/what-can-i-eat-on-the-paleo-diet">what foods are in the paleo diet</a>
  4. Halenaa Apr 24, 2013
    I actually love this idea here on the first place.. Thanks a lot for all the help... I love to at least make it clear then.  what foods are in the paleo diet
  5. when do kids lose their teeth May 08, 2013
    That's just what I am currently looking for. so the schedule here will be so fine and alright. I got to learn it then.
  6. Dentist Saratoga Springs Jun 13, 2013
    now of course that's probably what we are currently looking after. that's how it is then for all of us.

    Leave a comment