0
votes

Hello I am having trouble with disposing the SPweb and SpSite. The web part is loading superslow because the objects are not dipsosed properly. I am pretty sure its SPWEB and Spsite causing it and suggestions. I am not sure how to dispose it properly.

public Dictionary<string, List<string>> newData()
    {

        string url = HttpContext.Current.Request.Url.Host;
        SPSite siteCollectionOuter = new SPSite(url))
        SPWeb sPWeb = sPSite.OpenWeb();
        SPListCollection sPListCollection = sPWeb.Lists;

        Dictionary<string, List<string>> newData = new Dictionary<string, List<string>>();
        List<string> subject = new List<string>();

        if (sPListCollection.Count > 0)
        {
            foreach (SPList list in sPListCollection)
            {


                if (list.BaseTemplate == SPListTemplateType.DiscussionBoard)
                {

                    SPListItemCollection ListRows = list.Items;
                    SPListItemCollection oldTopics = list.Folders;



                    foreach (SPListItem topic in oldTopics)
                    {



                        if (topic["Subject"] != null)
                        {

                            subject.Add(topic["Subject"];


                        }

                        else
                        {
                            subject.Add("");

                        }        

                    }
                }

            }
            newData.Add("Subject", subject); 
        }

        return newData;

    }
2
Welcome to SO! When you place a question try to add a minimum content: input sample, expected output sample, what did you try, research and where are you stacked. What did you try?David García Bodego

2 Answers

0
votes

You can use the using statement.

using(SPSite site=new SPSite(url))
{
     using(SPWeb web=site.OpenWeb(nameOftheWeb))
     {
         // your code goes here
     }
}

As you are inside a WebPart, consider using the SPContextclass. It provides access to the current Site via SPContext.Current.Site

0
votes
**will the code look like.** Should I also add try finally block like this. I am new to Sharepoint

SPSite oSPSite = null;
SPWeb oSPWeb = null;

try
{
   oSPSite = new SPSite("http://server");
   oSPWeb = oSPSite.OpenWeb(..);

   str = oSPWeb.Title;
}
catch(Exception e)
{
   // Handle exception, log exception, etc.
}
finally
{
   if (oSPWeb != null)
     oSPWeb.Dispose();

   if (oSPSite != null)
      oSPSite.Dispose();
}
/////////////////////////////////////////////////

    using(SPSite site=new SPSite(url))
    {
         using(SPWeb web=site.OpenWeb(nameOftheWeb))
         {
        SPListCollection sPListCollection = sPWeb.Lists;    
        Dictionary<string, List<string>> newData = new Dictionary<string, List<string>>();
            List<string> subject = new List<string>();

            if (sPListCollection.Count > 0)
            {
                foreach (SPList list in sPListCollection)
                {


                    if (list.BaseTemplate == SPListTemplateType.DiscussionBoard)
                    {

                        SPListItemCollection ListRows = list.Items;
                        SPListItemCollection oldTopics = list.Folders;



                        foreach (SPListItem topic in oldTopics)
                        {



                            if (topic["Subject"] != null)
                            {

                                subject.Add(topic["Subject"];


                            }

                            else
                            {
                                subject.Add("");

                            }        

                        }
                    }

                }
                newData.Add("Subject", subject); 
            }
         }
 return newData;

    }
         }