"attachmentSelector" Macro

Last modified by Eleni Cojocariu on 2026/09/23 16:29

Reference

Description

The attachmentSelector macro binds a picker to an xobject property holding a string: the reader picks one of the Page's attachments, and the macro writes that attachment's name into the property. It is how a sheet asks for a file, such as the photo on a user profile or the logo of a theme.

In view mode the macro prints the name of the attachment, or the image itself when the attachment is an image and displayImage is set.

In edit mode it also renders a button that opens the selector: a gallery of the Page's attachments, from which the reader selects one, uploads a new one, replaces or deletes an existing one, or empties the property.

With savemode="form", an attachment uploaded from the selector is held as a temporary attachment and written to the Page only when the form around the macro is saved, which is why the selector marks those attachments with a clock icon.

Information

The macro saves the property on the Page it is displayed on. targetdocname changes whose attachments the gallery lists, not where the property is written.

Use the attachmentGalleryPicker macro instead when there is no property to bind to: it searches the attachments of every Page through the search index and reports the reader's choice to a script.

Usage

{{attachmentSelector classname="Space.Page" property="propertyName"/}}

{{attachmentSelector classname="Space.Page" property="propertyName" object="0"/}}

{{attachmentSelector classname="Space.Page" property="propertyName" filter="pdf,odt"/}}

{{attachmentSelector classname="Space.Page" property="propertyName" displayImage="true"/}}

{{attachmentSelector classname="Space.Page" property="propertyName" savemode="direct"/}}

The macro takes no content, and it works inline as well as on its own line. Its visibility is "Current Wiki", so it is available only in the wiki the Attachment Application is installed on.

Parameters

NameMandatoryAllowed ValuesDefault ValueDescription
classnameyesA document referencenoneThe Page holding the XClass whose property this picker edits.
propertyyesA property namenoneThe property the picker writes the attachment name into.
objectnoAn object numberThe first object of classname on the PageWhich object the property belongs to, when the Page carries several of that class.
cssClassnoA CSS class namenoneSet on the element surrounding the displayed value.
savemodenoform, directformHow the property is updated. form stores the selected value in an input that an enclosing form saves; direct makes the picker save the property itself.
buttontextnoAny textThe xe.attachmentSelector.selectFile translationText of the button that opens the selector.
defaultValuenoAn attachment referencenoneWhat view mode shows while the property is empty, written as photo.png for an attachment of the same Page or [email protected] for one of another Page.
filternoA comma-separated list of file extensionsnoneRestricts the gallery to attachments with those extensions. Every attachment is accepted when it is empty.
displayImagenotrue, falsefalseWhether an image attachment is shown as the image itself rather than as its name.
widthnoA length in pixelsnoneWidth of the displayed image. Read only with displayImage="true".
heightnoA length in pixelsnoneHeight of the displayed image. Read only with displayImage="true".
alternateTextnoAny textnoneThe alt attribute of the displayed image. Read only with displayImage="true".
linknotrue, falsefalseWhether view mode wraps the displayed name or image in a link to the attachment.
targetdocnamenoA document referenceThe current PageThe Page whose attachments the gallery lists.
versionSummarynotrue, falsefalseWhether the reader may type a version summary to record in the Page history. An automatic message is used when none is given.

Examples

Ask for an Image

The photo on a user profile is asked for this way: the picker displays the image itself, and falls back to a default avatar while the property is empty.

{{attachmentSelector classname="XWiki.XWikiUsers" property="avatar" savemode="direct"
  displayImage="true" width="180" filter="png,jpg,jpeg,gif"
  defaultValue="[email protected]"/}}

attachmentselector-image-mode.png

Ask for a Document

Without displayImage, the picker shows the name of the attachment, linked here to the file itself.

{{attachmentSelector classname="Sandbox.AttachmentSelectorDemo" property="handbook"
  filter="pdf,odt" link="true"/}}

attachmentselector-file-mode.png

Upload Without Saving the Page Yet

A picker inside a form uses savemode="form", the default, so that a file the reader uploads and then abandons is never written to the Page.

{{attachmentSelector classname="Sandbox.AttachmentSelectorDemo" property="handbook"
  savemode="form"/}}

attachmentselector-temporary-attachment.png

Related

Get Connected