views:

897

answers:

5

Hello,

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; 
    }
}
+2  A: 

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

dahlbyk
The ItemAdding method and all *ing methods in an event receiver are synchronous, and you can catch there the context of the HTTP request. In fact, when you test the code in an event receiver for a list, the HttpContext is returned, but when you tried with a document library the context is null.
Esteban Lalinde
A: 

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.

Esteban Lalinde
A: 

Try using the HttpRuntime class

Johan Leino
A: 

Hi All,

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 )

Mohammed Barakat
A: 

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