Mark Macumber

I have a very small peice of code running on a MOSS 2007 server that should just return a list of sites within a SPWebCollection. (shown below), but I get this error:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

when I run the code. Can anyone please help with this

Thanks!

Code:
ArrayList siteNames = new ArrayList();
SPSite siteCollection = new SPSite("http://tfsdev:25647");
SPWebCollection sites = siteCollection.AllWebs;

foreach(SPWeb site in sites)
{
siteNames.Add(site.Name);
}


Re: SharePoint - Development and Programming Access is denied Help!

Ishai Sagi

where is that code running from is it a web part is it a console application

Who's credentials is the code using administrator a user

Most probably you are running the code from a location that is not trusted, or the application is set (in the web.config) not to trust the code.






Re: SharePoint - Development and Programming Access is denied Help!

Mark Macumber

I have it so that My website (call it WEBA) makes a call to my WCF service which resides on the MOSS Server, this service is hosted by an ASP.NET 2.0 website which simply calls a class library which then tries to access Sharepoints Data.

So I guess it would run as the "Network Service" user.

Is Sharepoint throwing this error

Should I run the code as a domain user of somesort then make that user some kind of admin user on sharepoint

Thanks for the help!




Re: SharePoint - Development and Programming Access is denied Help!

Steve Mushkat

The culprit is the account you're using. I'm doing something similar in code, and when I use an admin account, when I access the SPSite.AllWebs property no problem, but when I switch to a visitor user I get the access denied exception.

The solution to this is to use impersonation inside the code - inside the code you can switch to a specific user ID to use, or there's a technique to switch to the service account ID (you can probably find this documented out there, haven't tried my SP2003 code on MOSS to know if it works yet). If you can control the account used by the web service give that account site admin access. Remember if you have to store a credential to use, even in code, best to encrypt it!!!

For me, I'm trying to show the user the list of sites they have permissions to see, so I can't really use impersonation - otherwise the user will see all sites regardless of their site access. So instead I'll need to add in some code that checks the user's site permissions then decides whether or not to show the site.





Re: SharePoint - Development and Programming Access is denied Help!

Steve Mushkat

Well, Microsoft's made things a bit easier for us now! There's a new approach to do impersonation that makes things a bit easier. Found the method here: http://www.sharepointblogs.com/mirjam/archive/2006/11/02/15669.aspx

In the code, you can specify the ID to use, but don't need to include a password.

SPSiteCollection sites = mySPWebApp.Sites;
foreach (SPSite site in sites)
{
// set this flag to false so the access denied exception isn't passed
// back to the browser
try
{
site.CatchAccessDeniedException = false;
SPWeb web = site.OpenWeb();
SPUser admin = web.AllUsers["domain\\serviceaccount"]; // need to make sure this account has access to the target site
SPUserToken token = admin.UserToken;

SPSite impersonatedSite = new SPSite(site.Url, token);

....

impersonatedSite.Dispose();





Re: SharePoint - Development and Programming Access is denied Help!

Mark Macumber

Hey, thanks Steve, this looks like what I need to solve this little issue.

Is there a way to get a list of all users for a site (i.e. a list of all available users for a site) other than through the Sharepoint Frontend




Re: SharePoint - Development and Programming Access is denied Help!

amishjt

Hi Mark,

you can use "SPWeb.AllUsers" property to get list of all users who are members of the site.

Regards,

Amish