Kentico CMS 6.0 Developer's Guide

Deleting documents

Deleting documents

Previous topic Next topic Mail us feedback on this topic!  

Deleting 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.



 

 

Please note

 

For these examples to produce meaningful results, it is expected to run the API examples on the Creating documents page first.

 

 

The following example deletes a linked document.

 

private bool DeleteLinkedDocuments()

{

  // Create an instance of the Tree provider first

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  string siteName = CMSContext.CurrentSiteName;

 

  // Get the document

  TreeNode node = tree.SelectSingleNode(siteName, "/API-Example/My-new-document", "en-us");

 

  if (node != null)

   {

      // Prepare the where condition

      string where = "NodeLinkedNodeID = " + node.NodeID;

 

      // Get linked documents' IDs

      DataSet documents = tree.SelectNodes(siteName, "/API-Example/%", "en-us", false, null, where, null, -1, false, -1, "NodeID");

 

      if (!DataHelper.DataSourceIsEmpty(documents))

       {

          // Loop through the documents and delete them. Alternatively, the DeleteLinks method from the TreeProvider can be used to delete all document's links.

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

           {

              // Get additional document data

              TreeNode document = tree.SelectSingleNode(ValidationHelper.GetInteger(documentRow["NodeID"], 0));

 

              // Delete the document

               document.Delete();

           }

 

          return true;

       }

      else

       {

           apiDeleteLinkedDocuments.ErrorMessage = "No linked documents were found.";

       }

   }

 

  return false;

}

 

The following example deletes a document's culture version.

 

private bool DeleteCultureVersion()

{

  // Create an instance of the Tree provider first

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Get the german culture version of the document

  TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/API-Example/My-new-document", "de-de");

 

  if (node != null)

   {

      // Delete the document

       node.Delete();

 

      return true;

   }

 

  return false;

}

 

The following example deletes a document.

 

private bool DeleteDocument()

{

  // Create an instance of the Tree provider first

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Get the document

  TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/API-Example/My-new-document", "en-us");

 

  if (node != null)

   {

      // Delete the document and all its culture versions

       node.DeleteAllCultures();

 

      return true;

   }

 

  return false;

}

 

The following example deletes the example document structure.

 

private bool DeleteDocumentStructure()

{

  // Create an instance of the Tree provider first

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Get the API Example folder

  TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/API-Example", "en-us");

 

  if (node != null)

   {

      // Delete the folder and all child documents

       node.DeleteAllCultures();

   }

 

  CultureInfo culture = CultureInfoProvider.GetCultureInfo("de-de");

 

  // Remove the example culture from the site

  CultureSiteInfoProvider.RemoveCultureFromSite(culture.CultureID, CMSContext.CurrentSiteID);

 

  return true;

}