Applies to:
SharePoint Online and SharePoint On-premises
Description
This article describes the steps to use JavaScript to count the total number of attachments for an item in the list and then, display the total count of these attachments in a specified column of the list.
This scenario can also be used to include validation for the number of attachments and is explained below.
The script used in this article is tied to a ‘Submit Action’ button and can be found for reference within the explained steps.
Detailed Steps
- Navigate to the desired list where the total attachments need to be counted and open the NITRO Forms for this list.
- Add the required columns of the list in the NITRO Forms designer page.
- To count the total attachments and populate this value to a specific column, create a new column (For example, a single line of text column) and provide it a name.
- In this example, we have used a ‘Single line of text’ column that has been named as ‘Docs’ (this name can be changed as per the requirement), and this column’s internal name is only used in the script.
- Then drag and drop a ‘Submit Action’ button present under ‘Actions’ tab as shown below:
- The default ‘Save’ button can be hidden by selecting ‘Hide Save Button’ option present under the Layouts and Theme -> Buttons as shown below:
- Once the Submit Action button is added, click on the ‘Edit’ button present adjacent to the ‘Pre-Save Process’ option and use the below script as shown below:
Script used above:
var attachcount = (window.ccs_g_FormUI.fetchAttachments() || []).length;
formContext.setColumnControlValueByName("Docs",attachcount);
functionCallback();
- This script would count the total number of attachments for any newly created item in the list as well as during the editing of the pre-existing item in the list and on submitting the form, it would display the total number of attachments in the ‘Docs’ column.
- Publish the NITRO Form to check the result.
- A sample output generated from the list configured with above mentioned steps is as shown below:
- The attached files can also be restricted using validations in attachment settings as shown below:
- Click on ‘Add’ to configure a new validation. In this example scenario, we are setting the maximum attachments limit to 2 for the default SharePoint Attachments column as shown below:
- If a user tries to attach more than 2 files (as specified in the above scenario) either in NITRO Attachments column or in the default SharePoint column, a message will appear as shown below:
Script to count only the NITRO Attachments column:
If there is a requirement to count the number of attachments in a NITRO Attachments column and not the total attachments in the list item, we can modify the script as shown below:
In this example, we have used the name ‘NITRO Attachments’ for NITRO Attachments column. This name can also be changed as per the requirement.
Script used above:
var objNITROAttachment = formContext.fetchColumnValueUI("NITROAttachments").split(/[\s;]+/);
if(objNITROAttachment && objNITROAttachment.length > 0){
formContext.setColumnControlValueByName("Docs",objNITROAttachment.length);
}
functionCallback();
The Docs column has count of files in the NITRO Attachments column only: