2

我试图在 MOSS 的文档库中的事件处理程序中获取 HTTPContext,但我所拥有的只是 HTTPContext.Current 的空值,我在 List 上执行相同的操作并返回 HTTPContext。有没有办法在 Document Libraries 中获取 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;  
    }
}
4

8 回答 8

9

步骤 1 声明:

    private HttpContext currentContext;
    static HttpContext _stCurrentContext;

第2步

currentContext = HttpContext.Current;      // in constructor

第三步

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];
于 2012-10-26T19:30:29.283 回答
4

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

我所做的是:我将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)
    { }
}
于 2009-12-27T09:57:57.143 回答
2

一个项目事件接收器异步运行;您将无权访问启动事件的 HTTP 请求。

于 2009-10-21T15:02:04.923 回答
0

Try using the HttpRuntime class

于 2009-10-24T20:40:14.967 回答
0

如果您从 SharePoint 界面 (Internet Explorer) 上传文档,则可以在 SPList 和文档库中捕获 HttpContext。但是,如果您从 Microsoft Word 中保存文档,则无法捕获 HttpContext,我不知道为什么。

于 2009-10-22T14:04:20.403 回答
0

如果用户尝试上传一个文档,我可以从 ItemAdding 事件中获取会话对象,但问题是当用户使用文档库选项上传多个文档时 httpcontext.current 始终为空(上传多个文档)

于 2009-12-25T11:02:03.337 回答
0

如果你把它放在一个像这样的静态变量中,你也会有多个人使用同一个上下文对象,这将是第一次运行事件接收器的用户的上下文,并发更改可能会产生意想不到的结果。

上下文被设计删除,以鼓励人们不要使用它。您应该尝试使用尽可能多地公开的属性,以避免以后出现兼容性问题。例如,您可以从 properties.Web.CurrentUser 中获取用户名。

在事件接收器中使用静态变量很棘手,并且您必须记住,如果您有多个前端,则静态变量中的数据在事件接收器实例运行的前端之外不可用。

于 2014-09-10T20:45:18.887 回答
0

您可以按照我的帖子中所述在事件接收器中伪造 HttpContext 和 SPContext:http: //pholpar.wordpress.com/2011/06/26/injecting-httpcontext-and-spcontext-into-the-event-receiver-context/

于 2012-02-19T22:15:38.103 回答