如何在Event Handler中获取HttpContext

发布于 2024-08-08 16:15:08 字数 672 浏览 13 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

爱已欠费 2024-08-15 16:15:08

步骤1
声明:

    private HttpContext currentContext;
    static HttpContext _stCurrentContext;

步骤 2

currentContext = HttpContext.Current;      // in constructor

步骤 3

public override void ItemAdding(SPItemEventProperties properties)
                 _stCurrentContext = currentContext;

步骤 4

 public override void ItemAdded(SPItemEventProperties properties)
 if (_stCurrentContext.Request.Files[0].ContentLength > 0)
 HttpPostedFile uploadfile = _stCurrentContext.Request.Files[0];

Step 1
Declare:

    private HttpContext currentContext;
    static HttpContext _stCurrentContext;

Step 2

currentContext = HttpContext.Current;      // in constructor

Step3

public override void ItemAdding(SPItemEventProperties properties)
                 _stCurrentContext = currentContext;

Step 4

 public override void ItemAdded(SPItemEventProperties properties)
 if (_stCurrentContext.Request.Files[0].ContentLength > 0)
 HttpPostedFile uploadfile = _stCurrentContext.Request.Files[0];
仙气飘飘 2024-08-15 16:15:08

当我在上传新文档时尝试更新文档库的某些自定义字段时,我遇到了同样的问题,该字段是 (ProjectID),我将其放入 Web 部件的会话中(上传文档之前的步骤)。

我所做的是:我将 projectID 放入自定义 Web 部件内的缓存(每个用户)中,该部件充当会话,如下所示:

if (Request.QueryString["ProjectID"] != null)
{
     HttpRuntime.Cache.Remove(SPContext.Current.Web.CurrentUser.LoginName);
     HttpRuntime.Cache.Add(SPContext.Current.Web.CurrentUser.LoginName,
                           ProjectID, null, DateTime.UtcNow.AddMinutes(60),
                           System.Web.Caching.Cache.NoSlidingExpiration,
                           System.Web.Caching.CacheItemPriority.Normal, null);
}

然后我实现了 ItemAdded 事件,然后我通过以下方式获取缓存的 projectId 值:

public override void ItemAdded(SPItemEventProperties properties)
{
    try
    {
        string ProjID = "";

        string CreatedBy = null;
        if (properties.ListItem["Created By"] != null)
            CreatedBy = properties.ListItem["Created By"].ToString().Split(';')[1].Replace("#","");

        if (HttpRuntime.Cache[CreatedBy] != null)
        {  
            //SPContext.Current.Web.CurrentUser.LoginName;
            ProjID = HttpRuntime.Cache[CreatedBy].ToString();

            if (properties.ListItem["Project"] == null)
            {
                properties.ListItem["Project"] = new SPFieldLookupValue(ProjID);
                properties.ListItem.SystemUpdate();
            }

            base.ItemAdded(properties);
        }
    }
    catch (Exception ex)
    { }
}

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:

if (Request.QueryString["ProjectID"] != null)
{
     HttpRuntime.Cache.Remove(SPContext.Current.Web.CurrentUser.LoginName);
     HttpRuntime.Cache.Add(SPContext.Current.Web.CurrentUser.LoginName,
                           ProjectID, null, DateTime.UtcNow.AddMinutes(60),
                           System.Web.Caching.Cache.NoSlidingExpiration,
                           System.Web.Caching.CacheItemPriority.Normal, null);
}

Then I implemented the ItemAdded event and I get the value of the cached projectId through:

public override void ItemAdded(SPItemEventProperties properties)
{
    try
    {
        string ProjID = "";

        string CreatedBy = null;
        if (properties.ListItem["Created By"] != null)
            CreatedBy = properties.ListItem["Created By"].ToString().Split(';')[1].Replace("#","");

        if (HttpRuntime.Cache[CreatedBy] != null)
        {  
            //SPContext.Current.Web.CurrentUser.LoginName;
            ProjID = HttpRuntime.Cache[CreatedBy].ToString();

            if (properties.ListItem["Project"] == null)
            {
                properties.ListItem["Project"] = new SPFieldLookupValue(ProjID);
                properties.ListItem.SystemUpdate();
            }

            base.ItemAdded(properties);
        }
    }
    catch (Exception ex)
    { }
}
提笔书几行 2024-08-15 16:15:08

项目事件接收器异步运行;您将无权访问发起该事件的 HTTP 请求。

An item event receiver run asynchronously; you will not have access to the HTTP request that initiated the event.

违心° 2024-08-15 16:15:08

如果您从 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.

我的奇迹 2024-08-15 16:15:08

尝试使用 HttpRuntime 类

Try using the HttpRuntime class

耶耶耶 2024-08-15 16:15:08

如果用户尝试上传一个文档,我可以从 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 )

南风几经秋 2024-08-15 16:15:08

您可以在事件接收器中伪造 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/

栀子花开つ 2024-08-15 16:15:08

如果将其放置在这样的静态变量中,则还会有多个人使用相同的上下文对象,该对象将成为首次运行事件接收器的用户的上下文,并且并发更改可能会产生意外的结果。

上下文被设计删除,以鼓励人们不要使用它。您应该尝试尽可能使用公开的属性,以避免以后出现兼容性问题。
作为一个示例,您可以从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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文