Kentico CMS 6.0 Developer's Guide

Managing documents

Managing documents

Previous topic Next topic Mail us feedback on this topic!  

Managing documents

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example performs checkout of the sample document created by the example in the Sample documents and objects topic. Please note that check-in/check-out must be enabled for the respective workflow for this example to be functional.

 

private bool CheckOut()

{

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Prepare parameters

  string siteName = CMSContext.CurrentSiteName;

  string aliasPath = "/API-Example";

  string culture = "en-us";

  bool combineWithDefaultCulture = false;

  string classNames = TreeProvider.ALL_CLASSNAMES;

  string where = null;

  string orderBy = null;

  int maxRelativeLevel = -1;

  bool selectOnlyPublished = false;

  string columns = null;

 

  // Get the document

  TreeNode node = DocumentHelper.GetDocument(siteName, aliasPath, culture, combineWithDefaultCulture, classNames, where, orderBy, maxRelativeLevel, selectOnlyPublished, columns, tree);

 

  if (node != null)

   {

      // Create a new Workflow manager instance

      WorkflowManager workflowmanager = new WorkflowManager(tree);

 

      // Make sure the document uses workflow

      WorkflowInfo workflow = workflowmanager.GetNodeWorkflow(node);

 

      if (workflow != null)

       {

          // Check if the workflow uses check-in/check-out functionality

          if (workflow.UseCheckInCheckOut(CMSContext.CurrentSiteName))

           {

              // The document has to be checked in

              if (!node.IsCheckedOut)

               {

                  // Create a new version manager instance

                  VersionManager versionmanager = new VersionManager(tree);

 

                  // Check out the document to create a new document version

                   versionmanager.CheckOut(node);

 

                  return true;

               }

              else

               {

                   apiCheckOut.ErrorMessage = "The document has already been checked out.";

               }

           }

          else

           {

               apiCheckOut.ErrorMessage = "The workflow does not use check-in/check-out. See the \"Edit document\" example, which checks the document out and in automatically.";

           }

       }

      else

       {

           apiCheckOut.ErrorMessage = "The document doesn't use workflow.";

       }

   }

 

  return false;

}

 

The following example edits the document checked out by the previous example. If the document hasn't been checked out, it creates a new modified version of the document.

 

private bool EditDocument()

{

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Prepare parameters

  string siteName = CMSContext.CurrentSiteName;

  string aliasPath = "/API-Example";

  string culture = "en-us";

  bool combineWithDefaultCulture = false;

  string classNames = TreeProvider.ALL_CLASSNAMES;

  string where = null;

  string orderBy = null;

  int maxRelativeLevel = -1;

  bool selectOnlyPublished = false;

  string columns = null;

 

  // Get the document

  TreeNode node = DocumentHelper.GetDocument(siteName, aliasPath, culture, combineWithDefaultCulture, classNames, where, orderBy, maxRelativeLevel, selectOnlyPublished, columns, tree);

 

  if (node != null)

   {

      WorkflowManager workflowmanager = new WorkflowManager(tree);

 

      // Make sure the document uses workflow

      WorkflowInfo workflow = workflowmanager.GetNodeWorkflow(node);

 

      if (workflow != null)

       {

          // Check if the workflow uses check-in/check-out

          bool autoCheck = !workflow.UseCheckInCheckOut(CMSContext.CurrentSiteName);

 

          // Create a new version manager instance

          VersionManager versionmanager = new VersionManager(tree);

 

          // If it does not use check-in/check-out, check out the document automatically

          if (autoCheck)

           {

               versionmanager.CheckOut(node);

           }

 

          if (node.IsCheckedOut)

           {

              // Edit the last version of the document

              string newName = node.DocumentName.ToLower();

 

               node.DocumentName = newName;

               node.SetValue("MenuItemName", newName);

 

              // Save the document version

              DocumentHelper.UpdateDocument(node, tree);

 

              // Automatically check in

              if (autoCheck)

               {

                   versionmanager.CheckIn(node, null, null);

               }

 

              return true;

           }

          else

           {

               apiEditDocument.ErrorMessage = "The document hasn't been checked out.";

           }

       }

      else

       {

           apiEditDocument.ErrorMessage = "The document doesn't use workflow.";

       }

   }

 

  return false;

}

 

The following example reverts changes made to the document after the last check-out and checks it back in.

 

private bool UndoCheckout()

{

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Prepare parameters

  string siteName = CMSContext.CurrentSiteName;

  string aliasPath = "/API-Example";

  string culture = "en-us";

  bool combineWithDefaultCulture = false;

  string classNames = TreeProvider.ALL_CLASSNAMES;

  string where = null;

  string orderBy = null;

  int maxRelativeLevel = -1;

  bool selectOnlyPublished = false;

  string columns = null;

 

  // Get the document

  TreeNode node = DocumentHelper.GetDocument(siteName, aliasPath, culture, combineWithDefaultCulture, classNames, where, orderBy, maxRelativeLevel, selectOnlyPublished, columns, tree);

 

  if (node != null)

   {

      WorkflowManager workflowmanager = new WorkflowManager(tree);

 

      // Make sure the document uses workflow

      WorkflowInfo workflow = workflowmanager.GetNodeWorkflow(node);

 

      if (workflow != null)

       {

          if (node.IsCheckedOut)

           {

              VersionManager versionmanager = new VersionManager(tree);

 

              // Undo the checkout

               versionmanager.UndoCheckOut(node);

 

              return true;

           }

          else

           {

               apiUndoCheckout.ErrorMessage = "The document hasn't been checked out.";

           }

       }

      else

       {

           apiUndoCheckout.ErrorMessage = "The document doesn't use workflow.";

       }

   }

 

  return false;

}