tags:

views:

19

answers:

1

Here's the scenario: We have an external SharePoint instance with anonymous access turned on. We want the document libraries open to the public. We do not want custom lists open. That was simple enough to configure.

Now we want to use those lists to create an attractive external ASP.Net web application. This web site will need anonymous enabled as well.

The problem is we need to access those lists from the web application without opening them to anonymous access.

We want to use the API (not web services) since this will be hosted on the same boxes.

So far we have been unable to create an SPUser with the appropriate access to open the lists.

SPContext is empty.

Doing this fails as well: SPSite temp = new SPSite(URL); SPUserToken token = temp.SystemAccount.UserToken;

SPSite site = new SPSite(URL, token); ...do stuff as the user.

RunWithElevatedPrivileges also fails.

Please help!

A: 

Any chance you are using SharePoint 2010? They have more options available to access from other applications. If not 2010, you are going to have to use a web service of some kind, either the OTB ones or your own Web Service that encapsulates your logic since the SP OM will not run on a non SharePoint box.

Independent of that, you could try getting the SPToken from the Application pool. Essentially

SPUserToken sysToken = SPContext.Current.Site.SystemAccount.UserToken;
using(var systemSite = new SPSite(SPContext.Current.Site.ID, sysToken))

Daniel Larson is a big proponent of this approach over using RunWithElevatedPriveleges. Check out his blog post on the matter.

John Ptacek
Not on 2010. We are on 2007. The app will be on the same box as SharePoint which is why we wanted to use the API. The above code throws an object reference not set to an instance of an object error on the first line.
SPCoder
Depending on how you are executing the code, the SPContext may not be set. You may need to get a reference to the site collection based on the URL you want to load.
John Ptacek