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 ?

  • Can you clarify the question so we can help you? you mentioned CSOM, can you share some code or tell us what you did? – Mohamed Derhalli Dec 12 '16 at 5:01
  • no need CSOM code , required SharePoint c# managed code or jsom – Krumil Patel Dec 12 '16 at 5:04
  • You can use event receiver for that. ItemAttachmentAdding event receiver. Reference: boostsolutions.com/blog/… – Dikesh Gandhi Dec 12 '16 at 10:36
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!

  • 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. – Dikesh Gandhi Dec 13 '16 at 11:59
  • I can't get a attachment count so that's why code is not execute properly. – Mavani Dec 13 '16 at 12:09
  • I have updated code. I have tested it. Its working properly. – Dikesh Gandhi Dec 13 '16 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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