Is this really the version you are looking for? Kentico 9 documentation is no longer updated. Use the switch in the header to choose your Kentico version.

Attachments

List of examples:

Getting the attachments of a page

// Creates a new instance of the Tree provider
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets a page
TreeNode page = tree.SelectNodes()
	.Path("/Articles")
	.OnCurrentSite()
	.Culture("en-us")
	.FirstObject;

if (page != null)
{
	// Iterates over all attachments of the page
	foreach (BaseInfo attachment in page.AllAttachments)
	{
		AttachmentInfo publishedAttachment = attachment as AttachmentInfo;
		if (publishedAttachment != null)
		{
			// Perform any action with the attachment object (AttachmentInfo)
		}
	}
}

// To get only unsorted attachments, use the TreeNode.Attachments collection
// To get only page field attachments, use the TreeNode.GroupedAttachments collection

> Back to list of examples

Adding unsorted attachments

// Creates a new instance of the Tree provider
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets a page
TreeNode page = tree.SelectNodes()
	.Path("/Articles")
	.OnCurrentSite()
	.Culture("en-us")
	.FirstObject;


// Prepares the path of the file
string file = System.Web.HttpContext.Current.Server.MapPath("/FileFolder/file.png");

if (page != null)
{
	// Adds the file as an attachment of the page
	DocumentHelper.AddUnsortedAttachment(page, Guid.NewGuid(), file, tree, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE);
}

> Back to list of examples

Inserting attachments into page fields

// Creates a new instance of the Tree provider
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets a page
TreeNode page = tree.SelectNodes()
	.Path("/Articles")
	.OnCurrentSite()
	.Culture("en-us")
	.FirstObject;

if (page != null)
{
	AttachmentInfo attachment = null;

	// Prepares the path of the file
	string file = System.Web.HttpContext.Current.Server.MapPath("/FileFolder/file.png");

	// Inserts the attachment into the "MenuItemTeaserImage" field and updates the page
	attachment = DocumentHelper.AddAttachment(page, "MenuItemTeaserImage", file, tree);
	page.Update();
}

> Back to list of examples

Changing the order of unsorted attachments

// Creates a new instance of the Tree provider
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets a page
TreeNode page = tree.SelectNodes()
	.Path("/Articles")
	.OnCurrentSite()
	.Culture("en-us")
	.FirstObject;

if (page != null)
{
	// Gets an attachment by file name
	AttachmentInfo attachment = DocumentHelper.GetAttachment(page, "file.png", tree);

	// Moves the attachment down in the list
	DocumentHelper.MoveAttachmentDown(attachment.AttachmentGUID, page);

	// Moves the attachment up in the list
	DocumentHelper.MoveAttachmentUp(attachment.AttachmentGUID, page);
}

> Back to list of examples

Modifying attachment metadata

// Creates a new instance of the Tree provider
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets a page
TreeNode page = tree.SelectNodes()
	.Path("/Articles")
	.OnCurrentSite()
	.Culture("en-us")
	.FirstObject;

if (page != null)
{
	// Gets an attachment by file name
	AttachmentInfo attachment = DocumentHelper.GetAttachment(page, "file.png", tree);

	// Edits the attachment's metadata (name, title and description)
	attachment.AttachmentName += " - modified";
	attachment.AttachmentTitle = "Attachment title";
	attachment.AttachmentDescription = "Attachment description.";

	// Ensures that the attachment can be updated without supplying its binary data
	attachment.AllowPartialUpdate = true;

	// Saves the modified attachment into the database
	AttachmentInfoProvider.SetAttachmentInfo(attachment);
}

> Back to list of examples

Deleting attachments

// Creates a new instance of the Tree provider
TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

// Gets a page
TreeNode page = tree.SelectNodes()
	.Path("/Articles")
	.OnCurrentSite()
	.Culture("en-us")
	.FirstObject;

if (page != null)
{
	// Gets an attachment by file name
	AttachmentInfo attachment = DocumentHelper.GetAttachment(page, "file.png", tree);

	// Deletes the attachment
	DocumentHelper.DeleteAttachment(page, attachment.AttachmentGUID, tree);
}

> Back to list of examples