How to check for attachments in ItemAdding() event

Item adding being a synchronous event nor the list item and attachments are committed in database. Hence if you need to get the attachments in ItemAdding() event then you could do so by ASP.NET request object

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web;

namespace MyProject.Features
{
    public class MyEventReceiver : SPItemEventReceiver
    {
        HttpContext currentContext;

        public MyEventReceiver()
        {
            currentContext = HttpContext.Current;
        }
       
        public override void ItemAdding(SPItemEventProperties properties)
        {            
  if (currentContext != null)
        {
          if (currentContext.Request.Files.Count > 0)
          {

            // there are attachments

          }
   else
   {

            // there are no attachments

   }
        }                           
        }
     }
}

Related Post

7 thoughts on “How to check for attachments in ItemAdding() event”

  1. I am attaching only one file, in currentContext.Request.Files.Count i am getting file count == 2, can you please explain why it is like that. thanks

  2. hi,

    i am new to sharepoit

    how to count the attachment in a particular custom list and its compare to the the

    drop down list numbers they its meet the numbers its show the message like 5 more pages 2 more pages etc

    please any one to help me its very urgent

Leave a Reply

Your email address will not be published. Required fields are marked *