Share via


Exercise 3: Create URL Redirection upon Event Errors

In this exercise you will create an error page the user will be redirected to when there is an error during an event. Cancelling events was introduced in WSS 3.0 but the capability to cancel and redirect the user to an error page is a new capability added in SharePoint Foundation 2010.

  1. Create a new empty SharePoint project. Launching Visual Studio 2010, select File » New » Project and follow these steps to create a new empty project.
    1. Project Types: Installed Templates » SharePoint » 2010
    2. Templates: Empty Project
    3. Name: CustomErrorPage
  2. In the SharePoint Customization Wizard, enter the URL of the site created in the previous step (https://intranet.contoso.com/sites/Lab04) for the debugging site and select the Deploy as a farm solution box.
  3. The first step is to create the event receiver that will test if a due date is filled out when a task is created. This is done by adding a new item to the project. Right-click the CustomErrorPage project in the Solution Explorer tool window and select Add » New Item... Pick Event Receiver from the list of SharePoint » 2010 templates and give it a name of DueDateEventReceiver. The wizard dialog will then prompt you for the event receiver properties. Use the following to complete the wizard dialog:
    1. When prompted for type of event receiver, choose a value of List Item Events.
    2. The second dropdown is populated with all list templates from the target site. Choose the Tasks list.
    3. In the Handle the following events list check the value An item is being added.

      Figure 1

      Add an event receiver

    4. Click the Finish button.

      Open the DueDateEventReceiver\DueDateEventReceiver.cs/vb code file. Replace the contents of the ItemAdding event with the following code:

    C#

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

    VB.NET

    Public Overrides Sub ItemAdding(ByVal properties As SPItemEventProperties) If (properties.AfterProperties("Due Date") Is Nothing) Then properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl properties.RedirectUrl = _ "/_layouts/CustomErrorPage/DueDateErrorPage.aspx" End If End Sub
  4. Each time a task is created within the current site, the event receiver will fire checking if a due date is filled out. If not, the event receiver will cancel the creation of the task and redirect the user to a custom ASPX page named DueDateErrorPage.aspx that you will create in the next few steps.
  5. First, think about why you would want to create a custom error page. Using this technique you can provide the user with rich contextual information on the problem and guidance about how to resolve it. For now you’ll just create a simple custom error page to tell the user that it is not possible to add a task without a due date.
  6. Now it's time to create a custom application page that will be used by your event handler to redirect the user when things go wrong. Right-click the CustomErrorPage project and select Add » SharePoint “Layouts” Mapped Folder:

    Figure 2

    Add a SharePoint Layouts mapped folder. C# shown, VB.NET similar in Add area.

  7. Right-click the Layouts\CustomErrorPage folder, select Add Item » New Item…, select the Application Page template from the SharePoint » 2010 group and name the file DueDateErrorPage.aspx. (Note: take care to name this aspx page EXACTLY as our event receiver requires an exact match).
  8. Open DueDateErrorPage.aspx in Markup view and update the Main, PageTitle and PageTitleInTitleArea content placeholder to the following:

    ASPX

    <asp:Content ID="Main" contentplaceholderid="PlaceHolderMain" runat="server"> <p> ERROR: You cannot create a task without a due date. </p> </asp:Content> <asp:Content ID="PageTitle" contentplaceholderid="PlaceHolderPageTitle" runat="server"> Due Date Error Page </asp:Content> <asp:Content ID="PageTitleInTitleArea" contentplaceholderid="PlaceHolderPageTitleInTitleArea" runat="server" > Due Date Error Page </asp:Content>
  9. Test your work by saving all changes and pressing [CTRL]+[F5] to build and deploy the solution. Eventually Visual Studio will launch the site in the browser. If no task list is available, create one using the More Options menu item of the Site Actions menu and name it TestTasks.
  10. Create a task with a due date. Create a second task without a due date. When saving the task, the custom error page is displayed:

    Figure 3

    The Due Data Error page

    Note:
    In this exercise you created a new event that used the error page event handler options.