none
Question on Customising the Error message during files upload

    Question

  • Dear all,

    I use the event receivers to control the file uploading behaviour and there will be error raised when I cancel the upload event through the event receivers. 

    I can use the SPEventReceiverStatus referenced here, https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.speventreceiverstatus.aspx, the cancel a upload event. 

    I would like to have both error message and error page customised. 

    However, I can only set the status once, which means I can have either the customised error message or the customised error page. 

    Is there any way to have both error message and error page customised?

    Thank you for the help. 

    Friday, December 16, 2016 9:07 AM

All replies

  • Hi,

    In SharePoint 2013 , please use _currentContext.Response.Redirect to redirect the error page with your custom error message.

    public class TestEventHandler : SPItemEventReceiver
     {
      private readonly HttpContext _currentContext;
      public TestEventHandler()
      {
       _currentContext = HttpContext.Current;
      }
      // Methods
      public override void ItemAdding(SPItemEventProperties properties)
      {
       var url = new StringBuilder("test.aspx");
       string urlRedirect = null;
       bool flag = SPUtility.DetermineRedirectUrl(url.ToString(), SPRedirectFlags.RelativeToLayoutsPage, _currentContext, null, out urlRedirect);
       _currentContext.Response.Redirect(urlRedirect, true);
      }
     }

    More information is here:

    http://blog.sharepointalist.com/2013/03/sharepoint-2013-event-receiver-redirect.html

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com


    Tuesday, December 20, 2016 2:52 AM
    Moderator
  • Hi Dennis,

    Thank you for the reply. 

    I tried your suggestion but I got the Exception "Cannot redirect after HTTP headers have been sent.". 

    I tried to handle it with code below but the exception remains. 

    if (!Response.IsRequestBeingRedirected) {
      _currentContext.Response.Redirect(urlRedirect, true);
    }
    Here is how I added your suggest into my ItemAdding event receiver.  I tried reordering properties.Status part and Response.Redirect part but it does not help. 

    public override void ItemAdding(SPItemEventProperties properties)

    {
    //Some checking code
    //invaild handling start
    string errorMessage = string.Format(errMessageTemplate, reason); properties.Status = SPEventReceiverStatus.CancelWithError; properties.ErrorMessage = errorMessage; string url = string.Format("/_layouts/{0}{1}&IsDlg=1", customErrPageURL, errorMessage); string urlRedirect = null; bool flag = SPUtility.DetermineRedirectUrl(url.ToString(), SPRedirectFlags.RelativeToLayoutsPage, _currentContext, null, out urlRedirect); if (!Response.IsRequestBeingRedirected) {
    _currentContext.Response.Redirect(urlRedirect, true);
    }
    //invaild handling end
    }

    Do you have any other suggestion to have the custom error message and the custom error page?
    Thursday, December 22, 2016 8:11 AM
  • Hi,

    Please note: "_currentContext.Response.Redirect does work though (IMPORTANT: Only when the form, which initiates an event, is being rendered in CSRRenderMode.ServerRender mode.) "

    Please go to the list new form page and edit the list view web part, select "Server Render" in the web part settings.

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com


    Thursday, December 22, 2016 8:28 AM
    Moderator
  • Hi,

    Any update?

    If the replies help you, please mark the reply as answer, it will make others who stuck with the similar issue easier to search for valid solutions in this forum.

    Thanks


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, December 28, 2016 6:43 AM
    Moderator