locked
WSS 3 SPItemEventReceiver ItemAdded RRS feed

  • Question

  • My requirement is to restrict files uploaded to document library based on file tpes (ie: doc, pdf, csv).

    I have created an event handler using SPItemEventReciever class overriding ItemAdded method, since I can’t get after properties in ItemAdding method. So in the ItemAdded method I check the type of file and if it not accepted type (based on config setting) I delete the file and raising a SharePoint Exception with custom message. The issue that I’m having is that the exception is not reaching to client or to custom application that uploads files to doc library.

    using (web = properties.OpenWeb())

                {

                    foreach (SPGroup iospGroup in web.CurrentUser.Groups)

                    {

                        spUsrGroups += iospGroup.ToString();

                    }

     

                    SPList myList = web.Lists[properties.ListId];

                    SPListItem myListItem = myList.GetItemById(listItemID);

     

                    fileName = myListItem["Name"].ToString();

                    fileExt = System.IO.Path.GetExtension(fileName).Substring(1);

     

                    if (spUsrGroups != "Documents Owners")

                    {

                        if (fileExt != "pdf")

                        {

                            myListItem.Delete();

                            myListItem.Update();

                            throw new SPException("This file type not accepted");

                        }

                    }

                }

    Could someone please let me know what I’m doing wrong or is there is another way to do this.

    Wednesday, May 11, 2011 6:14 AM

Answers

All replies

  • Hi,

    there is one more option which is more easier then this.

    Go to Central Admin > Operations > Blocked File Types and you can add file extensions which you want to restrict to be uploaded to the specific web app.

    when any user tries to upload that restricted extension file he / she will get an Error message : The following file(s) have been blocked by administrator : File name

    hth

     


    Warm Regards, Pratik Vyas | SharePoint Consultant | http://sharepointpratik.blogspot.com/
    Wednesday, May 11, 2011 11:36 AM
  • @Pratik: User wants to block file type only for that doc library not for whole appln.

    First thing is debug the code to see, why control not reach to that place. One more thing is server side validations we do in ItemAdding event not in added event.

    Checkout nice tutorial on event reciever here

    http://msdn.microsoft.com/en-us/library/ff713708(v=office.12).aspx#WSS2007_ItemEvent


    w: http://www.worldofsharepoint.com | t: @sharesandip
    • Marked as answer by Leoyi Sun Friday, May 20, 2011 9:38 AM
    Wednesday, May 11, 2011 12:55 PM
  • How about using the BeforeProperties of the Item Event Receiver?

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventproperties.beforeproperties(v=office.12).aspx

    Note that you need to be working in a DocumentLibrary.


    - Michael Mukalian - 2010 MS MVP SharePoint Services - MCTS: MOSS 2007 Configuration - http://www.mukalian.com/blog
    Thursday, May 12, 2011 12:03 AM
  • Hi Everyone,

    Thanks for your feedback.

    I want to implement this Event Handler for Document Libraries on site level and allow SharePoint Admin users to choose what file type (extensions) are allowed in the document libraries. That file type information I could store it in list or in xml file in the file system and read the list allowed file type allowed in ItemAdding method. The main issue I’m facing that AfterPropertise on ItemAdding method for Document Libraries are null.

    Hence I opted to use ItemAdded method to get file name and I manage to do the validation in this method and delete the file if the file extension is not allowed. The issue I’m having here is that after deleting the file I’m invoking (throwing) an exception with custom message that doesn’t go up the stack. What I noticed is that an error in the windows event log “: Item does not exist. It may have been deleted by another user” when a file is deleted. I tried DisableEventFiring() method before I delete the file and I still get that error in windows event log and the custom exception that I invoke doesn’t reread to the client.

     

    Thursday, May 12, 2011 12:58 AM