5

I want to restrict user from uploading multiple attachments in SharePoint list NewForm or EditForm. User should able to attach/upload only 1 file.

How can I do this ?

3
  • Can you clarify the question so we can help you? you mentioned CSOM, can you share some code or tell us what you did? Dec 12, 2016 at 5:01
  • no need CSOM code , required SharePoint c# managed code or jsom Dec 12, 2016 at 5:04
  • You can use event receiver for that. ItemAttachmentAdding event receiver. Reference: boostsolutions.com/blog/… Dec 12, 2016 at 10:36

1 Answer 1

4

Create an event receiver on the list with ItemAttachmentAdding event.

Put below code in your event method:

 public override void ItemAttachmentAdding(SPItemEventProperties properties)
    {
        base.ItemAttachmentAdding(properties);

        try
        {
            SPAttachmentCollection attachments = properties.ListItem.Attachments;
            if (attachments.Count >= 1)
            {
                properties.Status = SPEventReceiverStatus.CancelWithError;
                properties.ErrorMessage = "You cannot add more than one attachment.";
            }
        }
        catch (Exception ex)
        {
            properties.ErrorMessage = ex.Message;
            properties.Status = SPEventReceiverStatus.CancelWithError;
        }    
    }

Hope this will help you!

3
  • What Happened? Is there any problem in the code? If its useful to you, then kindly accept this answer. So it will be useful to others. Dec 13, 2016 at 11:59
  • I can't get a attachment count so that's why code is not execute properly.
    – Mavani
    Dec 13, 2016 at 12:09
  • I have updated code. I have tested it. Its working properly. Dec 13, 2016 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.