Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am using an ItemAdded event handler for doing some validation. If my validation fail is it possible to redirect the user to another page. Ideally it would be good to show the page in a dialog box.

Sadly setting the status on on the SPItemEventProperties to SPEventReceiverStatus.CancelWithRedirectUrl and setting the RedirectUrl does not work.

This is happening on a Document library. This means I can't do the validation in the ItemAdding event as BeforeProperties, AfterProperties & properties.ListItem does not contain the information. (http://www.synergyonline.com/Blog/Lists/Posts/Post.aspx?ID=122)

Update When trying out @Andy code below I get the following:

System.Web.HttpException was unhandled by user code 
Cannot redirect after HTTP headers have been sent
share|improve this question
add comment

3 Answers

This works, but only if the form is open in Dialog mode, for some reason.

//Constructor                                   
System.Web.HttpContext _context = null;
public MyEventHandler()
{
    _context = System.Web.HttpContext.Current;
}

public override void ItemAdded(SPItemEventProperties properties)
{
    SPList list = properties.List;
    if (!list.NavigateForFormsPages)
    {
        if (_context != null)
        {
            _context.Response.Redirect(webUrl + "/someurl", false);
        }
    }
}
share|improve this answer
    
Sadly getting a "System.Web.HttpException was unhandled by user code" "Cannot redirect after HTTP headers have been sent" –  John Jul 16 '12 at 10:56
add comment

from my understanding about event receivers is that you cannot edit the users current session as it is back end work.

perhaps a workflow with a workflow history could be used in your case.

if the validation fails you can log the desired redirection URL in the history they can follow it and rerun the workflow when they have made the validation changes

share|improve this answer
add comment

If you want to add validation and have the possibility of canceling the update and redirect the user, then you should implement the ItemAdding event receiver instead of the ItemAdded.

share|improve this answer
    
Your right. There will be a follow up question to this. –  John Jul 16 '12 at 12:42
    
sharepoint.stackexchange.com/questions/41046/… follow up –  John Jul 16 '12 at 12:45
    
As this is a document library the BeforeProperties, AfterProperties & properties.ListItem are all null on ItemAdding so can't. synergyonline.com/Blog/Lists/Posts/Post.aspx?ID=122 –  John Jul 17 '12 at 8:11
    
Then you should write that in the Question, as the answer will then be that you can't do what you want –  Per Jakobsen Jul 17 '12 at 8:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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