Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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)
        {
            try
            {
                SPSite site = SPContext.Current.Site;
                Guid siteGuid = site.ID;

                string url = string.Empty;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    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

4 Answers 4

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 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 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 : http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx

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 Sep 27 '11 at 10:31
    
Not worked with that explicitly but i did find this : stackoverflow.com/questions/358647/… –  Aabid Ali Sep 27 '11 at 10:50

Add the visitor to the Visitor-Group Here's a sample: http://geekswithblogs.net/madhawa/archive/2008/11/18/127171.aspx

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);
                    roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
                    list.RoleAssignments.Add(roleAssignment);
share|improve this answer

Your Answer

 
discard

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.