SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom webpart with spgridview & button.the webpart should be visible to all the users. The gridview populates all items from custom list. When I click button, the page is redirected to list/newform.aspx , user adds information and page comes back to homepage.

Problem : when a vistor logs in , he sees the webpart and when he click button, I get error saying "Access denied"

Page load : 
protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
                SPSite site = SPContext.Current.Site;
                Guid siteGuid = site.ID;

                string url = string.Empty;
                    site = new SPSite(siteGuid);
                        using (SPWeb web = site.OpenWeb())
                            code goes here 

onbutton click : page.redirect is done to new form.aspx.

I think this is problem with SPSEcurity.runwithelevatedprividles

share|improve this question

You shouldn't need to elevate privileges just to redirect the user to the newform.aspx.

But in order to avoid the access denied error message, and if you want the users to add new list items via the out-of-box newform.aspx then you actually have to give those users permissions to add list items.

share|improve this answer
ya ur correct . so how do i give the permissions for the visitors to add items . i want to do things programatically – Nikhil Jangle Sep 27 '11 at 12:12
Hi i creted new instance of spsite & spweb in button click event . so the Response.redirect code is working for me . But as the visitor is logged in , he doesnt have access to newform.aspx . how to accomplish this ? – Nikhil Jangle Sep 29 '11 at 12:59

Access is denied because by default visitors do not have add items permissions on list . Why not create a new group to better suit your requirements. Also regarding RunwithElevatedPrivileges , Inside the delegate, members of the site object run with elevated privileges, but calls to members of web.Site would not.

For more information refer :

share|improve this answer
Well Nikhil if it is really needed what you can do is go to the list in question. Go to settings - > List Settings and permissions for this list . Select actions -> edit permissions , select visitors group and go to actions -> edit user permissions and change the permissions to contribute . – Aabid Ali Sep 27 '11 at 10:03
I need to achieve this programatically – Nikhil Jangle Sep 27 '11 at 10:31
Not worked with that explicitly but i did find this :… – Aabid Ali Sep 27 '11 at 10:50

Add the visitor to the Visitor-Group Here's a sample:

share|improve this answer
up vote 0 down vote accepted

Finally :)

web.AllowUnsafeUpdates = true; SPList list = web.Lists["list"]; list.BreakRoleInheritance(true);

                    SPUser allUser = web.AllUsers["NT Authority\\Authenticated Users"];

                    SPRoleDefinitionCollection roleDefCollection = list.ParentWeb.RoleDefinitions;
                    SPRoleDefinition roleDefinition = roleDefCollection["Contribute"];

                    SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)allUser);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.