none
SharePoint document library file upload size limit by using event handlers

    Question

  • Hi All,

    Can any one help me how to set file upload size limit to 5MB for document library by using SharePoint event handlers.

    Thanks in advance.

     

     

    Monday, August 22, 2011 11:22 AM

All replies

  • Hi

    you have 2 choices

    first one, to configure in web app ( general config ) , file upload limit to 5M

    or

    to attach an workflow to your document library which will advertise and block all files with size >5Mb

     


    romeo donca
    Monday, August 22, 2011 12:06 PM
  • Hi,

    Thanks for reply, I need it by using a event handlers only.

    Monday, August 22, 2011 12:19 PM
  • Hello,

    You can use the following lines of code for the event handler

    public class LimitTheFileSize:SPItemEventReceiver   
        {
            private const long validDocSize = 5242880;//This size is in bytes , so for 5 MB ( 5 * 1024 * 1024 = 5242880)
            private long currentFileSize;currentDocSize
            public override void ItemAdding(SPItemEventProperties properties)
            {
                try
                {
                    if (properties.ListTitle == "DocumentLibraryName")
                    {
                        base.ItemAdding(properties);
                        this.DisableEventFiring();

                        currentDocSize = long.Parse(properties.AfterProperties["vti_filesize"].ToString());
                        if (currentDocSize> validDocSize)
                        {
                            properties.ErrorMessage = "Please upload files less or equal to 10 MB";
                            properties.Cancel = true;
                        }
                        this.EnableEventFiring();
                    }
                }
                catch (Exception ex)
                {                
                    Throw ex
                }
                finally
                {
                    this.EnableEventFiring();
                }
            }

            public override void ItemUpdating(SPItemEventProperties properties)
            {
                try
                {
                    if (properties.ListTitle == "DocumentLibraryName")
                    {
                        base.ItemUpdating(properties);
                        this.DisableEventFiring();
                        currentDocSize = long.Parse(properties.AfterProperties["vti_filesize"].ToString());
                        if (currentDocSize > validDocSize)
                        {
                            properties.ErrorMessage = "Please upload files less or equal to 10 MB";
                            properties.Cancel = true;
                        }
                        this.EnableEventFiring();
                    }
                }
                catch (Exception ex)
                {                
                    Throw ex
                }
                finally
                {
                    this.EnableEventFiring();
                }
            }

                       

    Hiren Patel

    Friday, July 06, 2012 6:20 AM
  • Hi romeo donca,

    Can u pls let me know how can we create a workflow to check the file size whether it is greater than 5 MB or not.

    Thanks in advance

    Suresh Kumar Gundala

    Monday, September 24, 2012 9:52 AM