Kentico CMS 7.0 Developer's Guide

Managing attachments

Managing attachments

Previous topic Next topic Mail us feedback on this topic!  

Managing attachments

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 adds an unsorted attachment to the document created by the example in the previous topic.

 

private bool InsertUnsortedAttachment()

{

  // Create a new instance of the Tree provider

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Get the document

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

 

  // Path to the file to be inserted. This example uses an explicitly defined file path. However, you can use an object of the HttpPostedFile type (uploaded via an upload control).

  string postedFile = Server.MapPath("Files/file.png");

 

  if (node != null)

   {

      // Insert the attachment

      return (DocumentHelper.AddUnsortedAttachment(node, Guid.NewGuid(), postedFile, tree, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE) != null);

   }

 

  return false;

}

 

The following example inserts a field attachment to the document created by the example in the previous topic.

 

private bool InsertFieldAttachment()

{

  // Create a new instance of the Tree provider

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Get the example document

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

 

  if (node != null)

   {

      AttachmentInfo newAttachment = null;

 

      // Path to the file to be inserted. This example uses an explicitly defined file path. However, you can use an object of the HttpPostedFile type (uploaded via an upload control).

      string postedFile = Server.MapPath("Files/file.png");

 

      // Insert the attachment and update the document with its GUID

       newAttachment = DocumentHelper.AddAttachment(node, "MenuItemTeaserImage", postedFile, tree);

       node.Update();

 

      if (newAttachment != null)

       {

          return true;

       }

 

       apiInsertFieldAttachment.ErrorMessage = "Couldn't insert the attachment.";

   }

 

  return false;

}

 

The following example moves the unsorted attachment created by the first example on this page down in the order of the document's unsorted attachments.

 

private bool MoveAttachmentDown()

{

  // Create a new instance of the Tree provider

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Get the example document

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

 

  if (node != null)

   {

      string where = "AttachmentIsUnsorted = 1";

      string orderBy = "AttachmentLastModified DESC";

 

      // Get the document's unsorted attachments with the latest on top

      DataSet attachments = DocumentHelper.GetAttachments(node, where, orderBy, false, tree);

 

      if (!DataHelper.DataSourceIsEmpty(attachments))

       {

          // Create attachment info object from the first DataRow

          AttachmentInfo attachment = new AttachmentInfo(attachments.Tables[0].Rows[0]);

 

          // Move the attachment

          DocumentHelper.MoveAttachmentDown(attachment.AttachmentGUID, node);

 

          return true;

       }

      else

       {

           apiMoveAttachmentDown.ErrorMessage = "No attachments were found.";

       }

   }

 

  return false;

}

 

The following example moves the unsorted attachment created by the first example on this page up in the order of the document's unsorted attachments.

 

private bool MoveAttachmentUp()

{

  // Create a new instance of the Tree provider

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Get the example document

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

 

  if (node != null)

   {

      string where = "AttachmentIsUnsorted = 1";

      string orderBy = "AttachmentLastModified DESC";

 

      // Get the document's unsorted attachments with the latest on top

      DataSet attachments = DocumentHelper.GetAttachments(node, where, orderBy, false, tree);

 

      if (!DataHelper.DataSourceIsEmpty(attachments))

       {

          // Create attachment info object from the first DataRow

          AttachmentInfo attachment = new AttachmentInfo(attachments.Tables[0].Rows[0]);

 

          // Move the attachment

          DocumentHelper.MoveAttachmentUp(attachment.AttachmentGUID, node);

 

          return true;

       }

      else

       {

           apiMoveAttachmentDown.ErrorMessage = "No attachments were found.";

       }

   }

 

  return false;

}

 

The following example edits metadata of the unsorted attachment created by the first example on this page. It modifies its name, title and description.

 

private bool EditMetadata()

{

  // Create a new instance of the Tree provider

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Get the example document

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

 

  if (node != null)

   {

      string where = "AttachmentIsUnsorted = 1";

      string orderBy = "AttachmentLastModified DESC";

 

      // Get the document's unsorted attachments with the latest on top

      DataSet attachments = DocumentHelper.GetAttachments(node, where, orderBy, false, tree);

 

      if (!DataHelper.DataSourceIsEmpty(attachments))

       {

          // Create attachment info object from the first DataRow

          AttachmentInfo attachment = new AttachmentInfo(attachments.Tables[0].Rows[0]);

 

          // Edit its metadata

           attachment.AttachmentName += " - modified";

           attachment.AttachmentTitle += "Example title";

           attachment.AttachmentDescription += "This is an example of an unsorted attachment.";

 

          // Ensure that the attachment can be updated without supplying its binary data.

           attachment.AllowPartialUpdate = true;

 

          // Save the object into database

          AttachmentInfoProvider.SetAttachmentInfo(attachment);

 

          return true;

       }

      else

       {

           apiEditMetadata.ErrorMessage = "No attachments were found.";

       }

   }

 

  return false;

}

 

The following example deletes the two document attachments created by the first and second example on this page.

 

private bool DeleteAttachments()

{

  // Create a new instance of the Tree provider

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Get the example document

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

 

  if (node != null)

   {

      // Get the document's unsorted attachments with the latest on top

      DataSet attachments = DocumentHelper.GetAttachments(node, null, null, false, tree);

 

      if (!DataHelper.DataSourceIsEmpty(attachments))

       {

          foreach (DataRow attachmentRow in attachments.Tables[0].Rows)

           {

              // Create attachment info object from the first DataRow

              AttachmentInfo attachment = new AttachmentInfo(attachmentRow);

 

              // Delete the attachment

              DocumentHelper.DeleteAttachment(node, attachment.AttachmentGUID, tree);

           }

 

          return true;

       }

      else

       {

           apiDeleteAttachments.ErrorMessage = "No attachments found.";

       }

   }

 

  return false;

}