A complete example

The following samples show you how to create, approve, publish, reject, archive and delete a document. It uses the sample workflow created in chapter Managing workflow schema:

 

Edit -> Article Approval (custom step) -> Publish -> Archive

 

Creating a new document

 

[C#]

 

using CMS.SettingsProvider;

using CMS.CMSHelper;

using CMS.DataEngine;

using CMS.GlobalHelper;

using CMS.TreeEngine;

using CMS.SiteProvider;

using CMS.WorkflowEngine;

 

...

 

           // Prepare the TreeProvider (it must be initialized with user information

           // when editing content)

           UserInfo ui = UserInfoProvider.GetUserInfo("administrator");

           TreeProvider tree = new TreeProvider(ui);

 

           // Get the parent document

           CMS.TreeEngine.TreeNode parentNode = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/",              TreeProvider.ALL_CULTURES, true, null, false);

           if (parentNode != null)

           {

               // Create new document

               CMS.TreeEngine.TreeNode node = new CMS.TreeEngine.TreeNode("CMS.Article", tree);

               node.DocumentName = "TestingWorkflow";

               node.DocumentCulture = CMSContext.CurrentUser.PreferredCultureCode;

 

               // Set document fields

               node.SetValue("ArticleName", "Testing workflow");

               node.SetValue("ArticleTeaserText", "Testing article for workflow");

               node.SetValue("ArticleText", "Testing article for workflow text");

              

               // Although article contains the attachment field, the attachment can be added only                     // after the document has been created (see below)

 

               // Insert the document - DocumentHelper should be used when using workflow

               DocumentHelper.InsertDocument(node, parentNode.NodeID, tree);

 

               // Insert the attachment (image) to the document

               if (FileUpload.PostedFile != null)

               {

                   DocumentHelper.AddAttachment(node, "ArticleTeaserImage", FileUpload.PostedFile,

                     tree);

 

                   // Update the document

                   DocumentHelper.UpdateDocument(node, tree);

               }

           }      

 

 

Editing a document

 

[C#]

 

using CMS.SettingsProvider;

using CMS.CMSHelper;

using CMS.DataEngine;

using CMS.GlobalHelper;

using CMS.TreeEngine;

using CMS.SiteProvider;

using CMS.WorkflowEngine;

 

...

 

           // Prepare the TreeProvider (it must be initialized with user information when editing

           // document structure)

           UserInfo ui = UserInfoProvider.GetUserInfo("administrator");

           TreeProvider tree = new TreeProvider(ui);

 

           // Get the document (current culture)

           CMS.TreeEngine.TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSiteName,

              "/TestingWorkflow", CMSContext.CurrentUser.PreferredCultureCode, false, null, false);

           if (node != null)

           {

               // Get the latest version of the document (always use DocumentHelper when using

               // workflow)

               node = DocumentHelper.GetDocument(node, tree);

 

               // Check out the document

               VersionManager vm = new VersionManager(tree);

               vm.CheckOut(node);

 

               // Update the document fields the regular way

               node.SetValue("ArticleName", "Edited testing workflow");

               node.SetValue("ArticleTeaserText", "Edited testing article for workflow");

               node.SetValue("ArticleText", "Edited testing article for workflow text");

              

               // Update the attachment if present

               if (FileUpload.PostedFile != null)

               {

                   // Add the attachment

                   DocumentHelper.AddAttachment(node, "ArticleTeaserImage", FileUpload.PostedFile,

                     tree);

               }

              

               // Update the document

               DocumentHelper.UpdateDocument(node, tree);

 

               // Check in the document

               vm.CheckIn(node, null, null);

           }      

 

 

Approving the document

 

[C#]

 

using CMS.SettingsProvider;

using CMS.CMSHelper;

using CMS.DataEngine;

using CMS.GlobalHelper;

using CMS.TreeEngine;

using CMS.SiteProvider;

using CMS.WorkflowEngine;

 

...

 

           // Prepare the TreeProvider (it must be initialized with user information when editing

           // document structure)

           UserInfo ui = UserInfoProvider.GetUserInfo("administrator");

           TreeProvider tree = new TreeProvider(ui);

 

           // Get the document (current culture)

           CMS.TreeEngine.TreeNode node =  tree.SelectSingleNode(CMSContext.CurrentSiteName,

              "/TestingWorkflow", CMSContext.CurrentUser.PreferredCultureCode, false, null, false);

           if (node != null)

           {

               // Get the latest version of the document (always get the document with DocumentHelper

               // when using workflow)

               node = DocumentHelper.GetDocument(node, tree);

 

               // Move the document to the next step (approve)

               WorkflowManager wm = new WorkflowManager(tree);

               wm.MoveToNextStep(node, "");

           }      

 

 

Rejecting the document

 

[C#]

 

using CMS.SettingsProvider;

using CMS.CMSHelper;

using CMS.DataEngine;

using CMS.GlobalHelper;

using CMS.TreeEngine;

using CMS.SiteProvider;

using CMS.WorkflowEngine;

 

...

 

           // Prepare the TreeProvider (it must be initialized with user information when editing

           // document structure)

           UserInfo ui = UserInfoProvider.GetUserInfo("administrator");

           TreeProvider tree = new TreeProvider(ui);

 

           // Get the document (current culture)

           CMS.TreeEngine.TreeNode node =  tree.SelectSingleNode(CMSContext.CurrentSiteName,

              "/TestingWorkflow", CMSContext.CurrentUser.PreferredCultureCode, false, null, false);

           if (node != null)

           {

               // Get the latest version of the document (always get the document with DocumentHelper

               // when using workflow)

               node = DocumentHelper.GetDocument(node, tree);

 

               // Move the document to the next step (approve)

               WorkflowManager wm = new WorkflowManager(tree);

               wm.MoveToPreviousStep(node, "");

           }

 

 

Publish the document

 

[C#]

 

using CMS.SettingsProvider;

using CMS.CMSHelper;

using CMS.DataEngine;

using CMS.GlobalHelper;

using CMS.TreeEngine;

using CMS.SiteProvider;

using CMS.WorkflowEngine;

 

...

 

           // Prepare the TreeProvider (it must be initialized with user information when editing

           // document structure)

           UserInfo ui = UserInfoProvider.GetUserInfo("administrator");

           TreeProvider tree = new TreeProvider(ui);

 

           // Get the document (current culture)

           CMS.TreeEngine.TreeNode node =  tree.SelectSingleNode(CMSContext.CurrentSiteName,

              "/TestingWorkflow", CMSContext.CurrentUser.PreferredCultureCode, false, null, false);

           if (node != null)

           {

               // Get the latest version of the document (always get the document with DocumentHelper

               // when using workflow)

               node = DocumentHelper.GetDocument(node, tree);

 

               // Move the document to the next step (approve)

               WorkflowManager wm = new WorkflowManager(tree);

 

               // Approve until the step is publish

               WorkflowStepInfo currentStep = wm.GetStepInfo(node);

               while ((currentStep != null) && (currentStep.StepName.ToLower() != "published"))

               {

                   currentStep = wm.MoveToNextStep(node, "");

               }

            }

 

 

Archive the document

 

[C#]

 

using CMS.SettingsProvider;

using CMS.CMSHelper;

using CMS.DataEngine;

using CMS.GlobalHelper;

using CMS.TreeEngine;

using CMS.SiteProvider;

using CMS.WorkflowEngine;

 

...

 

           // Prepare the TreeProvider (it must be initialized with user information when editing

           // document structure)

           UserInfo ui = UserInfoProvider.GetUserInfo("administrator");

           TreeProvider tree = new TreeProvider(ui);

 

           // Get the document (current culture)

           CMS.TreeEngine.TreeNode node =  tree.SelectSingleNode(CMSContext.CurrentSiteName,

              "/TestingWorkflow", CMSContext.CurrentUser.PreferredCultureCode, false, null, false);

           if (node != null)

           {

               // Get the latest version of the document (always get the document with DocumentHelper

               // when using workflow)

               node = DocumentHelper.GetDocument(node, tree);

 

               // Archive the document

               WorkflowManager wm = new WorkflowManager(tree);

               wm.ArchiveDocument(node, "");

           }

 

 

Delete (destroy) the document

 

[C#]

 

using CMS.SettingsProvider;

using CMS.CMSHelper;

using CMS.DataEngine;

using CMS.GlobalHelper;

using CMS.TreeEngine;

using CMS.SiteProvider;

using CMS.WorkflowEngine;

 

...

 

           // Prepare the TreeProvider (it must be initialized with user information when editing

           // document structure)

           UserInfo ui = UserInfoProvider.GetUserInfo("administrator");

           TreeProvider tree = new TreeProvider(ui);

          

           // Get the document (current culture)

           CMS.TreeEngine.TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSiteName,

              "/TestingWorkflow", CMSContext.CurrentUser.PreferredCultureCode, false, null, false);

 

           if (node != null)

           {

               // Always delete the document with DocumentHelper, it handles all the dependencies

               DocumentHelper.DeleteDocument(node, tree, true, true);

           }