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

I was wondering if there is any way I can get a SPsiteCollection object using a URL + if I can dispose it using "using" keyword as well.

I want to go through all the SPWebAPplications in a Local farm, and then each SiteCollection, its site and All respective subwebs.

I am able to do it but memory resources being used for too much, All I am actually trying to do is create a Sharepoint warmup script in a console App.

                   class Program
{
    public static ArrayList siteCollArray = new ArrayList();
    public static ArrayList FailedURLs = new ArrayList();
    public static ArrayList URLs = new ArrayList();
    public static InitValues initVals = new InitValues();
    public static Log log = new Log();

    static void Main(string[] args)
    {
        log.AddeventLog("Started running at " + DateTime.Now.ToString() + " " + "Running on the system " + System.Environment.MachineName

);

    GetSiteCollections();
    GetSubWebs();
    GetWebPage();

    foreach(string url in FailedURLs)
    {
        Console.WriteLine(url);
    }

    SendEmail();
}

private static void GetSiteCollections()
{
    try
    {
        SPFarm farm = SPFarm.Local;
        SPWebService service = farm.Services.GetValue<SPWebService>("");

        foreach (SPWebApplication webApp in service.WebApplications)
        {
            foreach (SPSite currentsite in webApp.Sites)
            {
                SiteCollection newSite = new SiteCollection(currentsite.Url, initVals.verbose);
                siteCollArray.Add(newSite);
            }
        }
    }
    catch (Exception ex)
    {
        log.AddeventLog("Error getting SiteCollections off Sharepoint Farm. Exception: " + ex.ToString());
    }
}

private static void GetSubWebs()
{
    try
    {
        foreach (SiteCollection currentSite in siteCollArray)
        {
            ArrayList subWebs = currentSite.subWebs;
            foreach (string URL in subWebs)
            {
                URLs.Add(URL);
            }
        }
    }
    catch (Exception ex)
    {
        log.AddeventLog("Error getting SubWebs off Site Collection. Exception: " + ex.ToString());
    }
}

public static void GetWebPage()
{
    foreach (String URL in URLs)
    {
        try
        {
            WebRequest request = WebRequest.Create(URL);
            request.Proxy = null;

            if ((initVals.userName == null) || (initVals.userName == ""))
            {
                request.Credentials = CredentialCache.DefaultCredentials;
            }
            else
            {
                CredentialCache myCache = new CredentialCache();
                myCache.Add(new Uri(URL), initVals.authType, new NetworkCredential(initVals.userName, initVals.password, initVals.domain));
                request.Credentials = myCache;
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Console.WriteLine(response.StatusDescription);
            using (Stream dataStream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(dataStream))
            {

                string responseFromServer = reader.ReadToEnd();
                reader.Close();
                dataStream.Close();
                response.Close();
            }
        }
        catch (Exception ex)
        {
            log.AddeventLog("Web Url: " + URL + " &  Exception Message: " + ex.Message.ToString());
            FailedURLs.Add(URL);
        }
    }
}

Caution its not complete code but enough to demonstrate what I am trying to do and main problem is memory leak.

Cheers

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

Instead of collecting all the SiteCollections and then all the Site I'd run though all in one sweep, collect the Url and deallocate when done with each:

In Main:

foreach (SPWebApplication webApp in service.WebApplications) 
{ 
  foreach (SPSite currentsite in webApp.Sites) 
  { 
    ProcessWeb(currentsite.RootWeb);
    currentsite.Dispose();
  } 
} 

In ProcessWeb:

URLs.Add(currentWeb.Url);
try
{
  foreach (SPWeb web in currentWeb.Webs)
  {
     ProcessWeb(web);
  }
}
catch ...
finally
{
  currentWeb.Dispose();
}
share|improve this answer
add comment

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.