1
votes

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!

2
If you are able to get the context properly then instead of using SPUtility.Redirect, you can try oContext.Response.Redirect("url", false), the second parameter(false) is importantMac

2 Answers

0
votes

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

0
votes

You need to add a constructor to retrieve the current httpcontext otherwise your context will always be to null.

See the following example : http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/8cfcb299-9a55-4139-86ec-0b53b2922a54/