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 user that doesn't have permissions to a site (Limited Access) but have Full Control permission on one of the Document libraries within this site.

Now if the user tries to run my custom code he gets Access Denied error. Other users that have Full Control, Design or Contribute permissions on the site can run my solution without any problems.

How can I allow to users without permission on the site but only on the document library use my code?

share|improve this question
    
I would think you'd have to use the run with elevated privledges code if that's the case. The devs round here will know for sure. So I'll let them give you an answer. –  PirateEric May 19 '11 at 16:00
add comment

3 Answers

You need to find the line of code that the user does not have permission to execute and make sure the instance of the SPSite and/or SPWeb is created inside of a RunWithElevatedPrivileges.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
  using (SPSite site = new SPSite(SPContext.Current.Web.Site.ID)) {
    using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID)) {
      // get your document library using web instead of SPContext.Current.Web
    }
  }
});

If your code is executing in an item event receiver and you are using the properties parameter instead of SPContext, you will need to modify the code, but the RunWithElevatedPrivileges is the same.

share|improve this answer
    
I'd love to, but as it seems to me Page_Load method finish correctly, but Page_PreRender method doesn't start. So i don't sure which part of my code i should run with elevated privileges. –  Dmitriy Inozemtsev May 19 '11 at 18:32
    
If you can't find the right place to break into your code, you could check the error log and hopefully you'll see a stack trace where your error occurred. –  Laurie May 19 '11 at 19:07
    
There no error in my application, looks like SharePoint simply stops excution of my code. I also checked Event Viewer and LOGS of SharePoint but did find anything meaningful there. –  Dmitriy Inozemtsev May 19 '11 at 19:14
    
Can you post the code that is in Page_PreRender if that's where you think the problem is? If you are getting your list using the SPContext.Current.Web and the user does not have permission on that site, that's where the error is occurring. –  Laurie May 20 '11 at 3:06
add comment

Do you need to retain user access for permission trimming? If not, running elevated could be the quick and easy way to get around the problem. Remember that you'll need to open new instances of site/web inside the block as existing instances will still contain the current user's permission map.

If you don't want to do this, take a look at your code. Giving access to the document library will allow code running on a limited users behalf to access the list using the list GUID or URL/name directly. If you try to get to the list using the web that was opened under their permission, you will get access denied. Referencing /anything/ the user could not see from their browser will get access denied errors.

If the code needs to reference values from another list on which the user has no permission, you either have to run elevated, re-open the web and access the list or grant the user read access to that additional list.

If you can specify some details about what your code is touching that would be helpful.

share|improve this answer
add comment
up vote 0 down vote accepted

As I found out the problem was with SharePoint control which called ListFieldIterator. Looks like this control execute some permission check right after Page_Load method and for some reason deny access to parent page. To workaround this issue you may set ListFieldIterator.ControlMode to Display, but it's not really good solution as the user won't have ability to edit metadata represented by ListFieldIterator.

share|improve this answer
add comment

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.