Sharepoint 2010 redirect from event receiver (after creating site)

asp.neteventreceiverredirectsharepointsharepoint-2010

My issue is:
In Sharepoint 2010 after user create site, at first I need to redirect user to my custom page and not to redirect user to url of new site.

I created event reciever for web and tried site after creating to redirect on my page with using SPUtility.Redirect():

public class MySiteEventReceiver : SPWebEventReceiver
{
    public override void WebProvisioned(SPWebEventProperties properties)
    {
        var web = properties.Web;
        base.WebProvisioned(properties);

        string url = web.Site.Url + "/_LAYOUTS/MyPage.aspx";
        SPUtility.Redirect(url, SPRedirectFlags.CheckUrl, HttpContext.Current);
    }
}

But always HttpContext.Current is null.

I was looking for the solution of my problem in the intrnet. And I found it: http://www.sharepointkings.com/2008/05/httpcontext-in-eventhandler.html

I did it:

public class MySiteEventReceiver : SPWebEventReceiver
{
    private static HttpContext oContext;

    public SubSiteEventReceiver() : base()
    {
        if (oContext == null)
        {
            oContext = HttpContext.Current;
        }
    }
    public override void WebProvisioned(SPWebEventProperties properties)
    {
        var web = properties.Web;
        base.WebProvisioned(properties);

        string url = web.Site.Url + "/_LAYOUTS/MyPage.aspx";
        SPUtility.Redirect(url, SPRedirectFlags.CheckUrl, oContext);
    }
}

After that HttpContext was not null ( I set for WebProvisioned properties Synchronous)

<Synchronization>Synchronous</Synchronization>

But in this case I get error "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack".

I also tried to go another way for redirect.
http://sharesilver.wordpress.com/2011/07/24/sharepoint-bugs-1-item-deleting-event-receiver/

But in this case also nothing doesn't work.

I would be grateful for any attempt to help!

Best Answer

Hi u can try this for redirecting from event receiver to your custom page

    properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
    properties.RedirectUrl = "/_layouts/EventReceiver/CustomError.aspx";

For more details check this url

http://www.c-sharpcorner.com/uploadfile/anavijai/how-to-redirect-to-a-custom-page-for-event-receiver-in-sharepoint-2010/

Let me know if working. Thanks

Related Topic