How to obtain the HttpContext in Event Handler

后端 未结 8 534
隐瞒了意图╮
隐瞒了意图╮ 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:25

    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.

    0 讨论(0)
  • 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)
        { }
    }
    
    0 讨论(0)
  • 2021-01-22 20:30

    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/

    0 讨论(0)
  • 2021-01-22 20:32

    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.

    0 讨论(0)
  • 2021-01-22 20:34

    Try using the HttpRuntime class

    0 讨论(0)
  • 2021-01-22 20:40

    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 )

    0 讨论(0)
提交回复
热议问题