Field placeholder [[Attachment]] in Modern Form's Expressions has additional properties after evaluation that can be used for different scenarios e.g. to configure some validation on the form.
Available properties by the placeholder:
Attachments | returns array of the existing attachements (each elements is an objects which includes FileName, etc, for Edit and Display forms) |
NewFiles | returns the array of attachments added during creation/editing the form |
Removed | returns the array of removed attachments during editing the form (for Edit form) |
UrlPrefix | refers to a part of the URL that serves as a base URL for accessing the file (e.g. https://tenant_host/Lists/list_internal_name/Attachments/1). Can be used with an attachment file relative URL to build full URL to the existing file. |
toString | returns comma separated attachment names, collecting it form Attachments property and NewFiles property |
You can see them with live values in the console when Debug Mode is turned on.
Here are some examples how to use these properties in your expressions:
- Validation expression to have only one attachment:
var a = [[Attachments]];
return a.Attachments.length + a.NewFiles.length == 1;
- Validation expression that allows upload only specific type of files. It will works on New- and Edit Form:
var attachments = [[Attachments]];
var existingAttachments = attachments.Attachments.map(attachment => attachment.FileName);
var newAttachments = attachments.NewFiles.map(attachment => attachment.name);
var allAttachments = existingAttachments.concat(newAttachments);
if (allAttachments.length > 0) {
if (allAttachments.length == 1) {
return allAttachments[0].split('.').pop().trim() == "xlsx";
}
return false;
}
return true;