How to Make Attachment Column in SharePoint List Mandatory?

     Hi, today I'm ready with the next customization in SharePoint list....Yes, today we gonna see how to make the attachment field in SharePoint list MANDATORY.

     We gonna achieve this by doing simple steps !!!!!!! Just follow the steps which I given down :)

Steps ( For New Form ):

  1. Open your SharePoint list where you want to make the Attachment Column Mandatory or Required.
  2. Enabling the Attachment Column in Views (All Items) [Note: we don't need to create a separate column for Attachment because we have it already under the ( ListSettings -->  In bottom of the page we have 'Views' under the views we have 'All Items' click the All Items  ) ].


  3. Now I have two items in my list, One item with attachment file and another item without attachment file. You can see it in below Screen Shot.
  4. Now we need to validate ( if the attachment column is empty give an alert at the same time the file should not be saved without the attachment file into the list ).
  5. Now click (New Item Option --> click Edit Page).
  6. In edit page you will see a text ( Add a Web Part )...Just click that. In next window you will see the Categories in that select ( Media and Content ) then in Parts Section ( Select Script Editor ) then click Add.
    ADDING WEB PART
    ADDING SCRIPT EDITOR

  7. Now you will see a ( Edit Snippet ) option, just click it and new window will open. In that new window paste the script without changing anything into the script editor input box and click Insert.
    EDITING SNIPPET
    ADDING THE SCRIPT

  8. Finally, on Left top of the Screen you will have a Option called ( Stop Editing ), Just click that, so that it will save all your embedded Script into the list.
  9. Now try to Create a item in list where you embedded the Script now and try to save without the attachment file. It will give you an alert now.










     
Script to Copy:


<script type="text/javascript" language="javascript">
function PreSaveAction() {
var elm = document.getElementById("idAttachmentsTable");
 if (elm == null || elm.rows.length == 0)
{
 document.getElementById("idAttachmentsRow").style.display='none';
alert("Attachment is Mandatory! Please attach the appropriate files");
return false ;
}
else { return true ;}
}
</script>

----------------------------------

     Wait not yet finished, we also need to do one more thing mainly. Yes, Now you validated the Attachment field in New Form only what about the Edit Form???

     Yes, your guess is right, we need to do the Same for the Edit form also. If an end user tries to edit the item and he deleted the attachment file after that he trying to save, in this case also you need to restrict him and give an alert. Without the Attachment in edit form, the item should not be updated into the list.

Steps ( For Edit Form ):
  1. Navigate to Edit Page of an Item.
    DELETE THE ATTACHMENT FIRST

    CLICK THE EDIT PAGE OPTION UNDER THE SETTINGS GEAR ICON
  2. Follow the same Steps ( Step 6 to Step 9 ) which is there in New Form.
  3. Finally Save Everything. Then Update the Item by removing the Attachment, then you will see the alert.
     If you want this validation for all your list, then follow the steps which I given in this article.

     That's all for the Day, Hope You have Got some Idea and Result. Don't Forget to Subscribe, Share and Bookmark my blog in your browser for easy access.
   
     Feel Free to ask your doubts....!!!!

HAPPY DEVELOPING...!!!

Comments

Popular posts from this blog

How to GET Attachments from the SharePoint List by using REST API and JAVASCRIPT (Hosted App)