Question

I have added usercontrol on a sharepoint 2007 site. When I add an item it gets added as I'm the site admin. But when my friends try to login it throws "Unable to evaluate expression bcoz the code is optimized or native frame is on top of the stack" On list.update() and on the site it displays Access denied error.

The following is the code:

protected void btnOk_Click(object sender, EventArgs e)
{   
    try
    {             
        using (SPSite site = new SPSite("http URL"))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList list = web.Lists["List name"];
                UserItem = list.Items.Add();
                UserItem["col 1"] = Data1;
                UserItem["col 2"] = Data2;
                UserItem["col 3"] = Data3;
                UserItem["col 4"] = Data4;
                UserItem["col 5"] = Data5;
                UserItem.Update();
                list.Update();
            }
        }
    }
}

I have also tried the below URLs:

But even that is not working.

Was it helpful?

Solution

This could occur when your friends dont have the required previlages .Every thing works fine for u being a Site admin. One thing you need to do is modify your btnOk_Click code as shown below.

 SPSecurity.RunWithElevatedPrivileges(delegate()
 {
   using (SPSite site = new SPSite("http URL"))
    {
        using (SPWeb web = site.OpenWeb())
        {  
            web .AllowUnsafeUpdates = true;
            SPListItemCollection listitems = web.Lists["List name"].Items;
            SPListItem userItem = listitems.Add();
            userItem ["col 1"] = Data1;
            userItem ["col 2"] = Data2;
            userItem ["col 3"] = Data3;
            userItem ["col 4"] = Data4;
            userItem ["col 5"] = Data5;
            userItem.Update();               
            web .AllowUnsafeUpdates = false;
        }
    }
  });

try this...hope it will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top