Max Andrade

I'm trying to add an ItemAdding event handler to a list, but it seems there is a bug in WSS when dealing with anonymous users. I'm using WSS 3.0, and I did the folowing:

a. Created a new class inheriting from "SPItemEventReceiver" and overriding to the "ItemAdding" method;
b. Published the new assembly to the GAC;
c. Registered a new event handler to the list (for example, announcements);
d. Enabled anonymous access to the web application;
e. Enabled anonymous users to add new items to the list;

Now, when I'm logged to the site with a valid user, my event handler works fine, but when I try to add a new item anonymously, I get the following error (I have more than 1GB of free RAM in my system during the operation):

p.s.: It doesn't matter the code is inside the ItemAdding method, it can be even blank that we still get this error.

Server Out Of Memory

There is no memory on the server to run your program. Please contact your administrator with this problem.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.OutOfMemoryException: Server Out Of Memory

There is no memory on the server to run your program. Please contact your administrator with this problem.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[OutOfMemoryException: Server Out Of Memory

There is no memory on the server to run your program. Please contact your administrator with this problem.]
Microsoft.SharePoint.Library.SPRequestInternalClass.AddOrUpdateItem(String bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPreserveItemVersion, Boolean bUpdateNoVersion, Int32& plID, String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion, Object& pvarAttachmentNames, Object& pvarAttachmentContents, Object& pvarProperties, Boolean bCheckOut, Boolean bCheckin, Boolean bMigration, Boolean bPublish) +0
Microsoft.SharePoint.Library.SPRequest.AddOrUpdateItem(String bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPreserveItemVersion, Boolean bUpdateNoVersion, Int32& plID, String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion, Object& pvarAttachmentNames, Object& pvarAttachmentContents, Object& pvarProperties, Boolean bCheckOut, Boolean bCheckin, Boolean bMigration, Boolean bPublish) +199
Microsoft.SharePoint.SPListItem.AddOrUpdateItem(Boolean bAdd, Boolean bSystem, Boolean bPreserveItemVersion, Boolean bNoVersion, Boolean bMigration, Boolean bPublish, Boolean bCheckOut, Boolean bCheckin, Guid newGuidOnAdd, Int32& ulID, Object& objAttachmentNames, Object& objAttachmentContents, Boolean suppressAfterEvents) +746
Microsoft.SharePoint.SPListItem.UpdateInternal(Boolean bSystem, Boolean bPreserveItemVersion, Guid newGuidOnAdd, Boolean bMigration, Boolean bPublish, Boolean bNoVersion, Boolean bCheckOut, Boolean bCheckin, Boolean suppressAfterEvents) +166
Microsoft.SharePoint.SPListItem.Update() +89
Microsoft.SharePoint.WebControls.SaveButton.SaveItem(SPContext itemContext, Boolean uploadMode, String checkInComment) +246
Microsoft.SharePoint.WebControls.SaveButton.SaveItem() +58
Microsoft.SharePoint.WebControls.SaveButton.OnBubbleEvent(Object source, EventArgs e) +249
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +35
System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +115
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +163
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +174
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102




Re: SharePoint - Development and Programming List event handlers X anonymous access

TheKid

Not sure if this will help, but a look at this blog may point you to something...

http://blogs.msdn.com/tess/archive/2005/11/25/496898.aspx






Re: SharePoint - Development and Programming List event handlers X anonymous access

Max Andrade

Thanks TheKid, but I believe this is not my case. My w3wp.exe is using only 80MB out of my 2GB system memory (and I have about 1.2GB of free memory). It seems to me that this exception is hiding some other problem, but I have no idea what it would be. As far as I can see, the problem is inside WSS's code. I say that because my event handler don't get even loaded during the operation.

Regards,

Max





Re: SharePoint - Development and Programming List event handlers X anonymous access

Olivier RICHARD

Hi Max,
I have exactly the same problem than you. I don't believe it's a lack of memory, because if I un-link the eventhandler, it would work.

As soon as I add an eventhandler to the list (even an empty one, with a minimum of code), it will crash.

Regards,





Re: SharePoint - Development and Programming List event handlers X anonymous access

Steven Tapping

Hi guys,

I have this exact problem. Initially I thought it was an ASP.NET issue and followed this article : http://support.microsoft.com/kb/820108. Basically if this was an ASP.NET issue you could either increase the virtual memory of the process, or make the app pool use multiple processes (web garden).

Unfortunatly, neither solves this SharePoint specific issue...

if anyone finds a hotfix or a workaround, please post

.

Steven.





Re: SharePoint - Development and Programming List event handlers X anonymous access

Steven Tapping

I just confirmed this morning the issue is with event handlers.

In my case, we had an anonymous site (Blog template to be specific) on which a list submission was causing this issue.

We added a workflow on a list people could submit to anonymously. We tested it and got this OutOfMemory exception on submits so we decided to remove the workflow.

The error was still thrown on submit however... we found the problem to be SharePoint not removing the event handlers after we removed the workflow.

So we wrote a program to list the event handlers and this came up:

Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce1 11e9429c : Microsoft.SharePoint.Workflow.SPWorkflowAutostartEventReceiver

Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce1 11e9429c : Microsoft.SharePoint.Workflow.SPWorkflowAutostartEventReceiver

We programatically removed the event handlers and the error went away.

During my research, I came across this code post: http://www.koders.com/csharp/fidA2DC5531F641EC3F8CD9428D02C65295AD3F6C90.aspx

It's not related to SPS but his comment (line 884) might be a good hint:

// Not adding it to the disposer, since that could fail (e.g., OutOfMemoryException),
// which could cause a security hole. Instead, we'll clean it up manually later.
impersonationContext = WindowsIdentity.Impersonate(IntPtr.Zero);

Perhaps the event hanlder needs elevated permissions to run Or at least not run as the anonymous user

I think SharePoint doesn't actually treat the anonymous users as a real user (like IIS does), so runnign as the anonymous user my throw errors.

Steven.





Re: SharePoint - Development and Programming List event handlers X anonymous access

Steven Tapping

So in my habit of answering my own questions, I did a quick internet search and found you do need to run your events with elevated priviledges as the anonymous user is not a user.

From the post below, someone found a workaround:

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1220669&SiteID=1)

Joe Cataford:

Ok. I've created a workaround for this issue since Microsoft will not be providing a fix until SP1 of SharePoint. Basically what I've done is as follows:

  1. Create a custom content type that inherits from the "Custom List" content type (you could also use any other content type to derive from). You don't need to define any additional fields unless you want. I'm just using a basic custom list and then adding fields at runtime.
  2. With the content type, create a custom rendering template (ascx file to be placed in control templates) that is almost the same as the ListForm template found in "DefaultTemplates.ascx" (the default form used to render list items).
  3. Modify the custom rendering template by removing the SharePoint save button and adding an ASP.NET button that posts back. Handle the button's OnLoad event to set it's visibility and handle the button's OnClick event to save the item.
  4. To save the item in the OnClick event, use the "SPSecurity.RunWithElevatedPriviliges".
  5. Package all of this in a solution and feature and deploy to your site. You can then create lists that use this content type to allow anonymous users to add items that have workflows or event handlers attached to them.