如何在Event Handler中获取HttpContext
我试图在 MOSS 文档库的事件处理程序中获取 HTTPContext,但我所拥有的只是 HTTPContext.Current 的空值,我在列表上执行相同的操作并返回 HTTPContext。有没有办法获取文档库中的HTTPContext来访问HTTPContext.Request方法?
感谢您的帮助
这是代码:
public class TestContextListItemEventReceiver : SPItemEventReceiver
{
HttpContext current;
static object obj;
/// <summary>
/// Initializes a new instance of the Microsoft.SharePoint.SPItemEventReceiver class.
/// </summary>
public TestContextListItemEventReceiver()
{
current = HttpContext.Current;
}
public override void ItemAdding(SPItemEventProperties properties)
{
obj = current;
}
}
I’m trying to obtain the HTTPContext within an Event Handler in a Document Library in MOSS, but all I have is a null value of the HTTPContext.Current, I do the same thing on a List and the HTTPContext is returned. There is a way to obtain the HTTPContext in Document Libraries to access the HTTPContext.Request method?
Thanks for your help
Here is the code:
public class TestContextListItemEventReceiver : SPItemEventReceiver
{
HttpContext current;
static object obj;
/// <summary>
/// Initializes a new instance of the Microsoft.SharePoint.SPItemEventReceiver class.
/// </summary>
public TestContextListItemEventReceiver()
{
current = HttpContext.Current;
}
public override void ItemAdding(SPItemEventProperties properties)
{
obj = current;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
步骤1
声明:
步骤 2
步骤 3
步骤 4
Step 1
Declare:
Step 2
Step3
Step 4
当我在上传新文档时尝试更新文档库的某些自定义字段时,我遇到了同样的问题,该字段是 (ProjectID),我将其放入 Web 部件的会话中(上传文档之前的步骤)。
我所做的是:我将
projectID
放入自定义 Web 部件内的缓存(每个用户)中,该部件充当会话,如下所示:然后我实现了
ItemAdded
事件,然后我通过以下方式获取缓存的projectId
值:I faced the same issue when I was trying to update some custom fields of my document library when uploading new documents, the field was (ProjectID) which I put it inside a session in my webpart (the step before uploading the document).
What I did is: I put the
projectID
into the cache (per user) inside the custom webpart which acts as a session as follows:Then I implemented the
ItemAdded
event and I get the value of the cachedprojectId
through:项目事件接收器异步运行;您将无权访问发起该事件的 HTTP 请求。
An item event receiver run asynchronously; you will not have access to the HTTP request that initiated the event.
如果您从 SharePoint 界面 (Internet Explorer) 上传文档,则可以在 SPList 和文档库中捕获 HttpContext。但是,如果您从 Microsoft Word 保存文档,则无法捕获 HttpContext,我不知道为什么。
You can catch the HttpContext in both SPList and Document Libraries, if you upload the document from the SharePoint Interface (Internet Explorer). But if you save the document from Microsoft Word the HttpContext can't be catched, i don't know why.
尝试使用 HttpRuntime 类
Try using the HttpRuntime class
如果用户尝试上传一个文档,我可以从 ItemAdding Event 内部获取会话对象,但问题是当用户使用文档库选项上传多个文档(上传多个文档)时,httpcontext.current 始终为 null
I can get the session object from inside ItemAdding Event if the user try to upload one document, but the problem is the httpcontext.current is always null when the user upload multiple documents using the document libarary option ( upload multiple document )
您可以在事件接收器中伪造 HttpContext 和 SPContext,如我的文章中所述:
http:// pholpar.wordpress.com/2011/06/26/injecting-httpcontext-and-spcontext-into-the-event-receiver-context/
You can fake the HttpContext and SPContext in event receivers as described in my post:
http://pholpar.wordpress.com/2011/06/26/injecting-httpcontext-and-spcontext-into-the-event-receiver-context/
如果将其放置在这样的静态变量中,则还会有多个人使用相同的上下文对象,该对象将成为首次运行事件接收器的用户的上下文,并且并发更改可能会产生意外的结果。
上下文被设计删除,以鼓励人们不要使用它。您应该尝试尽可能使用公开的属性,以避免以后出现兼容性问题。
作为一个示例,您可以从properties.Web.CurrentUser 中获取用户名。
在事件接收器中使用静态变量很棘手,并且您必须记住,如果您有多个前端,则静态变量中的数据在事件接收器实例运行的前端之外不可用。
If you place it in a static variable like that, you also have multiple people using the same context object that will be the context of the user who first ran the event receiver, and concurrent changes could have unexpected results.
The context is removed by design to encourage people not to use it. You should try to use the properties that are exposed as much as possible to avoid compatibility issues later.
You can get the username off the properties.Web.CurrentUser as one example.
Using static variables in the event receiver is tricky, and you have to remember if you have multiple front ends, the data in the static variable is not available outside the frontend that the instance of the event receiver runs on.