Question

I have created an event receiver on the "Task" list.

If the "Due Date" is null then the user should be redirected to the custom error page.

Custom error page resides in the SharePoint mapped folder under "Layouts" directory.

The event receiver code is as follows:

public override void ItemAdding(SPItemEventProperties properties)
{
    try
    {
        if (properties.AfterProperties["Due Date"] == null)
        {
            properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
            properties.RedirectUrl = "_layouts/CustomErrorPage/DueDateErrorPage.aspx";
        }
    }
    catch (Exception ex)
    {

    }
}

The custom error page which is of type "Application Page" having name. "DueDateErrorPage.aspx". The mark up is:

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">

</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
ERROR : You cannot create the task without due date
</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Custom Error Page
</asp:Content>

<asp:Content ID="PageTitleInTitleArea"     ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
Custom Error Page
</asp:Content>

Though in IIS, under the _layouts direcory the page is available I am getting this output.

enter image description here

How should I tackle this error? Please help.

Was it helpful?

Solution 2

First of all I first checked the url in web browser that is:

/_layouts/CustomErrorPage/DueDateErrorPage.aspx

It was opening the page that means the page is available but there is something missing in the url only.

So by using the SPUrlUtility class I was able to redirect the page.

The changes I needed to made was:

properties.RedirectUrl = SPUrlUtility.CombineUrl(properties.WebUrl,"/_layouts/CustomErrorPage/DueDateErrorPage.aspx");

It's working perfectly..

OTHER TIPS

I think your redirect url is invalid, because it will be relative to the current page, and not the current site or site collection.

You can diagnose this by looking at the dialog frame properties, and check the url.

  1. Method 1 : solve the amgiguity

    To resolve ambiguity, I suggest you to use the SPUtility.GetServerRelativeUrlFromPrefixedUrl method.

    This will create a correct url.

    Especially, change your code like this:

    properties.RedirectUrl = SPUtility.GetServerRelativeUrlFromPrefixedUrl(
        "~site/_layouts/CustomErrorPage/DueDateErrorPage.aspx"
        );
    

    This will dynamically resolve ~site to your current SPWeb url. You can also use ~sitecollection to resolve the site collection.

  2. Method 2: make the field mandatory

    Why don't you simply make the due date mandatory?

  3. Method 3: use the standard sharepoint error page (not tested I admit)

    Instead of redirecting to your own page, "Cancel" the event with a custom error message:

        properties.Status = SPEventReceiverStatus.CancelWithError;
        properties.ErrorMessage= "Due date is mandatory";
    

    Moreover, CancelWithRedirectUrl is now obsolete.

PS: as a side note, you should know that a SharePoint dedicated Stack Exchange site exists.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top