2009/08/11

OutOfMemory Exception

I am using customList to add some requests from Anonymouse useres.

But when I used EventHandler it gives me exception "OutOfMemory exception" while my code was as the following:

SPSecurity.RunWithElevatedPrivileges(delegate()
{

SPWeb web = SPControl.GetContextWeb(Context);
web.AllowUnsafeUpdates =true;

SPListItem item = web.Lists["MyList"].Items.Add();
item["RequestedName"] = textBoxName.Text;
item["DateRequestRecievd"] = DateTime.Now;
item["RequestedPhoneNo"] = textBoxMobileNo.Text;
item["RequestType"] = dropDownListRequestType.SelectedItem.Value;
item["RequestSubject"] = textBoxRequestSubject.Text;
item["state"] = textBoxState.Text;
item["IdentityNo"] = textBoxIdentityNo.Text;
item.Update();
web.AllowUnsafeUpdates =false;
});

I used that code because I know that "SPSecurity.RunWithElevatedPrivileges" imporsenates the current user permissions with "system Account" Permissions. But it doesn't work with Anonymouse users.


There the solution of this Issue Anonymouse doesn't have permission to access assembly. So, you need to Authonticate as an authonticated user. I created a user that will be used for imporsenation, "Domain\Visitors". I wrote the following code
1 SPSecurity.RunWithElevatedPrivileges(delegate()
2 {
3 using (SPSite site = new SPSite(SPContext.Current.Site.Url))
4 {
5
6 using (SPWeb parentWeb = site.OpenWeb())
7 {
8 string userName = ConfigurationManager.AppSettings["contactUsUser"];
9 SPUser user = parentWeb.AllUsers[userName];
10 ////YOU CAN ALSO HARD CODE THE USER AS BELOW
11 //SPUser user = parentWeb.AllUsers["DOMAIN\\Vistors"];
12 SPUserToken token = user.UserToken;
13
14 using (SPSite s = new SPSite(SPContext.Current.Site.Url, token))
15 {
16 using (SPWeb web = s.RootWeb)
17 {
18 //MY LOGIC HERE
19 }
20 }
21
22 }
23
24 }
25 });

you can see my post on MSDN Forum

No comments: