I'm trying to write an event receiver which uses the PortalSiteMapProvider. Without having HTTPContext or SPContext INSIDE the event receiver, how would one go about accessing the PortalSiteMapProvider?
Asked
Active
Viewed 1,615 times
2
-
I would be more interested in how you would use an event receiver without having a SPContext object. Please explain. – Magnus Johansson Oct 08 '09 at 15:33
-
You do not have SPContext inside an Event Receiver. – LB. Oct 08 '09 at 15:40
-
Which type of event are you using? – Magnus Johansson Oct 08 '09 at 15:45
-
You can determine both the web and list from SPItemEventProperties – Magnus Johansson Oct 08 '09 at 16:07
-
But you don't have SPContext to use the PortalSiteMapProvider. Getting a reference to web/list don't give me context. – LB. Oct 08 '09 at 16:53
-
I assume you want to use PortalSiteMapProvider because you want a quick way to query against the web from inside of your event. Is my assumption true? What is it you need to do? – Jeff Oct 10 '09 at 17:32
1 Answers
2
Try this in your event receiver:
var web = properties.Web;
HttpRequest request = new HttpRequest(string.Empty, web.Url, string.Empty);
HttpResponse response = new HttpResponse(new System.IO.StreamWriter(new System.IO.MemoryStream()));
HttpContext impersonatedContext = new HttpContext(request, response);
impersonatedContext.Items["HttpHandlerSPWeb"] = web;
HttpContext.Current = impersonatedContext;
SPContext context = SPContext.GetContext(impersonatedContext);
You should be able to get your SPContext from that.

Kristopher
- 819
- 14
- 31