Kentico CMS 7.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 gets and updates the document under workflow created by the first example in the Creating documents topic.

 

private bool GetAndUpdateDocuments()

{

  // Create an instance of the Tree provider first

  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 = "CMS.MenuItem";

  string where = null;

  string orderBy = null;

  int maxRelativeLevel = -1;

  bool selectOnlyPublished = false;

 

  // Fill dataset with documents

  DataSet documents = DocumentHelper.GetDocuments(siteName, aliasPath, culture, combineWithDefaultCulture, classNames, where, orderBy, maxRelativeLevel, selectOnlyPublished, tree);

 

  if (!DataHelper.DataSourceIsEmpty(documents))

   {

      // Create a new Version manager instance

      VersionManager manager = VersionManager.GetInstance(tree);

 

      // Loop through all documents

      foreach (DataRow documentRow in documents.Tables[0].Rows)

       {

          // Create a new Tree node from the data row

          TreeNode editDocument = TreeNode.New(documentRow, "CMS.MenuItem", tree);

 

          // Check out the document

           manager.CheckOut(editDocument);

 

          string newName = editDocument.DocumentName.ToLower();

 

          // Change document data

           editDocument.DocumentName = newName;

 

          // Change coupled data

           editDocument.SetValue("MenuItemName", newName);

 

          // Save the changes

          DocumentHelper.UpdateDocument(editDocument, tree);

 

          // Check in the document

           manager.CheckIn(editDocument, null, null);

       }

 

      return true;

   }

 

  return false;

}

 

The following example creates a copy of the document under workflow created by the first example in the Creating documents topic.

 

private bool CopyDocument()

{

  // Create an instance of the Tree provider first

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Prepare parameters

  string siteName = CMSContext.CurrentSiteName;

  string aliasPath = "/API-Example/My-new-document";

  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 example folder

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

 

   aliasPath = "/API-Example/Source";

 

  // Get the new parent document

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

 

  if ((node != null) && (parentNode != null))

   {

      // Copy the document

      DocumentHelper.CopyDocument(node, parentNode.NodeID, false, tree);

 

      return true;

   }

 

  return false;

}

 

The following example moves the document under workflow created by the first example in the Creating documents topic to a different location.

 

private bool MoveDocument()

{

  // Create an instance of the Tree provider first

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Prepare parameters

  string siteName = CMSContext.CurrentSiteName;

  string aliasPath = "/API-Example/My-new-document";

  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 example folder

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

 

   aliasPath = "/API-Example/Target";

 

  // Get the new parent document

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

 

  if ((node != null) && (parentNode != null))

   {

      // Move the document

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

 

      return true;

   }

 

  return false;

}