tags:

views:

696

answers:

2

Hi,

We have created a virtual directory under the root sharepoint site. This virtual directory hosts a Web Service which is accessed anonymously. In the code we are doing the following:

using(SPSite site = new site(some uri))
{
    using(SPWeb web = site.RootWeb)
    {
        SPList list = web.Lists["SomeList"];
        SPListItem item = list.GetItemById(1); // Exception Here, COM Exception        
    }
}

This does not work even when using RunWithElevatedPrivileges.

However, if the HttpContext.Current is set to null, before creating the SPSite, everything works fine.

Any pointersas to why this is happening will be very helpful.

Kind regards,

A: 

This may not be the cause of your problem, but your SPWeb does not need to be disposed of, as per Roger Lamb here, which is a distillation of Microsoft's best practices document here.

Andy Mikula
Please see this thread, facing similar issues:http://mphacker.spaces.live.com/blog/cns!8040CC624DDC5404!290.entry
SharePoint Newbie
If this is the answer to your question, submit it as an answer and accept - it will help others who have similar issues.
Andy Mikula
A: 

On the contrary! It is considered a good practice to always check for null and dispose both your SPSite and your SPWeb explicitly, even though SPSite arguably will dispose your SPWeb for you.

And that is not part of the problem either :-) At the worst that would cause the worker process to recycle a bit more often than usual...

Could you maybe add some more code (the whole class).

Also please include full stack trace for the error. (details on how to enable stack trace and debugging here http://blogs.importchaos.com/alonsorobles/2008/06/09/enabling-the-sharepoint-safe-mode-call-stack-disabling-custom-errors-and-enabling-compilation-debugging/ )

Also i take it you are sure that the list and the item exists and allow anonymous access?

regards Anders Rask

Anders Rask
It is actually possible to dispose of the SPWeb that is handling your code, and if you're running on the RootWeb, your site will go down, which is not a pretty sight!
Andy Mikula
Andy is correct. The SPSite object should be disposed of because its being created; on the otherhand the SPWeb object (Site.RootWeb) does not need to be disposed of.
Jason
The using does dispose the SPWeb. The list and items do exist and are accessible to anonymous users. I can get a handle to both Web and List. But when I try and open a list item....COM Exception
SharePoint Newbie
When the user is not anonymous, the code works correctly.
SharePoint Newbie
Please see this thread...facing sinilar issues:http://mphacker.spaces.live.com/blog/cns!8040CC624DDC5404!290.entry
SharePoint Newbie
Yes - the SPWeb should -not- be disposed of in this case, because it is being provided by SPSite.RootWeb. This may be tangential to your problem, but it will cause issues down the line.
Andy Mikula
Sorry i had not seen the update that RootWeb no longer needs to be disposed. http://blogs.msdn.com/rogerla/archive/2008/10/04/updated-spsite-rootweb-dispose-guidance.aspx"unknown": please supply us with a proper stack trace for the error.
Anders Rask
Did you try impersonating with a specific user? For example system.SPUserToken systemAccountToken = SPContext.Current.Site.SystemAccount.UserToken;Now open a new site with the SPSite ctor that takes both url and token.
Anders Rask