Hi I am using the SharePoint namespace to pull items from various lists throughout the site. My web part works, but only on my account. When I try it on another account it gives me ‘Error: Access Denied’ for the page. I have taken all web parts out and have only this web part on the page. When I remove the following lines the page loads for everyone, when I add it back in however it does not work. I am guessing this is some permission problem. I was wondering is there away to programatically query different lists on SharePoint by assigning a user id to use? Thank you for any help
... SPSite site = new SPSite(_SPSite); SPWeb eachWeb = site.AllWebs[0]; SPListItemCollection myItemCollection = eachWeb.Lists['Listings'].Items; ...
You’re correct, the access denied error is occurring when you’re using an account which does not have access to the ‘Listings’ list in the current website.
The easiest way around the issue is to use a SPSecurity.RunWithElevatedPrivleges call:
which will run whatever code is contained in the anonymous method using the SharePoint/System account, granting complete control. Be careful when using this technique though, as it equivalent to running code at full trust with a super user account. There are other caveats to be aware of as well.