We might come across scenarios where we must work with “attachments”. Ideally, SharePoint libraries are the answer, but due to various reasons, we might still have to use lists rather than a library. Now, there is no direct means to make an “Attachments” field mandatory in SharePoint Lists. But, we can very easily work around this with a some tweaks here and there. Read on to know how!
This article explains how file attachments can be made mandatory in Lists. It can be done in two different ways.
- CSOM method of making list attachments mandatory
- InfoPath way of making list attachments mandatory
We will show these methods in a simple list in SharePoint 2010 – Employees, that contains 4 columns.
CSOM method of making list attachments mandatory
This method makes use of the PreSaveAction() method of SharePoint. This method allows us to override the behavior of a “Save” button.
Step 1
Open the list. Right-click “Add new item” and open the page in a new tab. Our goal is to edit the newform.aspx page.
Step 2
Edit the page and add a content editor webpart.
Step 3
Edit the content editor and add the below code,
- function PreSaveAction() {
- var myAttachments = document.getElementById("idAttachmentsTable");
- if (myAttachments == null || myAttachments.rows.length == 0) {
- document.getElementById("idAttachmentsRow").style.display = 'none';
- alert("Attachments are mandatory for this list. Please attach the appropriate files");
- return false;
- } else {
- return true
- }
- }
This concludes the steps. Now users will not be allowed to save the list item without attaching a document.
InfoPath way of making list attachments mandatory
The outline of this method is basically creating a hidden text box that holds the name of the attachment file. Then by creating a validation rule we make sure that the attachment isn’t empty.
Step 1
Open the list for editing in InfoPath,
Step 2
Add a new text box. Open its Text Box Properties.
Step 3
Set its default value to the Attachment. Click on the “fx”
Step 4
Click on “Insert Field or Group”. Then select the “Attachment”. Follow this link
Insert Field or Group - Attachments - Ok - Ok
Step 5
Create a validation rule in the text box as shown below. In conditions, select the “Attachments” with condition is “not blank”
Step 6
Similarly, create a new rule for hiding the text box. This concludes the steps.
This article can also be found in my personal blogsite – SharePoint Surgeon