none
Count no of files attached in itemadding event in sharepoint RRS feed

  • Question

  • hii

    I want to count no of files attached in itemadding event..

    This is the code i have written

    code:

      HttpContext currentContext;
            public EventReceiver3()
            {
                currentContext = HttpContext.Current;
            }
            public override void ItemAdding(SPItemEventProperties properties)
            {
                base.ItemAdding(properties);
            
                if (properties.ListTitle == "Leave_list")
                {

                    if (Convert.ToString(properties.AfterProperties["LeaveType"]) == "MedicalLeave")
                    {
                       
                                
                                    //SPAttachmentCollection attchList = properties.ListItem.Attachments;
                                    //int countElem = attchList.Count;
                                     if (currentContext != null)
                                    {
                                        if (currentContext.Request.Files.Count < 1)
                                        {
                                            properties.Cancel = true;
                                            properties.Status = SPEventReceiverStatus.CancelWithError;
                                            properties.ErrorMessage = "Please Attach The Document";
                                        }
                                    }
                                     else
                                     {

                                         // there are no attachments

                                     }
                    }

                }

                
            }

    Error: at     if (currentContext.Request.Files.Count < 1)  i am getting the exception "This method or property is not supported after HttpRequest.GetBufferlessInputStream has been invoked"

    Please help me

    Friday, December 20, 2013 5:25 AM

Answers

  • You can use SPcontext instead of HttpContex, try this code, you can also try SPContext to

    properties.current.web

       SPWeb myweb = SPContext.Current.Web;
                SPSite mysite = SPContext.Current.Site;
                SPList mylist = SPContext.Current.List;
                SPListItem myitem = SPContext.Current.ListItem;
                SPAttachmentCollection myattach = myitem.Attachments;



    Mark ANSWER if this reply resolves your query, If helpful then VOTE HELPFUL
    INSQLSERVER.COM Mohammad Nizamuddin

    Friday, December 20, 2013 8:43 AM

All replies