Schedule content items publishing with workflow enabled

December 18, 2008 Digital Experience

 

 

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.

The Progress Team

Read next Progress DataDirect Now Connects to Denodo