How to schedule unpublish when an item is published

How to schedule unpublish when an item is published

Posted on January 09, 2013 0 Comments

The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.

If you still haven't got a chance to take a look at the part of our documentation about modifying existing Sitefinity workflow, may be this is the right moment. My colleague Radoslav Georgiev  already showed you how to  customize the Workflow notification messages. This blog post is about changing the AnyContentWorkflow.xaml in order to schedule an item for unpublishing right after you click on the Publish button. For this purpose you need to have Professional or Enterprise Sitefinity edition and the default workflow files. You can find the workflow files used by Sitefinity in the SDK (C:\Program Files (x86)\Telerik\Sitefinity 5.2\SDK\Content\Resources). Once you have the files, add them to a folder in your Sitefinity project - for example I'm adding them to my CustomWorkflows folder.Then navigate to Administration -> Settings -> Advanced -> Workflows and replace the the workflow you would like to customize as in the image.

 Simply enter the relative path to your custom workflow file. Now you're ready to modify the AnyContentApproval.xaml file. Open it and click on Default workflow (we're going to modify the default workflow Sitefinity uses). After you do that you will see the Workflow diagram, where you will notice a Switch with all available actions that can be performed with an item. Follow the pointer to the Publish activity. The default Publishing operation ends with an ExecuteCodeActivity, where the draft version is checked in and Published. We're going to extend this behavior by adding a custom ExecuteCodeActivity class to the process.To do that add a class to your project, which inherits from Execute code activity and override the Execute protected method. In it do the following:

protected override void Execute(System.Activities.CodeActivityContext context)
        {
            var dataContext = context.DataContext;
            var masterFluent = dataContext.GetProperties()["masterFluent"].GetValue(dataContext) as
 
AnyDraftFacade;
            var masterFull = masterFluent.Get();
            if (masterFluent.IsPublished())
            {
  
                var temp = masterFluent.CheckOut();
                var newsItemTempVersion = temp.Get();
                newsItemTempVersion.ExpirationDate = System.DateTime.Now.AddMinutes(2);
  
                temp.CheckIn().SaveChanges();
            }
        }
        }

In the above code we get the datacontext and from it - the draft item. Then we checkout to the temp version of the item and set an expiration date to it. The item is again checked in. This is all we need in our custom Execute activity. Now, to add it to your custom workflow simply go back to the AnyContentApprovalWorkflow.xamlx file and open the Visual Studio Toolbox. On top you will see your custom activity.


 Drag and drop it on the workflow page and add a control flow, from the Publish activity to the custom one. This is all it takes to get your code working. There's one last thing we need to do to Unpublish the item. We add a ScheduleWorkflowCallActivity with Operation name Unpublish and we specify when this ScheduleWorkflowCallActivity should be executed. In our case, we want this to happen on the ExpirationDate of the item (which we set in the custom ExecuteCode activity), so we add the following code:

fluent.Done().Get().ExpirationDate



Now simply build the project and test the implementation. If you have followed all steps, each time you you publish a news item, you will also schedule it for unpublishing after particular time period. Now that you know how to customize Sitefinity workflows, the possibilies are countless. This is only one of the use-cases, but you can achieve many others. Hope you find this blog helpful.

progress-logo

The Progress Team

View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.

Comments

Comments are disabled in preview mode.
Topics

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation