Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Is this correct way of using the SPSite and SPWeb and the SPList.

private SPList list;

pulic void getList()
{
using (SPSite site = SPContext.Current.Web.Site.WebApplication.Sites[0])
{
    using (SPWeb web = site.RootWeb)
    {
        SPListCollection lists = web.Lists;
        list = lists.TryGetList("listname");
    }
} 
}

private override void PerformAction()
{
  //retrieving items from the splist
}

I think because of this code i get an error:

Detected use of SPRequest for previously closed SPWeb object. Please close SPWeb objects when you are done with all objects obtained from them, but not before. Stack trace: at Microsoft.SharePoint.SPListItemCollection.EnsureListItemsData() ....

Can the cause be: that i retrieve list and then so to say dispose the SPWeb object as im using it in using clause, but the list instance is still there and i try to retrieve items from it? I do NOT perform any insert or update actions only retrieve.

And if it is the cause how can i work around that? The retrievement of the list should happen in different method, because i do an override of the method.

I transformed the code to this, as msdn says rootweb I shouldn't dispose.

 using (SPSite site = new SPSite(SPContext.Current.Site.WebApplication.Sites[0].Url))
{
    SPWeb web = site.RootWeb;
    SPListCollection lists = web.Lists;
    list = lists.TryGetList("listname");
}

Still have the same error in the logs

share|improve this question

2 Answers 2

up vote 1 down vote accepted

If you would like to work with SharePoint, first at all, you should read this post. It describes how to dispose SharePoint objects.

In the post you can find this:

public void RootWebBestPractice()
{
    // New SPSite.
    using (SPSite siteCollection = new SPSite("http://moss"))
    {
        SPWeb rootWeb1 = siteCollection.RootWeb;
        // No explicit rootWeb1 dispose required.
    }  // siteCollection automatically disposed by implementing using().
    // rootWeb1 will be Disposed by SPSite.

    // SPContext and SPControl
    SPWeb rootWeb2 = SPContext.Current.Site.RootWeb;
    // Also would apply to SPControl.GetContextSite(Context);
    // No explicit rootWeb2 dispose required because it's obtained from SPContext.Current.Site.
}
share|improve this answer
    
I did read this posts many times ... but i still get this error and im disposing the object right, I'm always using the best practice in this case the "using" clause. Which automatically disposes the objects when they are out of the context –  Shkipper Jul 25 '12 at 8:12
    
I updated my answer –  Alexander Jul 25 '12 at 9:10
    
Also I suggest you to work with list items in the same context that you use to retrieve a list. –  Alexander Jul 25 '12 at 9:15

Just use it like this:

 SPSite currentSite = SPContext.Current.Site;
 SPList mySPList = currentSite.RootWeb.Lists.TryGetList("VoorbeeldLijst");

 if (mySPList != null)
 {
   ...
   ...
 }
share|improve this answer

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.