In below code I am canceling file upload when user tries to upload a file other than an image file.

But when I cancel the file it shows below error.

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

    public override void ItemAdding(SPItemEventProperties properties)
        string strFileExtension = Path.GetExtension(properties.AfterUrl);
        bool isValidExtension = false;
        string strValidFileTypes = string.Empty;
        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. 

        //problem in canceling file upload
        if (!isValidExtension)
            properties.Status = SPEventReceiverStatus.CancelWithError; 
            properties.ErrorMessage = "Only Images are Allowed !";




Runtime Error Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed.

Details: To enable the details of this specific error message to be viewable on the local server machine, please create a tag within a "web.config" configuration file located in the root directory of the current web application. This tag should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off".

<!-- Web.Config Configuration File -->

        <customErrors mode="RemoteOnly"/>

I tried to change the web.config file it still gives above error

  • I've never used two different error messages like that. Perhaps that's what's causing the issue? Try removing one of those lines. Also, after making the web.config change did you do an IIS reset?
    – Submits
    Commented Jun 15, 2015 at 8:03
  • No this is not the issue.. I tried. Commented Jun 16, 2015 at 4:10

1 Answer 1


Try to set it to "Off". Remote Only is only when you are on the SharePoint Server locally.

  • I tried that to set it to Off but no luck. my sharepoint server is local Commented Jun 16, 2015 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.