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

How to schedule unpublish when an item is published

by Zheyna Peleva

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.

13 comments

Leave a comment
  1. Home improvement and Real estate May 07, 2013
    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.
  2. Prescott AZ May 12, 2013

    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..........
    <a
    href="http://www.youtube.com/user/PrescottAZ101/">Prescott
    AZ</a>
  3. Panzar free codes May 13, 2013
    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.
  4. funny pictures May 17, 2013
    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.........
  5. Nucig review May 17, 2013
    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
  6. check here nickname May 20, 2013
    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.
  7. putas valencia May 21, 2013
    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
  8. Burial at Sea May 21, 2013
    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.
  9. funny sms May 29, 2013
    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.
  10. crystal hearts May 30, 2013

    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
  11. sajid Jun 27, 2013
    Well when i go to the work place i cant find the desired files...can you please help that how to detect and know where they go
    Any help will really be appreciated.
    Thanks
  12. Jen Peleva Jul 01, 2013
    Hi, Sajid!
    Can you elaborate a bit on what you mean by "go to work place"? You aren't able to find the workflow files in the SDK?
  13. midsummer.co.uk Jul 24, 2013
    I must admit that this is one great insight. It surely gives a company the opportunity to get in on the ground floor and really take part in creating something special and tailored to their needs.

    Leave a comment