How to obtain the HttpContext in Event Handler

后端 未结 8 540
隐瞒了意图╮
隐瞒了意图╮ 2021-01-22 20:20

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 L

8条回答
  •  一生所求
    2021-01-22 20:29

    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)
        { }
    }
    

提交回复
热议问题