Thursday, July 26, 2012

File Extention Validation Document Library Event Receiver


   protected string[] ValidExtensions = new string[] { "png", "jpeg", "gif" };

        public override void ItemAdding(SPItemEventProperties properties)
        {
            string strFileExtension = Path.GetExtension(properties.AfterUrl);

            bool isValidExtension = false;

            string strValidFileTypes = string.Empty;

            using (SPWeb web = properties.OpenWeb())
            {
                foreach (string strValidExt in ValidExtensions)
                {
                    if (strFileExtension.ToLower().EndsWith(strValidExt.ToLower()))
                    {
                        isValidExtension = true;
                    }
                    strValidFileTypes += (string.IsNullOrEmpty(strValidFileTypes) ? "" : ", ") + strValidExt;
                }

                // Here i am going to check is this validate or not if not than redirect to the
                //Error Message Page.
                if (!isValidExtension)
                {
                    properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
                    properties.RedirectUrl = properties.WebUrl + "/_layouts/error.aspx?ErrorText=" + "Only " + strValidFileTypes + " extenstions are allowed";

                }
            }
            //base.ItemAdding(properties);
        }

1 comment:

  1. I have a month column which is a choice column in my document library in the format specified below.. For eg. Jan 2013 I have the following requirements 1. The month dropdown should always have only those months which start from 6 months previous of the current month.For eg.If the current month is March 2013 then only the months starting from oct 2012 to march 2013 should be populated in the dropdown 2.There is another column in my document library called 'date' of date format.It is not to be shown in the default view.The date field should automatically be set as the 1st date of the month selected from the month column.For example. IF the month selected is March 2013 .The date Should b 1/3/2013. Please Help!!Its urgent..can This be done by event reciever..If yes then Step by Step Explanation With Code is needed.. Any Help is welcome...Thanks in Advance..

    ReplyDelete