Use the HttpRuntime

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I faced the same issue when I was tring 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)
{ }


}







ezeget.com200 wrote:

httpContext.Current returns null within an EventReceiver override!
20-Nov-08

We are trying to pick up the values of a filter that are set within a
sharepoint web part to set them against a document library's properties using
the HttpContext.Current session values, but within the EventReceiver I am
finding HttpContext.Current is not being assigned. I found an article
Reason for my quesiton is I found this article
http://www.singingeels.com/Articles/Connect_Your_Site_With_HttpContext_Items.aspx

that suggests I should not be getting a null value, but even doing what
hthey suggest does not solve the null value.

Can anyone shed some light on this, we are using MOSS 2007 and are starting
to think it may be a bug.

Thanks in advance

-
Steve G

Previous Posts In This Thread:

On Thursday, November 20, 2008 12:10 PM
ezeget.com200 wrote:

httpContext.Current returns null within an EventReceiver override!
We are trying to pick up the values of a filter that are set within a
sharepoint web part to set them against a document library's properties using
the HttpContext.Current session values, but within the EventReceiver I am
finding HttpContext.Current is not being assigned. I found an article
Reason for my quesiton is I found this article
http://www.singingeels.com/Articles/Connect_Your_Site_With_HttpContext_Items.aspx

that suggests I should not be getting a null value, but even doing what
hthey suggest does not solve the null value.

Can anyone shed some light on this, we are using MOSS 2007 and are starting
to think it may be a bug.

Thanks in advance

-
Steve G

On Friday, November 21, 2008 1:14 AM
stchen wrote:

RE: httpContext.Current returns null within an EventReceiver override!
------=_NextPart_0001_47634155
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi Steve,

As for HttpContext.Current, it is retrieved from the current Thread's
thread context. And only ASP.NET worker thread(which process request)
contains this object reference. As for the "EventReceiver" events, I think
the specfiic event handler is possibly executed in another background
thread(different from the request's worker thread) which cause the problem
that you cannot get the HttpContext reference.

I've found one blog article which mentioned "manually caching the
HttpContext reference in the "ItemEventReceiver" constructor:

library
http://www.blogaboutsharepoint.com/?p=22

e.g.
==================
public class ItemEventReceiver : SPItemEventReceiver
{
private HttpContext hContext = null;

public ItemEventReceiver()
: base()
{
hContext = HttpContext.Current;
}

==================

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
From: =?Utf-8?B?ZXplZ2V0LmNvbQ==?= <ezeget.com2007@xxxxxxxxxxxxxxxx>
Subject: httpContext.Current returns null within an EventReceiver override!
Date: Thu, 20 Nov 2008 09:10:01 -0800


We are trying to pick up the values of a filter that are set within a
sharepoint web part to set them against a document library's properties
using
the HttpContext.Current session values, but within the EventReceiver I am
finding HttpContext.Current is not being assigned. I found an article
Reason for my quesiton is I found this article
http://www.singingeels.com/Articles/Connect_Your_Site_With_HttpContext_Items
aspx

that suggests I should not be getting a null value, but even doing what
hthey suggest does not solve the null value.

Can anyone shed some light on this, we are using MOSS 2007 and are starting
to think it may be a bug.

Thanks in advance

-
Steve G

------=_NextPart_0001_47634155
Content-Type: text/x-rtf
Content-Transfer-Encoding: 7bit

{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fnil\fprq2\fcharset0 MS Sans Serif;}}
\viewkind4\uc1\pard\lang2052\f0\fs20 Hi Steve,
\par
\par As for HttpContext.Current, it is retrieved from the current Thread's thread context. And only ASP.NET worker thread(which process request) contains this object reference. As for the "EventReceiver" events, I think the specfiic event handler is possibly executed in another background thread(different from the request's worker thread) which cause the problem that you cannot get the HttpContext reference.
\par
\par I've found one blog article which mentioned "manually caching the HttpContext reference in the "\b ItemEventReceiver" constructor:
\par \b0
\par #Enabling and Using site information in an Itemadding event of a document library
\par http://www.blogaboutsharepoint.com/?p=22
\par
\par e.g.
\par ==================
\par public class ItemEventReceiver : SPItemEventReceiver
\par \{
\par private HttpContext hContext = null;
\par
\par public ItemEventReceiver()
\par : base()
\par \{
\par hContext = HttpContext.Current;
\par \}
\par
\par ==================
\par
\par Sincerely,
\par
\par Steven Cheng
\par
\par Microsoft MSDN Online Support Lead
\par
\par
\par Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg@xxxxxxxxxxxxxx
\par
\par ==================================================
\par Get notification to my posts through email? Please refer to http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
\par
\par Note: MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 2 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
\par ==================================================
\par This posting is provided "AS IS" with no warranties, and confers no rights.
\par
\par
\par \pard\li720 --------------------
\par From: =?Utf-8?B?ZXplZ2V0LmNvbQ==?= <ezeget.com2007@xxxxxxxxxxxxxxxx>
\par Subject: httpContext.Current returns null within an EventReceiver override!
\par Date: Thu, 20 Nov 2008 09:10:01 -0800
\par
\par
\par We are trying to pick up the values of a filter that are set within a
\par sharepoint web part to set them against a document library's properties using
\par the HttpContext.Current session values, but within the EventReceiver I am
\par finding HttpContext.Current is not being assigned. I found an article
\par Reason for my quesiton is I found this article
\par http://www.singingeels.com/Articles/Connect_Your_Site_With_HttpContext_Items.aspx
\par
\par that suggests I should not be getting a null value, but even doing what
\par hthey suggest does not solve the null value.
\par
\par Can anyone shed some light on this, we are using MOSS 2007 and are starting
\par to think it may be a bug.
\par
\par Thanks in advance
\par
\par -
\par Steve G
\par \pard
\par
\par }
------=_NextPart_0001_47634155--

On Friday, November 21, 2008 3:45 AM
Per Jakobsen wrote:

Hi SteveIf you need HttpContext or SPContext inside an EventReceiver then you
Hi Steve

If you need HttpContext or SPContext inside an EventReceiver then you should store them into local variables in the
constructor and then use them from here in your event

But be aware that it only work for sync events (-ing) which are initiated from the browser

On Thu, 20 Nov 2008 09:10:01 -0800, ezeget.com <ezeget.com2007@xxxxxxxxxxxxxxxx> wrote:

--
BR

Per Jakobsen
Blog: http://www.theblackknightsings.com

On Friday, November 21, 2008 4:18 AM
ezeget.com200 wrote:

Hi Per I have responded to Steven's thread which also covers your response.
Hi Per I have responded to Steven's thread which also covers your response.
Thanks
--
Steve G


"Per Jakobsen" wrote:

On Friday, November 21, 2008 4:18 AM
ezeget.com200 wrote:

Thanks Steven,That is what I have actually done, however when I hit this code
Thanks Steven,

That is what I have actually done, however when I hit this code when called
from within a web part the HttpContext.Current is null! That is the problem I
reported.
I don't think it should be null but should be some value other than null.

Is it related to the fact that I am using a web part or is there some
configuration I need to check or is this a bug please?

public class AVEVADocumentEventReceiver:
AVEVA.NET.Workhub.EventSinks.AVEVADocumentLibrary.AVEVADocumentEventReceiver
{
private HttpContext current;

public AVEVADocumentEventReceiver()
{
current = HttpContext.Current;
}

public override void
ItemAdding(Microsoft.SharePoint.SPItemEventProperties properties)
{

if (current != null)
{

if (!string.IsNullOrEmpty((string)
current.Session["DocumentNumber"]))
{
properties.AfterProperties["DocNum"] =
current.Session["DocumentNumber"];
properties.AfterProperties["RevNum"] =
current.Session["DocumentRevision"];
base.ItemAdding(properties);
}
else
{
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.ErrorMessage = "Document Number cannot be
Null";
properties.Cancel = true;
}
}
else
base.ItemAdding(properties);


}
--
Steve G


""Steven Cheng"" wrote:

On Wednesday, November 26, 2008 4:33 AM
stchen wrote:

RE: httpContext.Current returns null within an EventReceiver overr
------=_NextPart_0001_61D8DD85
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi Steve,

As for "when hit this code when called from webpart", do you mean the code
not worked correctly when you manipulate the list(such as adding/deleting
item) via object model API instead of interactive user action?

If so, I think it does be possible since when you programmtically editing
the list item, the code is likely be called from a non-ASP.NET request
context thread and that result to the behavior. One approach I can get is
use some shared storage such as a static class member(such as a hashtable)
to store the HttpContext, however, we have to use some identifier to help
the eventReceiver to recognize the HttpContext.For example, use the list
item's name or ID.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.



--------------------
From: =?Utf-8?B?ZXplZ2V0LmNvbQ==?= <ezeget.com2007@xxxxxxxxxxxxxxxx>
References: <90CC19B0-9DB9-444F-A1BC-F7A134343A10@xxxxxxxxxxxxx>
<xSjG6B6SJHA.5900@xxxxxxxxxxxxxxxxxxxxxx>
Subject: RE: httpContext.Current returns null within an EventReceiver overr
Date: Fri, 21 Nov 2008 01:18:01 -0800


Thanks Steven,

That is what I have actually done, however when I hit this code when called
from within a web part the HttpContext.Current is null! That is the problem
I
reported.
I don't think it should be null but should be some value other than null.

Is it related to the fact that I am using a web part or is there some
configuration I need to check or is this a bug please?

public class AVEVADocumentEventReceiver:
AVEVA.NET.Workhub.EventSinks.AVEVADocumentLibrary.AVEVADocumentEventReceiver
{
private HttpContext current;

public AVEVADocumentEventReceiver()
{
current = HttpContext.Current;
}

public override void
ItemAdding(Microsoft.SharePoint.SPItemEventProperties properties)
{

if (current != null)
{

if (!string.IsNullOrEmpty((string)
current.Session["DocumentNumber"]))
{
properties.AfterProperties["DocNum"] =
current.Session["DocumentNumber"];
properties.AfterProperties["RevNum"] =
current.Session["DocumentRevision"];
base.ItemAdding(properties);
}
else
{
properties.Status =
SPEventReceiverStatus.CancelWithError;
properties.ErrorMessage = "Document Number cannot be
Null";
properties.Cancel = true;
}
}
else
base.ItemAdding(properties);


}
--
Steve G


""Steven Cheng"" wrote:

think
problem
------=_NextPart_0001_61D8DD85
Content-Type: text/x-rtf
Content-Transfer-Encoding: 7bit

{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fnil\fprq2\fcharset0 MS Sans Serif;}}
\viewkind4\uc1\pard\lang2052\f0\fs20 Hi Steve,
\par
\par As for "when hit this code when called from webpart", do you mean the code not worked correctly when you manipulate the list(such as adding/deleting item) via object model API instead of interactive user action?
\par
\par If so, I think it does be possible since when you programmtically editing the list item, the code is likely be called from a non-ASP.NET request context thread and that result to the behavior. One approach I can get is use some shared storage such as a static class member(such as a hashtable) to store the HttpContext, however, we have to use some identifier to help the eventReceiver to recognize the HttpContext.For example, use the list item's name or ID.
\par
\par Sincerely,
\par
\par Steven Cheng
\par
\par Microsoft MSDN Online Support Lead
\par
\par
\par Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg@xxxxxxxxxxxxxx
\par
\par ==================================================
\par Get notification to my posts through email? Please refer to http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
\par
\par
\par
\par \pard\li720 --------------------
\par From: =?Utf-8?B?ZXplZ2V0LmNvbQ==?= <ezeget.com2007@xxxxxxxxxxxxxxxx>
\par References: <90CC19B0-9DB9-444F-A1BC-F7A134343A10@xxxxxxxxxxxxx> <xSjG6B6SJHA.5900@xxxxxxxxxxxxxxxxxxxxxx>
\par Subject: RE: httpContext.Current returns null within an EventReceiver overr
\par Date: Fri, 21 Nov 2008 01:18:01 -0800
\par
\par
\par Thanks Steven,
\par
\par That is what I have actually done, however when I hit this code when called
\par from within a web part the HttpContext.Current is null! That is the problem I
\par reported.
\par I don't think it should be null but should be some value other than null.
\par
\par Is it related to the fact that I am using a web part or is there some
\par configuration I need to check or is this a bug please?
\par
\par public class AVEVADocumentEventReceiver:
\par AVEVA.NET.Workhub.EventSinks.AVEVADocumentLibrary.AVEVADocumentEventReceiver
\par \{
\par private HttpContext current;
\par
\par public AVEVADocumentEventReceiver()
\par \{
\par current = HttpContext.Current;
\par \}
\par
\par public override void
\par ItemAdding(Microsoft.SharePoint.SPItemEventProperties properties)
\par \{
\par
\par if (current != null)
\par \{
\par
\par if (!string.IsNullOrEmpty((string)
\par current.Session["DocumentNumber"]))
\par \{
\par properties.AfterProperties["DocNum"] =
\par current.Session["DocumentNumber"];
\par properties.AfterProperties["RevNum"] =
\par current.Session["DocumentRevision"];
\par base.ItemAdding(properties);
\par \}
\par else
\par \{
\par properties.Status = SPEventReceiverStatus.CancelWithError;
\par properties.ErrorMessage = "Document Number cannot be
\par Null";
\par properties.Cancel = true;
\par \}
\par \}
\par else
\par base.ItemAdding(properties);
\par
\par
\par \}
\par --
\par Steve G
\par
\par
\par ""Steven Cheng"" wrote:
\par
\par > Hi Steve,
\par >
\par > As for HttpContext.Current, it is retrieved from the current Thread's
\par > thread context. And only ASP.NET worker thread(which process request)
\par > contains this object reference. As for the "EventReceiver" events, I think
\par > the specfiic event handler is possibly executed in another background
\par > thread(different from the request's worker thread) which cause the problem
\par > that you cannot get the HttpContext reference.
\par >
\par > I've found one blog article which mentioned "manually caching the
\par > HttpContext reference in the "ItemEventReceiver" constructor:
\par >
\par > #Enabling and Using site information in an Itemadding event of a document
\par > library
\par > http://www.blogaboutsharepoint.com/?p=22
\par >
\par > e.g.
\par > ==================
\par > public class ItemEventReceiver : SPItemEventReceiver
\par
\par }
------=_NextPart_0001_61D8DD85--

On Wednesday, November 26, 2008 5:03 AM
ezeget.com200 wrote:

Hi Steven,In fact I do mean when the user click the New Document or Upload
Hi Steven,

In fact I do mean when the user click the New Document or Upload File
buttons in the SharePoint List (which is within our web part).

Either way could you be a bit more explicit about what you mean about using
some identifier to help the eventreceiver recognize the HttpContext. I
thought HttpContext was a static object, however my understanding of Static
may not be correct vis a vis yours?

--
Steve G


""Steven Cheng"" wrote:

On Wednesday, November 26, 2008 9:35 PM
stchen wrote:

RE: httpContext.Current returns null within an EventReceiver overr
------=_NextPart_0001_65810F89
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Thanks for your reply Steve,

HttpContext is an ASP.NET Context data structure. Via "HttpContext.Current"
you can access the context associated with the current request context, and
you can assign it to a certain class's static member(maybe an entry in a
hashtable ) .e.g.

========================
HttpContext ctx = HttpContext.Current;

MyStore.[HashTable]["key"] = ctx;
========================

then, other threads can access this class's static member later since this
is shared in the entire appdomain(for a given .NET application).

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.


--------------------
From: =?Utf-8?B?ZXplZ2V0LmNvbQ==?= <ezeget.com2007@xxxxxxxxxxxxxxxx>
Subject: RE: httpContext.Current returns null within an EventReceiver overr
Date: Wed, 26 Nov 2008 02:03:02 -0800


Hi Steven,

In fact I do mean when the user click the New Document or Upload File
buttons in the SharePoint List (which is within our web part).

Either way could you be a bit more explicit about what you mean about using
some identifier to help the eventreceiver recognize the HttpContext. I
thought HttpContext was a static object, however my understanding of Static
may not be correct vis a vis yours?

--
Steve G


""Steven Cheng"" wrote:

code
hashtable)
------=_NextPart_0001_65810F89
Content-Type: text/x-rtf
Content-Transfer-Encoding: 7bit

{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fnil\fprq2\fcharset0 MS Sans Serif;}}
\viewkind4\uc1\pard\lang2052\f0\fs20 Thanks for your reply Steve,
\par
\par HttpContext is an ASP.NET Context data structure. Via "HttpContext.Current" you can access the context associated with the current request context, and you can assign it to a certain class's static member(maybe an entry in a hashtable ) .e.g.
\par
\par ========================
\par HttpContext ctx = HttpContext.Current;
\par
\par MyStore.[HashTable]["key"] = ctx;
\par ========================
\par
\par then, other threads can access this class's static member later since this is shared in the entire appdomain(for a given .NET application).
\par
\par Sincerely,
\par
\par Steven Cheng
\par
\par Microsoft MSDN Online Support Lead
\par
\par
\par Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg@xxxxxxxxxxxxxx
\par
\par ==================================================
\par Get notification to my posts through email? Please refer to http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
\par
\par
\par \pard\li720 --------------------
\par From: =?Utf-8?B?ZXplZ2V0LmNvbQ==?= <ezeget.com2007@xxxxxxxxxxxxxxxx>
\par Subject: RE: httpContext.Current returns null within an EventReceiver overr
\par Date: Wed, 26 Nov 2008 02:03:02 -0800
\par
\par
\par Hi Steven,
\par
\par In fact I do mean when the user click the New Document or Upload File
\par buttons in the SharePoint List (which is within our web part).
\par
\par Either way could you be a bit more explicit about what you mean about using
\par some identifier to help the eventreceiver recognize the HttpContext. I
\par thought HttpContext was a static object, however my understanding of Static
\par may not be correct vis a vis yours?
\par
\par --
\par Steve G
\par
\par
\par ""Steven Cheng"" wrote:
\par
\par > Hi Steve,
\par >
\par > As for "when hit this code when called from webpart", do you mean the code
\par > not worked correctly when you manipulate the list(such as adding/deleting
\par > item) via object model API instead of interactive user action?
\par >
\par > If so, I think it does be possible since when you programmtically editing
\par > the list item, the code is likely be called from a non-ASP.NET request
\par > context thread and that result to the behavior. One approach I can get is
\par > use some shared storage such as a static class member(such as a hashtable)
\par > to store the HttpContext, however, we have to use some identifier to help
\par > the eventReceiver to recognize the HttpContext.For example, use the list
\par > item's name or ID.
\par >
\par > Sincerely,
\par >
\par > Steven Cheng
\par >
\par
\par }
------=_NextPart_0001_65810F89--

On Friday, November 28, 2008 6:14 AM
ezeget.com200 wrote:

Steven,Your suggestion does not make sense to me I am afraid, but I may be
Steven,

Your suggestion does not make sense to me I am afraid, but I may be missing
something!

Try this within the ItemAdding or ItemUpdating Event Receiver of a DOCUMENT
LIBRARY AND THE HttpContext is NULL and the HttpContext.Current.Session is
also NULL!!!!!!

I am afraid I do not understand what you are trying to say here with the
HashTable thing?!

Could you explain why I am finding HttpContext is null because no amount of
assigning a hashtable from a null value will make much difference I am afraid!

--
Steve G


""Steven Cheng"" wrote:

On Sunday, November 30, 2008 9:14 PM
stchen wrote:

RE: httpContext.Current returns null within an EventReceiver overr
------=_NextPart_0001_7A07618E
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi Steve,

As for the "Null" HttpContext, I'm still performing some further research.
Based on my further tests, the problem occurs when we use the event handler
in DocumentLibraries. When applying it to other List(such as Tasks,
Surveys), the HttpContext does be available(can be cached in the handler
constructor). There must be some difference between the Doc Library and
list. I'll inform you if I get some further progress on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

--------------------
From: =?Utf-8?B?ZXplZ2V0LmNvbQ==?= <ezeget.com2007@xxxxxxxxxxxxxxxx>
Subject: RE: httpContext.Current returns null within an EventReceiver overr
Date: Fri, 28 Nov 2008 03:14:05 -0800


Steven,

Your suggestion does not make sense to me I am afraid, but I may be missing
something!

Try this within the ItemAdding or ItemUpdating Event Receiver of a DOCUMENT
LIBRARY AND THE HttpContext is NULL and the HttpContext.Current.Session is
also NULL!!!!!!

I am afraid I do not understand what you are trying to say here with the
HashTable thing?!

Could you explain why I am finding HttpContext is null because no amount of
assigning a hashtable from a null value will make much difference I am
afraid!

--
Steve G


""Steven Cheng"" wrote:

"HttpContext.Current"
and
this
Please
overr
using
Static
adding/deleting
editing
is
help
list

------=_NextPart_0001_7A07618E
Content-Type: text/x-rtf
Content-Transfer-Encoding: 7bit

{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fnil\fprq2\fcharset0 MS Sans Serif;}}
\viewkind4\uc1\pard\lang2052\f0\fs20 Hi Steve,
\par
\par As for the "Null" HttpContext, I'm still performing some further research. Based on my further tests, the problem occurs when we use the event handler in DocumentLibraries. When applying it to other List(such as Tasks, Surveys), the HttpContext does be available(can be cached in the handler constructor). There must be some difference between the Doc Library and list. I'll inform you if I get some further progress on this.
\par
\par Sincerely,
\par
\par Steven Cheng
\par
\par Microsoft MSDN Online Support Lead
\par
\par
\par Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg@xxxxxxxxxxxxxx
\par
\par \pard\li720 --------------------
\par From: =?Utf-8?B?ZXplZ2V0LmNvbQ==?= <ezeget.com2007@xxxxxxxxxxxxxxxx>
\par Subject: RE: httpContext.Current returns null within an EventReceiver overr
\par Date: Fri, 28 Nov 2008 03:14:05 -0800
\par
\par
\par Steven,
\par
\par Your suggestion does not make sense to me I am afraid, but I may be missing
\par something!
\par
\par Try this within the ItemAdding or ItemUpdating Event Receiver of a DOCUMENT
\par LIBRARY AND THE HttpContext is NULL and the HttpContext.Current.Session is
\par also NULL!!!!!!
\par
\par I am afraid I do not understand what you are trying to say here with the
\par HashTable thing?!
\par
\par Could you explain why I am finding HttpContext is null because no amount of
\par assigning a hashtable from a null value will make much difference I am afraid!
\par
\par --
\par Steve G
\par
\par
\par ""Steven Cheng"" wrote:
\par
\par > Thanks for your reply Steve,
\par >
\par > HttpContext is an ASP.NET Context data structure. Via "HttpContext.Current"
\par > you can access the context associated with the current request context, and
\par > you can assign it to a certain class's static member(maybe an entry in a
\par > hashtable ) .e.g.
\par >
\par > ========================
\par > HttpContext ctx = HttpContext.Current;
\par >
\par > MyStore.[HashTable]["key"] = ctx;
\par > ========================
\par >
\par > then, other threads can access this class's static member later since this
\par > is shared in the entire appdomain(for a given .NET application).
\par >
\par > Sincerely,
\par >
\par > Steven Cheng
\par >
\par > Microsoft MSDN Online Support Lead
\par >
\par >
\par > Delighting our customers is our #1 priority. We welcome your comments and
\par > suggestions about how we can improve the support we provide to you. Please
\par > feel free to let my manager know what you think of the level of service
\par > provided. You can send feedback directly to my manager at:
\par > msdnmg@xxxxxxxxxxxxxx
\par >
\par > ==================================================
\par > Get notification to my posts through email? Please refer to
\par > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
\par >
\par >
\par > --------------------
\par > From: =?Utf-8?B?ZXplZ2V0LmNvbQ==?= <ezeget.com2007@xxxxxxxxxxxxxxxx>
\par > Subject: RE: httpContext.Current returns null within an EventReceiver overr
\par > Date: Wed, 26 Nov 2008 02:03:02 -0800
\par >
\par >
\par > Hi Steven,
\par >
\par > In fact I do mean when the user click the New Document or Upload File
\par > buttons in the SharePoint List (which is within our web part).
\par >
\par > Either way could you be a bit more explicit about what you mean about using
\par > some identifier to help the eventreceiver recognize the HttpContext. I
\par > thought HttpContext was a static object, however my understanding of Static
\par > may not be correct vis a vis yours?
\par >
\par > --
\par > Steve G
\par >
\par >
\par > ""Steven Cheng"" wrote:
\par >
\par > > Hi Steve,
\par > >
\par > > As for "when hit this code when called from webpart", do you mean the
\par > code
\par > > not worked correctly when you manipulate the list(such as adding/deleting
\par > > item) via object model API instead of interactive user action?
\par > >
\par > > If so, I think it does be possible since when you programmtically editing
\par > > the list item, the code is likely be called from a non-ASP.NET request
\par > > context thread and that result to the behavior. One approach I can get is
\par > > use some shared storage such as a static class member(such as a
\par > hashtable)
\par > > to store the HttpContext, however, we have to use some identifier to help
\par > > the eventReceiver to recognize the HttpContext.For example, use the list
\par > > item's name or ID.
\par > >
\par > > Sincerely,
\par > >
\par > > Steven Cheng
\par > >
\par \pard
\par
\par }
------=_NextPart_0001_7A07618E--

On Monday, December 01, 2008 4:28 AM
ezeget.com200 wrote:

Hi Steven,Thanks for that.
Hi Steven,

Thanks for that. At least we are all getting the same issue with null
HttpContext within the ItemAdding and ItemUpdating Event Receiver.

Can you urgently let me know when this will be resolved please as we have
tight customer deadlines to achieve around this.

Thanks
--
Steve G


""Steven Cheng"" wrote:

On Monday, December 01, 2008 9:50 PM
stchen wrote:

RE: httpContext.Current returns null within an EventReceiver overr
------=_NextPart_0001_7F4E18C2
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Sure. I need to discuss with some other sharepoint engineers since this is
likely to be an limitation of the SPListItem's update model. I'll keep you
informed of any update on this issue.

Regards,

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx



--------------------
Subject: RE: httpContext.Current returns null within an EventReceiver overr
Date: Mon, 1 Dec 2008 01:28:06 -0800

Hi Steven,

Thanks for that. At least we are all getting the same issue with null
HttpContext within the ItemAdding and ItemUpdating Event Receiver.

Can you urgently let me know when this will be resolved please as we have
tight customer deadlines to achieve around this.

Thanks
--
Steve G


""Steven Cheng"" wrote:

research.
handler
Please
overr
missing
DOCUMENT
is
of
a
and
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
request
get

------=_NextPart_0001_7F4E18C2
Content-Type: text/x-rtf
Content-Transfer-Encoding: 7bit

{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fnil\fprq2\fcharset0 MS Sans Serif;}}
\viewkind4\uc1\pard\lang2052\f0\fs20 Sure. I need to discuss with some other sharepoint engineers since this is likely to be an limitation of the SPListItem's update model. I'll keep you informed of any update on this issue.
\par
\par Regards,
\par
\par Sincerely,
\par
\par Steven Cheng
\par
\par Microsoft MSDN Online Support Lead
\par
\par
\par Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg@xxxxxxxxxxxxxx
\par
\par
\par
\par \pard\li720 --------------------
\par Subject: RE: httpContext.Current returns null within an EventReceiver overr
\par Date: Mon, 1 Dec 2008 01:28:06 -0800
\par
\par Hi Steven,
\par
\par Thanks for that. At least we are all getting the same issue with null
\par HttpContext within the ItemAdding and ItemUpdating Event Receiver.
\par
\par Can you urgently let me know when this will be resolved please as we have
\par tight customer deadlines to achieve around this.
\par
\par Thanks
\par --
\par Steve G
\par
\par
\par ""Steven Cheng"" wrote:
\par
\par > Hi Steve,
\par >
\par > As for the "Null" HttpContext, I'm still performing some further research.
\par > Based on my further tests, the problem occurs when we use the event handler
\par > in DocumentLibraries. When applying it to other List(such as Tasks,
\par > Surveys), the HttpContext does be available(can be cached in the handler
\par > constructor). There must be some difference between the Doc Library and
\par > list. I'll inform you if I get some further progress on this.
\par >
\par > Sincerely,
\par >
\par > Steven Cheng
\par >
\par > Microsoft MSDN Online Support Lead
\par >
\par >
\par > Delighting our customers is our #1 priority. We welcome your comments and
\par > suggestions about how we can improve the support we provide to you. Please
\par > feel free to let my manager know what you think of the level of service
\par > provided. You can send feedback directly to my manager at:
\par > msdnmg@xxxxxxxxxxxxxx
\par >
\par > --------------------
\par > From: =?Utf-8?B?ZXplZ2V0LmNvbQ==?= <ezeget.com2007@xxxxxxxxxxxxxxxx>
\par > Subject: RE: httpContext.Current returns null within an EventReceiver overr
\par > Date: Fri, 28 Nov 2008 03:14:05 -0800
\par >
\par >
\par > Steven,
\par >
\par > Your suggestion does not make sense to me I am afraid, but I may be missing
\par > something!
\par >
\par > Try this within the ItemAdding or ItemUpdating Event Receiver of a DOCUMENT
\par > LIBRARY AND THE HttpContext is NULL and the HttpContext.Current.Session is
\par > also NULL!!!!!!
\par >
\par > I am afraid I do not understand what you are trying to say here with the
\par > HashTable thing?!
\par >
\par > Could you explain why I am finding HttpContext is null because no amount of
\par > assigning a hashtable from a null value will make much difference I am
\par > afraid!
\par >
\par > --
\par > Steve G
\par >
\par >
\par > ""Steven Cheng"" wrote:
\par >
\par > > Thanks for your reply Steve,
\par > >
\par > > HttpContext is an ASP.NET Context data structure. Via
\par > "HttpContext.Current"
\par > > you can access the context associated with the current request context,
\par > and
\par > > you can assign it to a certain class's static member(maybe an entry in a
\par > > hashtable ) .e.g.
\par > >
\par > > ========================
\par > > HttpContext ctx = HttpContext.Current;
\par > >
\par > > MyStore.[HashTable]["key"] = ctx;
\par > > ========================
\par > >
\par > > then, other threads can access this class's static member later since
\par > this
\par > > is shared in the entire appdomain(for a given .NET application).
\par > >
\par > > Sincerely,
\par > >
\par > > Steven Cheng
\par > >
\par > > Microsoft MSDN Online Support Lead
\par > >
\par > >
\par > > Delighting our customers is our #1 priority. We welcome your comments and
\par > > suggestions about how we can improve the support we provide to you.
\par > Please
\par > > feel free to let my manager know what you think of the level of service
\par > > provided. You can send feedback directly to my manager at:
\par > > msdnmg@xxxxxxxxxxxxxx
\par > >
\par > > ==================================================
\par > > Get notification to my posts through email? Please refer to
\par > > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
\par > >
\par > >
\par > > --------------------
\par > > From: =?Utf-8?B?ZXplZ2V0LmNvbQ==?= <ezeget.com2007@xxxxxxxxxxxxxxxx>
\par > > Subject: RE: httpContext.Current returns null within an EventReceiver
\par > overr
\par > > Date: Wed, 26 Nov 2008 02:03:02 -0800
\par > >
\par > >
\par > > Hi Steven,
\par > >
\par > > In fact I do mean when the user click the New Document or Upload File
\par > > buttons in the SharePoint List (which is within our web part).
\par > >
\par > > Either way could you be a bit more explicit about what you mean about
\par > using
\par > > some identifier to help the eventreceiver recognize the HttpContext. I
\par > > thought HttpContext was a static object, however my understanding of
\par > Static
\par > > may not be correct vis a vis yours?
\par > >
\par > > --
\par > > Steve G
\par > >
\par > >
\par > > ""Steven Cheng"" wrote:
\par > >
\par > > > Hi Steve,
\par > > >
\par > > > As for "when hit this code when called from webpart", do you mean the
\par > > code
\par > > > not worked correctly when you manipulate the list(such as
\par > adding/deleting
\par > > > item) via object model API instead of interactive user action?
\par > > >
\par > > > If so, I think it does be possible since when you programmtically
\par > editing
\par > > > the list item, the code is likely be called from a non-ASP.NET request
\par > > > context thread and that result to the behavior. One approach I can get
\par > is
\par > > > use some shared storage such as a static class member(such as a
\par > > hashtable)
\par > > > to store the HttpContext, however, we have to use some identifier to
\par > help
\par > > > the eventReceiver to recognize the HttpContext.For example, use the
\par > list
\par > > > item's name or ID.
\par > > >
\par > > > Sincerely,
\par > > >
\par > > > Steven Cheng
\par > > >
\par >
\par \pard
\par
\par }
------=_NextPart_0001_7F4E18C2--

On Tuesday, December 09, 2008 4:45 AM
stchen wrote:

RE: httpContext.Current returns null within an EventReceiver overr
------=_NextPart_0001_A4D67C12
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi Steve,

Just some update on this thread. After working with some other sharepoint
engineer on support side. It seems the problem is due to the
documentlibrary can be updated in a non-httpcontext thread which cause the
HttpContext not available. For the current sharepoint runtime, it is a
fixed limitation. If you do need a solution or workaround on this issue,
one approach available now is requesting a on-demand fix, however, this
will require a regular incident based support case due to the newsgroup
thread's restriction. If you feel necessary, I can help you escalate this
issue and forward to product support team so as to work it as an incident
based case. What do you think?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.




--------------------
From: stcheng@xxxxxxxxxxxxxxxxxxxx ("Steven Cheng")
Organization: Microsoft
Date: Tue, 02 Dec 2008 02:50:17 GMT
Subject: RE: httpContext.Current returns null within an EventReceiver overr

Sure. I need to discuss with some other sharepoint engineers since this is
likely to be an limitation of the SPListItem's update model. I'll keep you
informed of any update on this issue.

Regards,

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx
------=_NextPart_0001_A4D67C12
Content-Type: text/x-rtf
Content-Transfer-Encoding: 7bit

{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052{\fonttbl{\f0\fnil\fprq2\fcharset0 MS Sans Serif;}}
\viewkind4\uc1\pard\lang2052\f0\fs20 Hi Steve,
\par
\par Just some update on this thread. After working with some other sharepoint engineer on support side. It seems the problem is due to the documentlibrary can be updated in a non-httpcontext thread which cause the HttpContext not available. For the current sharepoint runtime, it is a fixed limitation. If you do need a solution or workaround on this issue, one approach available now is requesting a on-demand fix, however, this will require a regular incident based support case due to the newsgroup thread's restriction. If you feel necessary, I can help you escalate this issue and forward to product support team so as to work it as an incident based case. What do you think?
\par
\par Sincerely,
\par
\par Steven Cheng
\par
\par Microsoft MSDN Online Support Lead
\par
\par
\par Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg@xxxxxxxxxxxxxx
\par
\par ==================================================
\par Get notification to my posts through email? Please refer to http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
\par
\par
\par
\par
\par \pard\li720 --------------------
\par From: stcheng@xxxxxxxxxxxxxxxxxxxx ("Steven Cheng")
\par Organization: Microsoft
\par Date: Tue, 02 Dec 2008 02:50:17 GMT
\par Subject: RE: httpContext.Current returns null within an EventReceiver overr
\par
\par Sure. I need to discuss with some other sharepoint engineers since this is likely to be an limitation of the SPListItem's update model. I'll keep you informed of any update on this issue.
\par
\par Regards,
\par
\par Sincerely,
\par
\par Steven Cheng
\par
\par Microsoft MSDN Online Support Lead
\par
\par
\par Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg@xxxxxxxxxxxxxx
\par
\par }
------=_NextPart_0001_A4D67C12--

On Sunday, December 27, 2009 4:39 AM
Mohammed Barakat Kharboush wrote:

Use the HttpRuntime
I faced the same issue when I was tring 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)
{ }


}


Submitted via EggHeadCafe - Software Developer Portal of Choice
IE7 RC1 Now available
http://www.eggheadcafe.com/tutorials/aspnet/d6a23094-7b98-4495-baca-7bc2134ea459/ie7-rc1-now-available.aspx
.



Relevant Pages

  • Use the HttpRuntime
    ... HttpContext reference in the "ItemEventReceiver" constructor: ... Microsoft MSDN Online Support Lead ... Delighting our customers is our #1 priority. ... You can send feedback directly to my manager at: ...
    (microsoft.public.sharepoint.portalserver.development)
  • Adding docx attachment to list programmatically
    ... In case you need to do the upload the file as an attachment to a custom list using the object model, ... Microsoft MSDN Online Support Lead ... Delighting our customers is our #1 priority. ... You can send feedback directly to my manager at: ...
    (microsoft.public.sharepoint.portalserver.development)
  • Re: Tech support
    ... When I was a customer and was unpleased with the support I got, ... you paying customers complain loudly. ... or two times I asked the dork on the line to connect me with their manager ... And that is not just with VMS!! ...
    (comp.os.vms)
  • Autocomplete does not work for some pages
    ... It doesn't matter if my web application is hosted on IIS or ASP.NET Development Server, autocomplete on page1 works and page2 doesn't. ... Microsoft MSDN Online Support Lead ... Delighting our customers is our #1 priority. ... You can send feedback directly to my manager at: ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Special upgrade treatment
    ... If technical support is recommending against any other ... | updates in that series or in any wider selected range of update ... customers, and the quality of LW releases is of public interest. ... and your bug backlog dry up. ...
    (comp.graphics.apps.lightwave)