Make attachments mandatory in SharePoint Lists

Published by Satyajit Paul on

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 library. Now, there is no direct means to make an “Attachments” field mandatory in SharePoint Lists.  But, we can very easily get it worked around with a little bit of 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 to override behavior for a “Save” button

Step 1. Open the list. Right click on the “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.

 5,529 total views,  2 views today

Care to Share?

Satyajit Paul

SharePoint Developer with 6+ years of experience. Skilled in Add-in Development, BPM, CSOM and Migrations.

3 Comments

Rose Harpen · March 1, 2019 at 4:06 PM

Thank you very much for posting this solution.

I am still learning SharePoint and I have been all over the internet for two days to find a way to do this but all the solutions I found didn’t work except this one. Would You tell Does this work for SharePoint online?

    Satyajit Paul · March 31, 2019 at 6:15 PM

    Yes, it will work for SP Online, provided you put in your code in classic SharePoint mode.

Prateek Mangal · August 28, 2019 at 8:34 PM

In SharePoint, you can user SPFX solution for modern page or PowerApps customized form.

Leave a Reply